论坛首页 编程语言技术论坛

blazeds在服务器端取得发送消息内容

浏览 3107 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-05-12   最后修改:2009-05-12

突发奇想看了下源代码,写了个简单的类,因为 messaging -config.xml的缺省adapther是actionScriptAdapter

我写了个类继承自他,就能得到从客户端发送的消息

package adapter;

import flex.management.runtime.messaging.services.messaging.adapters.ActionScriptAdapterControl;
import flex.messaging.Destination;
import flex.messaging.MessageDestination;
import flex.messaging.io.amf.ASObject;
import flex.messaging.messages.CommandMessage;
import flex.messaging.messages.Message;
import flex.messaging.services.MessageService;
import flex.messaging.services.messaging.adapters.ActionScriptAdapter;



/**
 *  服务器启动时执行步骤
 *  
 *  1.
 *  
 *  init messageing-config.xml配置了一个destionation
    setDestination
    setController
    
    
    如果messageing-config.xml配置了两个destination
    
    执行结果
     init
    setDestination
     init
    setDestination
    setController
    setController
 * 
 * **/
public class MyAdapter extends ActionScriptAdapter {

private ActionScriptAdapterControl controller;

   
    //--------------------------------------------------------------------------
    //
    // Constructor
    //
    //--------------------------------------------------------------------------
    
    /**
     * Constructs a default <code>ActionScriptAdapter</code>.
     */
    public MyAdapter()
    {
        super();
        System.out.println("init");
    }
    
    //--------------------------------------------------------------------------
    //
    // Public Getters and Setters for ServiceAdapter properties
    //                             
    //--------------------------------------------------------------------------

    /**
     * Casts the <code>Destination</code> into <code>MessageDestination</code>
     * and calls super.setDestination.
     * 
     * @param destination
     */
    public void setDestination(Destination destination)
    {
        Destination dest = (MessageDestination)destination;
        super.setDestination(dest);
        System.out.println("setDestination");
    }
    
    //--------------------------------------------------------------------------
    //
    // Other Public APIs
    //                 
    //--------------------------------------------------------------------------
    
    /**
     * Handle a data message intended for this adapter.
     */
    //主要的发送方法
    public Object invoke(Message message)
    {
    	if(message instanceof CommandMessage){
    		CommandMessage cm=(CommandMessage)message;
    		
    	}
        MessageService msgService = (MessageService)getDestination().getService();
        
        System.out.println(msgService.getSubscriberIds("ChatTopic", "chat_team", null));//所有订阅者的id
        
        msgService.pushMessageToClients(message, true);//发送消息
        
        msgService.sendPushMessageFromPeer(message, true);//发送消息
        
       
        
        System.out.println("invoke message");
      
        Object obj=message.getBody();
		System.out.println("message:"+obj.toString());//得到消息体
		if(obj instanceof ASObject){ //把它转成 AsObject 因为AsObject是个HashMap 
		    //所以就能猜见如何得到消息内容
			System.out.println("isAsObject");
			ASObject as=(ASObject)obj;
			System.out.println("text:"+as.get("text")); //输出 (as message.body.text="你好"中的)你好
			as.put("text", "大家好才是真的好");//还可以修改从客户端发过来的数据, 客户端text 输出内容就是 “大家好才是真的好”
			
		}
        return null;
    }

    /**
     * Invoked automatically to allow the <code>ActionScriptAdapter</code> to setup its corresponding
     * MBean control.
     * 
     * @param broker The <code>Destination</code> that manages this <code>ActionScriptAdapter</code>.
     */
    protected void setupAdapterControl(Destination destination)
    {
    	
        controller = new ActionScriptAdapterControl(this, destination.getControl());
        controller.register();
        setControl(controller);
        System.out.println("setController");
    }

}

 

	private function sendMessage():void{
				if(textEditor.text==""){
					Alert.show("不能发送空消息");
					return;
				}
				
				var message:AsyncMessage=new AsyncMessage();
//				message.body.text=userName.text+":"+msg.text+" ";
				var sendU:String=userName.text+":"+new Date();
				message.headers=new Array;
				message.headers["pro"]=5;
				message.body.text=textEditor.text;
				
				message.body.userAndDate=sendU;
				message.body.fontWeight=this.fontWeight;//粗体
				message.body.fontStyle=this.fontStyle;//倾斜
				message.body.fontColor=this.fontColor;//下划线
				message.body.fontTextDecoration=this.fontTextDecoration;//下换线
				
				producer.send(message);
//				vd.addChild(getTxtMessage(sendU));
//				vd.addChild(parseMessage(textEditor.text));
				textEditor.text="";
}
                                                                                                                                                              
<?xml version="1.0" encoding="UTF-8"?>
<service id="message-service" 
    class="flex.messaging.services.MessageService">

    <adapters>
        <!--adapter-definition id="actionscript" class="flex.messaging.services.messaging.adapters.ActionScriptAdapter" default="true"/-->
        <!-- <adapter-definition id="jms" class="flex.messaging.services.messaging.adapters.JMSAdapter"/> -->
         <adapter-definition id="myadapter" class="adapter.MyAdapter" default="true"/>
    </adapters>

    <default-channels>
        <channel ref="my-polling-amf"/>
    </default-channels>
	<!--destination id="mychat">
		<adapter ref="myadapter"/>
	</destination-->
	<destination id="ChatTopic">
<properties>
<network>
<subscription-timeout-minutes>0</subscription-timeout-minutes>
</network>
<server>
<message-time-to-live>0</message-time-to-live>//这些都是关于subtopic,header的,我之前发过blazeds过滤消息
<allow-subtopics>true</allow-subtopics>
<subtopic-separator>.</subtopic-separator>
</server>
</properties>


</destination>
</service>
 
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics