Return to Snippet

Revision: 34701
at October 27, 2010 07:48 by chrisaiv


Initial Code
<?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" 
	xmlns:mx="library://ns.adobe.com/flex/mx"
	width="100%" height="100%"
	applicationComplete="OnApplicationComplete()">
	
	<fx:Script>
		<![CDATA[
			private var netConnection:NetConnection = null;
			private var netGroup:NetGroup = null;
			private var sequenceNumber:uint = 0;
			
			private const SERVER:String = "rtmfp://p2p.local/MAX2010-8fb08f87e701/";
			
			[Bindable] private var joined:Boolean = false;
			
			private function OnApplicationComplete():void
			{
				DoConnect();
			}
			
			private function Transcript(msg:Object):void
			{
				transcript.appendText(msg.toString());
			}
			
			private function NetStatusHandler(e:NetStatusEvent):void
			{
				switch(e.info.code)
				{
					case "NetConnection.Connect.Success":
						//
						OnConnect();
						break;
					
					case "NetConnection.Connect.Closed":
					case "NetConnection.Connect.Failed":
					case "NetConnection.Connect.Rejected":
					case "NetConnection.Connect.AppShutdown":
					case "NetConnection.Connect.InvalidApp":
					case "NetGroup.Connect.Rejected": // e.info.group
					case "NetGroup.Connect.Failed": // e.info.group
						OnDisconnect();
						break;
					
					case "NetGroup.Connect.Success": // e.info.group
						//The allowed choice was selected on the peer assisten network dialog
						OnJoin();
						break;
					//The Message property of the EventsInfo object has the message
					case "NetGroup.Posting.Notify": // e.info.message, e.info.messageID
						OnPosting( e.info.message );
						break;
					
					case "NetGroup.Neighbor.Connect": // e.info.neighbor
					case "NetGroup.Neighbor.Disconnect": // e.info.neighbor
						DoUpdateStatus();
						break;
					
					case "NetStream.Connect.Success": // e.info.stream
					case "NetStream.Connect.Rejected": // e.info.stream
					case "NetStream.Connect.Failed": // e.info.stream
					case "NetGroup.SendTo.Notify": // e.info.message, e.info.from, e.info.fromLocal
					case "NetGroup.LocalCoverage.Notify": //
					case "NetGroup.MulticastStream.PublishNotify": // e.info.name
					case "NetGroup.MulticastStream.UnpublishNotify": // e.info.name
					case "NetGroup.Replication.Fetch.SendNotify": // e.info.index
					case "NetGroup.Replication.Fetch.Failed": // e.info.index
					case "NetGroup.Replication.Fetch.Result": // e.info.index, e.info.object
					case "NetGroup.Replication.Request": // e.info.index, e.info.requestID
						
					default:
						break;
				}
			}
			
			private function DoConnect():void
			{
				Transcript("Connecting to \"" + SERVER + "\" ...\n");
				netConnection = new NetConnection();
				netConnection.addEventListener( NetStatusEvent.NET_STATUS, NetStatusHandler, false, 0, true );
				netConnection.connect( SERVER );
			}
			
			private function OnConnect():void
			{
				Transcript("Connected\n");
				var gs:GroupSpecifier = new GroupSpecifier( "com.adobe.max.chat" );				
					//Posting must be enabled for peer to peer chat
					gs.postingEnabled = true;
					//When the group is connected, ask for help to get the bootrap all set up
					gs.serverChannelEnabled = true;
					//NetGroup is very similar to NetStream
					netGroup = new NetGroup( netConnection, gs.groupspecWithAuthorizations() );
					netGroup.addEventListener(NetStatusEvent.NET_STATUS, NetStatusHandler );
					Transcript( "Joining Group :" + gs.groupspecWithAuthorizations() + "...\n" );
			}
			
			private function OnJoin():void
			{
				Transcript("Joined.  Welcome to simplechat.\n");
				joined = true;
				DoUpdateStatus();
			}
			
			private function OnDisconnect():void
			{
				Transcript("Disconnected\n");
				netConnection = null;
				joined = false;
			}
			
			private function OnTextInput():void
			{
				//Show something when entered in UI
				var message:Object = new Object();
					message.text = inputLine.text;
					
					message.username = username.text;
					
					//This is a big bad unique number
					message.sender = netConnection.nearID;
					//This will make it unique
					message.sequence = sequenceNumber++;
					
					
						
				Transcript("==> " + inputLine.text + "\n");
				inputLine.text = "";
			}
			
			private function OnPosting(message:Object):void
			{
				Transcript( "<" + message.username + ">" + message.text + "\n" );
			}
			
			private function DoUpdateStatus():void
			{
				statusLine.text = "Neighbors: " + netGroup.neighborCount + " estimated size: " + netGroup.estimatedMemberCount;
			}
		]]>
	</fx:Script>
	
	<s:VGroup width="100%" height="100%" top="10" left="10" bottom="10" right="10" gap="6">
		<s:Label width="100%" id="statusLine"/>
		<s:TextArea width="100%" height="100%" id="transcript"/>
		<s:HGroup width="100%">
			<s:TextInput width="150" id="username" text="username123" />
			<s:TextInput width="100%" id="inputLine" enter="OnTextInput()" enabled="{joined}"/>
		</s:HGroup>
	</s:VGroup>
	
</s:Application>

Initial URL


Initial Description


Initial Title
Flex: Simple Peer-to-Peer Chat

Initial Tags
Flex

Initial Language
MXML