首页 技术 正文
技术 2022年11月18日
0 收藏 947 点赞 4,653 浏览 3650 个字

实现的一般步骤是:

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>
上一篇: ProgressBarLayoutView
下一篇: 关于View端
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295