原址:
在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)