即時通訊(RPC)的Rtmp實現--代碼實現篇

實現的通常步驟是:服務器

step 1: 定義NetConnection對象鏈接rtmp,並監聽NetStatusEvent.NET_STATUS事件ide

step 2: 在NetStatusEvent.NET_STATUS事件裏判斷event.info.code=="NetConnection.Connect.Success",經過SharedObject.getRomote()獲得SharedObject對象函數

step 3: 監聽SharedObject對象的SyncEvent.SYNC事件,並sharedObj.connect(netConnection)flex

step 4: 這個時候能夠利用sharedObj.send()向服務器發送消息,而後服務器再把登陸消息廣播給當前鏈接的全部客戶端this

step 5: 在SyncEvent.SYNC事件裏能夠取到sharedObj.data裏的數據了spa

step 6: 定義一個供服務器調用後獲得返回值的函數,須要聲明爲public.net

PS:code

服務器端和客戶端,都是在操做SharedObject對象,客戶端是讀取,服務器端是寫入(只有服務器執行寫入才能保證數據無誤)xml

服務器端發送消息給每一個客戶端,分爲客戶端請求和服務器端請求實現對象

* 客戶端請求通常是調用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>
相關文章
相關標籤/搜索