[AS3]as3下p2p NetGroup聊天代码示例(rtmfp协议示例)(3)
第六步:建立界面 s:TextArea left = 10 right = 10 top = 10 bottom = 40 id = txtHistory / s:TextInput x = 10 id = txtUser text = {user} bottom = 10 / s:TextInput left = 145 right = 88 id = txtMessage b
第六步:建立界面
- <s:TextArea left="10" right="10" top="10" bottom="40" id="txtHistory"/>
- <s:TextInput x="10" id="txtUser" text="{user}" bottom="10"/>
- <s:TextInput left="145" right="88" id="txtMessage" bottom="10" enter="sendMessage()"/>
- <s:Button label="Send" click="sendMessage()" enabled="{connected}" bottom="10" right="10"/>
- 以下是完整的代码:
- <fx:Script>
- <![CDATA[
- private const SERVER:String = "rtmfp://p2p.rtmfp.net/";
- private const DEVKEY:String = "d8c6d5b18ddf65bf1529fd4c-c98c749416ce";
- private var nc:NetConnection;
- private var netGroup:NetGroup;
- [Bindable]
- private var connected:Boolean = false;
- [Bindable]
- private var user:String;
- private function connect():void{
- nc = new NetConnection();
- nc.addEventListener(NetStatusEvent.NET_STATUS,netStatus);
- nc.connect(SERVER+DEVKEY);
- }
- internal function netStatus(evt:NetStatusEvent):void{
- switch(evt.info.code){
- case "NetConnection.Connect.Success":
- setupGroup();
- break;
- case "NetGroup.Connect.Success":
- connected = true;
- break;
- case "NetGroup.Posting.Notify":
- receiveMessage(evt.info.message);
- break;
- }
- }
- private function setupGroup():void{
- var gorupspec:GroupSpecifier = new GroupSpecifier("myGroup/g1");
- gorupspec.serverChannelEnabled = true;
- gorupspec.postingEnabled = true;
- netGroup = new NetGroup(nc,gorupspec.groupspecWithAuthorizations());
- netGroup.addEventListener(NetStatusEvent.NET_STATUS,netStatus);
- user = "user"+Math.round(Math.random() * 1000);
- }
- private function sendMessage():void{
- var message:Object = new Object();
- message.sender = netGroup.convertPeerIDToGroupAddress(nc.nearID);
- message.user = txtUser.text;
- message.text = txtMessage.text;
- netGroup.post(message);
- receiveMessage(message);
- txtMessage.text = "";
- }
- internal function receiveMessage(message:Object):void{
- write(message.user+": "+message.text);
- }
- private function write(txt:String):void{
- txtHistory.text += txt+"\n";
- }
- ]]>
- </fx:Script>
热门文章推荐
- [HLS]做自己的m3u8点播系统使用HTTP Live Streaming(HLS技术)
- [FMS]FMS流媒体服务器配置与使用相关的介绍
- [AS3]什么是M3U8,与HTML5的区别是什么
- AS2.0 让flash自适应全屏,并且不自动缩放
- [AS3]as3.0的sound类常用技巧整理
- [AS3]as3与ByteArray详解、ByteArray介绍、ByteArray用法
- 关于RTMP,RTMPT,RTMPS,RTMPE,RTMPTE协议的介绍
- [JS]分享浏览器弹出窗口不被拦截JS示例
请稍候...