Flex: Simple Peer-to-Peer Chat


/ Published in: MXML
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <s:Application
  3. xmlns:fx="http://ns.adobe.com/mxml/2009"
  4. xmlns:s="library://ns.adobe.com/flex/spark"
  5. xmlns:mx="library://ns.adobe.com/flex/mx"
  6. width="100%" height="100%"
  7. applicationComplete="OnApplicationComplete()">
  8.  
  9. <fx:Script>
  10. <![CDATA[
  11. private var netConnection:NetConnection = null;
  12. private var netGroup:NetGroup = null;
  13. private var sequenceNumber:uint = 0;
  14.  
  15. private const SERVER:String = "rtmfp://p2p.local/MAX2010-8fb08f87e701/";
  16.  
  17. [Bindable] private var joined:Boolean = false;
  18.  
  19. private function OnApplicationComplete():void
  20. {
  21. DoConnect();
  22. }
  23.  
  24. private function Transcript(msg:Object):void
  25. {
  26. transcript.appendText(msg.toString());
  27. }
  28.  
  29. private function NetStatusHandler(e:NetStatusEvent):void
  30. {
  31. switch(e.info.code)
  32. {
  33. case "NetConnection.Connect.Success":
  34. //
  35. OnConnect();
  36. break;
  37.  
  38. case "NetConnection.Connect.Closed":
  39. case "NetConnection.Connect.Failed":
  40. case "NetConnection.Connect.Rejected":
  41. case "NetConnection.Connect.AppShutdown":
  42. case "NetConnection.Connect.InvalidApp":
  43. case "NetGroup.Connect.Rejected": // e.info.group
  44. case "NetGroup.Connect.Failed": // e.info.group
  45. OnDisconnect();
  46. break;
  47.  
  48. case "NetGroup.Connect.Success": // e.info.group
  49. //The allowed choice was selected on the peer assisten network dialog
  50. OnJoin();
  51. break;
  52. //The Message property of the EventsInfo object has the message
  53. case "NetGroup.Posting.Notify": // e.info.message, e.info.messageID
  54. OnPosting( e.info.message );
  55. break;
  56.  
  57. case "NetGroup.Neighbor.Connect": // e.info.neighbor
  58. case "NetGroup.Neighbor.Disconnect": // e.info.neighbor
  59. DoUpdateStatus();
  60. break;
  61.  
  62. case "NetStream.Connect.Success": // e.info.stream
  63. case "NetStream.Connect.Rejected": // e.info.stream
  64. case "NetStream.Connect.Failed": // e.info.stream
  65. case "NetGroup.SendTo.Notify": // e.info.message, e.info.from, e.info.fromLocal
  66. case "NetGroup.LocalCoverage.Notify": //
  67. case "NetGroup.MulticastStream.PublishNotify": // e.info.name
  68. case "NetGroup.MulticastStream.UnpublishNotify": // e.info.name
  69. case "NetGroup.Replication.Fetch.SendNotify": // e.info.index
  70. case "NetGroup.Replication.Fetch.Failed": // e.info.index
  71. case "NetGroup.Replication.Fetch.Result": // e.info.index, e.info.object
  72. case "NetGroup.Replication.Request": // e.info.index, e.info.requestID
  73.  
  74. default:
  75. break;
  76. }
  77. }
  78.  
  79. private function DoConnect():void
  80. {
  81. Transcript("Connecting to \"" + SERVER + "\" ...\n");
  82. netConnection = new NetConnection();
  83. netConnection.addEventListener( NetStatusEvent.NET_STATUS, NetStatusHandler, false, 0, true );
  84. netConnection.connect( SERVER );
  85. }
  86.  
  87. private function OnConnect():void
  88. {
  89. Transcript("Connected\n");
  90. var gs:GroupSpecifier = new GroupSpecifier( "com.adobe.max.chat" );
  91. //Posting must be enabled for peer to peer chat
  92. gs.postingEnabled = true;
  93. //When the group is connected, ask for help to get the bootrap all set up
  94. gs.serverChannelEnabled = true;
  95. //NetGroup is very similar to NetStream
  96. netGroup = new NetGroup( netConnection, gs.groupspecWithAuthorizations() );
  97. netGroup.addEventListener(NetStatusEvent.NET_STATUS, NetStatusHandler );
  98. Transcript( "Joining Group :" + gs.groupspecWithAuthorizations() + "...\n" );
  99. }
  100.  
  101. private function OnJoin():void
  102. {
  103. Transcript("Joined. Welcome to simplechat.\n");
  104. joined = true;
  105. DoUpdateStatus();
  106. }
  107.  
  108. private function OnDisconnect():void
  109. {
  110. Transcript("Disconnected\n");
  111. netConnection = null;
  112. joined = false;
  113. }
  114.  
  115. private function OnTextInput():void
  116. {
  117. //Show something when entered in UI
  118. var message:Object = new Object();
  119. message.text = inputLine.text;
  120.  
  121. message.username = username.text;
  122.  
  123. //This is a big bad unique number
  124. message.sender = netConnection.nearID;
  125. //This will make it unique
  126. message.sequence = sequenceNumber++;
  127.  
  128.  
  129.  
  130. Transcript("==> " + inputLine.text + "\n");
  131. inputLine.text = "";
  132. }
  133.  
  134. private function OnPosting(message:Object):void
  135. {
  136. Transcript( "<" + message.username + ">" + message.text + "\n" );
  137. }
  138.  
  139. private function DoUpdateStatus():void
  140. {
  141. statusLine.text = "Neighbors: " + netGroup.neighborCount + " estimated size: " + netGroup.estimatedMemberCount;
  142. }
  143. ]]>
  144. </fx:Script>
  145.  
  146. <s:VGroup width="100%" height="100%" top="10" left="10" bottom="10" right="10" gap="6">
  147. <s:Label width="100%" id="statusLine"/>
  148. <s:TextArea width="100%" height="100%" id="transcript"/>
  149. <s:HGroup width="100%">
  150. <s:TextInput width="150" id="username" text="username123" />
  151. <s:TextInput width="100%" id="inputLine" enter="OnTextInput()" enabled="{joined}"/>
  152. </s:HGroup>
  153. </s:VGroup>
  154.  
  155. </s:Application>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.