基于NetGroup的P2P多人聊天室
来源:互联网 发布:点击开票软件没反应 编辑:程序博客网 时间:2024/06/08 02:04
原址:
http://swfever.com/?p=956
在Stratus Beta2上线后,有很多朋友表示不知道如何基于NetGroup进行开发。事实上,NetGroup是一个非常强大的类,可以实现诸如Direct Routing, Posting和Object Replication等功能。为此,我花了20分钟时间写了一个简单的Demo:基于NetGroup的P2P多人聊天室,其中主要使用了Posting功能。其他NetGroup功能的使用方法大同小异,请各位自行发掘。
要运行这个Demo,你需要一个Stratus Developer Key,并确保你的Flash Player版本为10.1。
代码:
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" width="600" height="400"> <fx:Script> .... </fx:Script> <s:TextInput id="developerKeyInput" x="10" y="10" width="304" text="@{developerKey}"/> <s:TextInput id="yourNameInput" x="320" y="10" width="192" text="@{yourName}"/> <s:Button id="connectBtn" x="520" y="10" label="Connect" click="connectBtn_onClick()" /> <s:TextArea text="{logText}" left="10" right="10" bottom="10" editable="false"/> <s:TextArea text="{chatMsg}" width="372" x="218" y="56" height="129" editable="false"/> <s:Label left="10" bottom="170" text="Log:"/> <s:Label left="10" bottom="350" text="Neighbor:"/> <s:Label left="218" bottom="350" text="Chat:"/> <s:List id="userList" x="10" y="57" width="200" height="153"> <s:ArrayCollection id="userList_ac" /> </s:List> <s:Button x="520" y="189" label="Send" id="sendButton" click="sendBtn_onClick()" enabled="false"/> <s:TextInput x="218" y="188" width="294" id="chatMsgInput"/> </s:Application> Script标签内的代码如下: import mx.utils.StringUtil; [Bindable] private var developerKey:String = "Your Developer Key"; [Bindable] private var yourName:String = "no name"; private const SERVER_URL:String = "rtmfp://stratus.adobe.com/"; [Bindable] private var logText:String = ""; [Bindable] private var chatMsg:String = ""; private var netConn:NetConnection; private var netGroup:NetGroup; private function connectBtn_onClick():void { if(!netConn) { netConn = new NetConnection(); netConn.addEventListener(NetStatusEvent.NET_STATUS, netConn_onNetStatus); } netConn.connect(SERVER_URL+developerKey); } private function sendBtn_onClick():void { if(StringUtil.trim(chatMsgInput.text).length) { var msg:Object = { text: chatMsgInput.text, sender: yourName }; netGroup.post(msg); msgRecieved(msg); chatMsgInput.text = ""; } } private function netConn_onNetStatus(event:NetStatusEvent):void { log(event.info.code); if(event.info.code == "NetConnection.Connect.Success") { connectBtn.enabled = false; developerKeyInput.enabled = false; yourNameInput.enabled = false; log("My Peer Id = "+netConn.nearID); var groupSpec:GroupSpecifier = new GroupSpecifier("swfever"); groupSpec.serverChannelEnabled = true; groupSpec.ipMulticastMemberUpdatesEnabled = true; groupSpec.multicastEnabled = true; groupSpec.postingEnabled = true; var groupId:String = groupSpec.groupspecWithAuthorizations(); log("Group Id = "+groupId); netGroup = new NetGroup(netConn,groupId); netGroup.addEventListener(NetStatusEvent.NET_STATUS, netGroup_onNetStatus); } else if(event.info.code == "NetGroup.Connect.Success") { sendButton.enabled = true; } } private function netGroup_onNetStatus(event:NetStatusEvent):void { log(event.info.code); if(event.info.code == "NetGroup.Neighbor.Connect") { log("NetGroup Neighbor Count = "+netGroup.neighborCount); userList_ac.addItem(event.info.neighbor); } else if(event.info.code == "NetGroup.Neighbor.Disconnect") { log("NetGroup Neighbor Count = "+netGroup.neighborCount); userList_ac.removeItemAt(userList_ac.getItemIndex(event.info.neighbor)); } else if(event.info.code == "NetGroup.Posting.Notify") { msgRecieved(event.info.message); } } private function log(msg:String):void { logText+=msg+"/n"; } private function msgRecieved(msg:Object):void { chatMsg+=msg.sender+" : "+msg.text+"/n"; } 源代码下载 NetGroup_Posting_Demo_src (1.4 KiB, 56 hits)
- 基于NetGroup的P2P多人聊天室
- flashplayer10.1 p2p NetGroup 简单的聊天示例
- 多人聊天室程序(P2P)C#版
- 基于websocket的多人页面聊天室
- 基于ServerSocket的多人聊天室模拟实现
- 多人聊天室(基于Linux的SOCKET UDP编程)
- 基于Socket的聊天室
- 基于workerman的聊天室
- 基于P2P的聊天
- 编写基于PHP的聊天室
- 基于socket的聊天室服务器端
- 基于B/S的聊天室
- Android 基于Socket的聊天室
- 基于Socket通信的聊天室
- android 基于socket的聊天室
- Android 基于Socket的聊天室
- 基于UDP的局域网聊天室
- 基于epoll的聊天室程序
- c++读取文件-20100127
- jquery 插件
- Linux字符集的修改方法
- 做好测试计划和测试用例的工作的关键 (转)
- Windows平台上的Apache+Tomcat配置指南
- 基于NetGroup的P2P多人聊天室
- 如何安裝/ 移除 .wsp Feature
- System.Diagnostics.Process
- SQL Server根据查询结果,生成XML文件
- Luhn algorithm(附信用卡校验算法C语言实现)
- Windows7下IIS7的安装及ASP配置方法
- 应聘华为 16道经典面试题
- SMTP的基本命令
- ie8 相关