Flex,推送消息,ServiceAdapter,BlazeDS

时间:2018-04-18 15:30:25

标签: actionscript-3 flex push-notification blazeds

我有Flex 3.6A + BlazeDS + java6 webapp。

我想从服务器推送必须由客户端拦截的消息(.mxml页面)。 Googlin周围我设法建立以下示例:

public class TestMessaging extends ServiceAdapter {

private String previousMsg = "";
private volatile boolean running;

private Message createTestMessage() {
    AsyncMessage msg = new AsyncMessage();
    msg.setDestination("RandomDataPush");
    msg.setClientId(UUIDUtils.createUUID());
    msg.setMessageId(UUIDUtils.createUUID());
    msg.setBody(BarValues.STATUS);

    return msg;
}

@Override
public void start(){    
    super.start();

    Thread messageSender = new Thread(){
        public void run(){
            running = true;
            while(running){
                sendMessageToClients(createTestMessage());
                secondsToSleep(3);
            }
        }
    };

    messageSender.start();        
}
/**
 * @see flex.messaging.services.ServiceAdapter#stop()
 */
@Override
public void stop(){
    super.stop();
    running = false;
}

public void sendMessageToClients(Message msg) {
    if (!msg.getBody().equals(previousMsg)) {
        previousMsg = msg.getBody().toString();
        ((MessageService) getDestination().getService()).pushMessageToClients(msg, false);
    }
}

@Override
public Object invoke(Message message) {
    if (message.getBody().equals("stop")) {
        running = false;
    } else if (message.getBody().equals("start")) {
        start();
    }
    return null;
}

private void secondsToSleep(int seconds) {
    try{
        Thread.sleep(seconds * 1000);
    }catch(InterruptedException e){
        System.out.println("TestServiceAdapter Interrupted while sending messages");
        e.printStackTrace();
    }
}        

}

在.mxml中我有:

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="100%" height="100%" creationComplete="init()">

<mx:Script>
    <![CDATA[
      private function messageHandler(message:IMessage):void {
         randomNumbers = message.body as String;
         if (randomNumbers == null) {
         var msg:AsyncMessage = new AsyncMessage();
         msg.body = "stop";
         producer.send(msg);
    } else Alert.show(randomNumbers.toString());
 }

private function init():void {
   var msg:AsyncMessage = new AsyncMessage();

   msg.body = "stop";
   producer.send(msg);
   consumer.subscribe();
}

private function start():void {
   var msg:AsyncMessage = new AsyncMessage();

   msg.body = "start";
   producer.send(msg);
   consumer.subscribe();
}

private function handleFault(event:MessageFaultEvent):void {
   Alert.show(event.faultString);
}

private function ack(event:MessageAckEvent):void {
   if (event.message.body != null) {
      randomNumbers = event.message.body as String;
      Alert.show(randomNumbers.toString());
   }
}

<mx:Producer id="producer" destination="RandomDataPush" acknowledge="ack(event)"/>
<mx:Consumer id="consumer" destination="RandomDataPush" message="messageHandler(event.message)" />

</mx:Application>

问题是:

1)将线程打开,我无法推送消息

2)当我打开应用程序时,线程会自动启动。为了防止这种情况,我发送&#34;停止&#34;当调用flex页面的init()方法时(但实际上很糟糕)

我想要做的是:当执行java查询(或复杂的详细说明开始)时,我想启动服务并发送手动调用 sendMessageToClients 方法的消息(如果是,则不带线程)可能)并且,当查询或详细说明结束时,停止服务。

你能帮帮我吗? 谢谢大家

0 个答案:

没有答案