最近在写一个RIA系统,要实现广播消息,想用broadcastMsg来解决,结果发现fms官方文档的实际有点问题,所以就把我写的东西贴出来,希望对读者有帮助。
客户端代码:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
private var cam:Camera;
private var mic:Microphone;
private var cn:NetConnection;
private var ns:NetStream;
private var conStr:String="rtmp://192.168.1.188/AvFashion"
private var userName:String;
private var cnResponder:Responder=new Responder(onReply);
private function getConnection():void
{
userName=user.text;
cn=new NetConnection();
cn.client=new Object();
cn.client.sbmsg=function(msf:String):void{Alert.show(msf);};
cn.connect(conStr,userName);
cn.addEventListener(NetStatusEvent.NET_STATUS,netConnetHanndler);
//cn.client=this;
cn.call("msgFromSrv",cnResponder,"中国");
}
private function setVideo():void
{
cam=Camera.getCamera();
}
private function netConnetHanndler(msg:NetStatusEvent):void
{
if(msg.info.code == "NetConnection.Connect.Success")
{
Alert.show("连接成功");
}
else
{
Alert.show("连接失败");
}
}
private function onReply(result:String):void
{
msg.text=result;
}
]]>
</mx:Script>
<mx:VideoDisplay x="170" y="38" width="298" height="224"/>
<mx:Label x="170" y="280" text="UerName:" />
<mx:Button x="359" y="278" label="Get Connecton" id="GetConnection" click="getConnection()"/>
<mx:TextArea x="170" y="315" width="298" id="msg"/>
<mx:TextInput x="239" y="278" width="102" id="user"/>
</mx:Application>
服务端代码:
//定义用户的列表
userList=[];
application.onAppStart=function()
{
trace("fms set up server......");
trace("magictanghehe.......");
}
application.onConnect = function(currentClient,username)
{
application.acceptConnection(currentClient);
currentClient.username = username;
userList.push(username);
currentClient.msgFromSrv= function(value)
{
return "Welcome to: "+currentClient.username+" for bring "+value.toString();;
}
application.broadcastMsg("sbmsg", "Hello World");
trace("当前用户列表"+userList);
}
fms3,flex3上调试通过。