使用 Flerry 在 Java 和 Flex 之间进行消息传递
我目前正在开发一个项目,需要在不使用 Tomcat 这样的服务器的情况下从空中与 java 进行通信。为此,我找到并使用了 Flerry。
只要我尝试从我最初从 Air 实例化的类发送消息,Java 和 Air 之间的通信就没有问题。
我尝试做的是来自 java 中消息类的消息的下标,并使用该类从 Java 向 Air 发送信息和错误。
MessageController.java:
public class MessageController
{
public MessageController()
{
}
public static void sendErrorMessage(String errorMessage)
{
NativeObject.sendMessage(errorMessage, "error");
}
public static void sendInfoMessage(String infoMessage)
{
NativeObject.sendMessage(infoMessage, "info");
}
}
在 Air 中,我在消息传递类上创建一个 NativeObject 并订阅消息:
var messageController:NativeObject = new NativeObject();
messageController.source = "controller.MessageController" ;
messageController.singleton = true;
messageController.debug = false;
messageController.addEventListener(FaultEvent.FAULT, onFileControllerFault, false, 0, true);
messageController.subscribe("info", infoMessageHandler);
messageController.subscribe("error", errorMessageHandler);
正如您所猜到的,这不起作用。 看来我只能从我直接订阅的类中分派消息,例如,如果我这样做:
messageController.start();
并且在我的 MessageController.java 中我这样写:
public void start()
{
NativeObject.sendMessage("test message", "info");
}
infoMessageHandler 接收一条包含测试消息的消息,因为它应该。
如何从 Java 中的任何类发送消息并在 Air 端捕获它们?
I'm currently working on a project that need to communicate with java from air without the use of a server like Tomcat. For this i found and use Flerry.
Communicating between Java and Air is no problem, as long as I try to send a message from the class that I initially instantiated from Air.
What I try to do is subscript to messages from a Message Class in java and use that class to send info and errors to Air from Java.
MessageController.java:
public class MessageController
{
public MessageController()
{
}
public static void sendErrorMessage(String errorMessage)
{
NativeObject.sendMessage(errorMessage, "error");
}
public static void sendInfoMessage(String infoMessage)
{
NativeObject.sendMessage(infoMessage, "info");
}
}
In Air I create a NativeObject on the messaging class and subscribe to the messages:
var messageController:NativeObject = new NativeObject();
messageController.source = "controller.MessageController" ;
messageController.singleton = true;
messageController.debug = false;
messageController.addEventListener(FaultEvent.FAULT, onFileControllerFault, false, 0, true);
messageController.subscribe("info", infoMessageHandler);
messageController.subscribe("error", errorMessageHandler);
As you would have guessed, this doesn't work.
It seems that I am only able to dispatch messages from the class that I subscribe to directly, for example if I do this:
messageController.start();
and in my MessageController.java i put this:
public void start()
{
NativeObject.sendMessage("test message", "info");
}
the infoMessageHandler receives an message containing test message, as it should.
How can I dispatch messages from whatever class in Java and catch them on the Air side?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定我是否完全理解这个问题,但是是否有理由不能通过 Java“通信”类简单地发送和接收所有消息?如果这有效,我只需在 Java 接口/通信类中设置公共方法并完成它。
(自从我深入研究 Flerry 的工作原理以来已经有一段时间了,所以我记不起足够多的内容来指出您所看到的行为的可能原因。)此外,Flerry 是开源的,而且根本不是很大。如果你真的想知道为什么它会以某种方式表现,我打赌你可以通过查看源代码来弄清楚。 (我保证,不会像某些 Spring 或 Hibernate 代码库,甚至 BlazeDS 那样需要 6 个月的时间来学习。)
我确实在一个小应用程序中使用 Flerry,但现在我想一想,我只有一个可以调度任何内容的类到伟创力!但我觉得你可能需要以不同的方式描述你面临的问题,因为这对我来说听起来像是预期的行为。
I'm not sure I fully understand the issue yet, but is there a reason you can't simply send and receive all messages through a Java 'communication' class? If that's working, I'd just set up public methods in Java interfacing/communications class and be done with it.
(It's been awhile since I've dug into how Flerry works so I can't recall enough to point out the likely cause of the behavior you're seeing.) Also Flerry is open source, and not very big at all. If you really want to know why it's behaving in a certain way, I'd wager you could figure it out by looking at the source. (Won't take 6 months to learn like some Spring or Hibernate code base, or even BlazeDS, I promise.)
I do use Flerry in a small app, but now that I think about it, I only have one class which dispatches anything to Flex! But I feel like you may need to describe the problem you're facing differently, because it sounds like expected behavior to me.