实现的一般步骤是:
step 1: 定义NetConnection对象连接rtmp,并监听NetStatusEvent.NET_STATUS事件
step 2: 在NetStatusEvent.NET_STATUS事件里判断event.info.code=="NetConnection.Connect.Success",通过SharedObject.getRomote()得到SharedObject对象
step 3: 监听SharedObject对象的SyncEvent.SYNC事件,并sharedObj.connect(netConnection)
step 4: 这个时候可以利用sharedObj.send()向服务器发送消息,然后服务器再把登录消息广播给当前连接的所有客户端
step 5: 在SyncEvent.SYNC事件里可以取到sharedObj.data里的数据了
step 6: 定义一个供服务器调用后得到返回值的函数,需要声明为public
PS:
服务器端和客户端,都是在操作SharedObject对象,客户端是读取,服务器端是写入(只有服务器执行写入才能保证数据无误)
服务器端发送消息给每个客户端,分为客户端请求和服务器端请求实现
* 客户端请求一般是调用sharedObj.send("getServerData","message")来实现
* 服务器端是调用netConnection.call("后台方法",null,"message"),然后后台方法定义通过soUser.SendMessage("getServerData", new object[]{"message"})来发送消息
服务器端代码(.net):
using System; using System.Collections.Generic; using System.Text; using FluorineFx.Messaging.Adapter; using FluorineFx.Messaging.Api; using FluorineFx.Messaging.Api.SO; namespace ServiceLibrary5 { public class MyChatApp : ApplicationAdapter { public override bool AppConnect(IConnection connection, object[] parameters) { string name = parameters[0].ToString(); ISharedObject soUser = this.GetSharedObject(connection.Scope, "onlineUserList"); if (soUser == null) { this.CreateSharedObject(connection.Scope, "onlineUserList", false); soUser = this.GetSharedObject(connection.Scope, "onlineUserList"); } soUser.SetAttribute(name, name); //这里是为了提供给没有parameters参数,而又需要访问连接时传过来的参数 connection.Client.SetAttribute("currentName", name); return true; } public override void AppDisconnect(IConnection connection) { string name = connection.Client.GetAttribute("currentName").ToString(); ISharedObject soUser = this.GetSharedObject(connection.Scope, "onlineUserList"); if (soUser != null) { soUser.RemoveAttribute(name); } soUser.SendMessage("getServerData", new object[] { name + "已经断开" }); base.AppDisconnect(connection); } //利用服务器端发送消息的通用方法,客户端需通过netConnection.call()来调用该方法 public void sendMessage(IConnection connection, string msg) { string userName = connection.Client.GetAttribute("currentName") as string; ISharedObject soUser = GetSharedObject(connection.Scope, "onlineUserList"); soUser.SendMessage("getServerData", new object[] { msg }); } } }
客户端代码(flex):
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" backgroundColor="0xffffff"> <mx:List x="404" y="122" backgroundColor="#3A6EA5" width="115" height="289" id="dlOnlineUsers"></mx:List> <mx:Label x="420" y="92" text="当前在线用户" fontSize="13"/> <mx:Label x="67" y="122" text="你的昵称:" fontSize="13"/> <mx:TextInput id="txtName" x="134" y="123"/> <mx:Button x="302" y="121" label="登录" click="onLogin()" fontSize="12"/> <mx:Label x="67" y="225" text="系统信息" fontSize="13"/> <mx:TextArea id="txtMessage" x="67" y="279" width="287" backgroundColor="#3A6EA5" color="0xffffff" /> <mx:Button x="270" y="420" label="注销" fontSize="12" click="onDisconnect()" /> <mx:Script> <![CDATA[ private var nc:NetConnection; private var soUser:SharedObject; private function onLogin():void { nc = new NetConnection(); nc.addEventListener(NetStatusEvent.NET_STATUS,onNetStatus); nc.connect("rtmp://localhost:8323/MyChatRoom",this.txtName.text); nc.client = this; } private function onNetStatus(event:NetStatusEvent):void { if(event.info.code == "NetConnection.Connect.Success") { soUser = SharedObject.getRemote("onlineUserList",nc.uri,false); if(soUser != null) { soUser.addEventListener(SyncEvent.SYNC,onSYNC); soUser.connect(nc); soUser.client = this; } soUser.send("getServerData","用户【" + this.txtName.text + "】已连接"); } } private function onDisconnect():void { nc.close(); } private function onSYNC(event:SyncEvent):void { var arrUser:Array = new Array(); for(var str:String in soUser.data) { arrUser.push(soUser.data[str]); } this.dlOnlineUsers.dataProvider = arrUser; } public function getServerData(message:Object):void { this.txtMessage.text += message.toString()+"\n"; } ]]> </mx:Script> </mx:Application>
时间: 2024-10-08 21:45:08