一:利用ShareObject的同步事件来将消息广播到所有客户端:
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" backgroundColor="white" layout="vertical" initialize="BasicPracticeSO();">
- <mx:Script>
- <![CDATA[
- private var nc:NetConnection;
- private var shareObject:SharedObject;
- private var monthDay:Date;
- private var isConnectSuccess:Boolean;
- private static const RTMP_URL:String="rtmp://localhost/BasicSO";
- private function BasicPracticeSO():void{
- nc=new NetConnection;
- nc.connect(RTMP_URL);
- nc.addEventListener(NetStatusEvent.NET_STATUS,operateShareObject);
- }
- private function operateShareObject(evt:NetStatusEvent):void{
- isConnectSuccess=(evt.info.code=="NetConnection.Connect.Success");
- if(isConnectSuccess){
- shareObject=SharedObject.getRemote("userInfo",nc.uri,false);
- monthDay=new Date();
- shareObject.connect(nc);
- shareObject.addEventListener(SyncEvent.SYNC,detectSO);
- }
- }
- private function detectSO(e:SyncEvent):void{
- for(var chng:uint;chng<e.changeList.length;chng++){
- switch(e.changeList[chng].code){
- case "change":
- updateAllClientMsg();
- break;
- case "clear":
- break;
- case "success":
- break;
- default:
- break;
- }
- }
- }
- private function updateAllClientMsg():void{
- soList.text+=shareObject.data.msg+"\n";
- }
- private function addDataToShareObject():void{
- shareObject.setProperty("msg",msgText.text);
- soList.text+=msgText.text+"\n";
- msgText.text="";
- }
- ]]>
- </mx:Script>
- <mx:Label text="ShareObject data:"/>
- <mx:TextArea id="soList" width="300" height="120">
- </mx:TextArea>
- <mx:Form>
- <mx:FormHeading label="add data to ShareObject:"/>
- <mx:FormItem label="msg:">
- <mx:TextInput id="msgText"/>
- </mx:FormItem>
- <mx:FormItem>
- <mx:Button label="send Msg" click="addDataToShareObject();"/>
- </mx:FormItem>
- </mx:Form>
- </mx:Application>
二:在FMS服务端通过编码方试广播消息到所有客户端:
1.客户端代码:
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" backgroundColor="white" layout="vertical" initialize="initAppSO();">
- <mx:Script>
- <![CDATA[
- import com.client.ClientObj;
- private var nc:NetConnection;
- private var shareObject:SharedObject;
- private var monthDay:Date;
- private var isConnectSuccess:Boolean;
- private static const RTMP_URL:String="rtmp://localhost/BasicSO/chat";
- private function initAppSO():void{
- nc=new NetConnection;
- nc.connect(RTMP_URL);
- var clientObj:ClientObj=new ClientObj(soList);
- nc.client=clientObj;
- nc.addEventListener(NetStatusEvent.NET_STATUS,checkStatus);
- }
- private function checkStatus(evt:NetStatusEvent):void{
- isConnectSuccess=(evt.info.code=="NetConnection.Connect.Success");
- if(isConnectSuccess){
- soList.text+="connect success"+"\n";
- }
- }
- private function addDataToShareObject():void{
- var msg:String=msgText.text;
- var responder:Responder=new Responder(callback);
- nc.call("writeFile",responder,msg);
- msgText.text="";
- }
- private function callback(str:String):void{
- // ToDo something
- }
- ]]>
- </mx:Script>
- <mx:Label text="chating list data:"/>
- <mx:TextArea id="soList" width="300" height="220">
- </mx:TextArea>
- <mx:HBox>
- <mx:Label text="msgText:"/>
- <mx:TextInput id="msgText"/>
- <mx:Button label="send Msg" click="addDataToShareObject();"/>
- </mx:HBox>
- </mx:Application>
2.FMS服务器端代码:
这里我把聊天记录写到了服务器端的一个文件里.其实写不写都无所谓.
- application.onAppStart=function(){
- trace("app is started");
- };
- application.onConnect=function(client,name){
- client.id=name;
- application.acceptConnection(client);
- var file=new File("chatfile.text");
- client.writeFile=function(msg){
- file.open("text","append");
- if(file.isOpen){
- msg=client.id+":"+client.ip+":say:\n"+msg;
- trace(msg);
- file.write(msg);
- file.close();
- }
- //client.call("serverToClient",null,msg);
- for(var i=0;i<application.clients.length;i++){
- application.clients[i].call("serverToClient",null,msg);
- }
- }
- };
- application.onDisconnect=function(client){
- for(var i=0;i<application.clients.length;i++){
- msg=application.clients[i].id+" left!";
- application.clients[i].call("serverToClient",null,msg);
- }
- }