Avg. Rating 2.7

Problem

The new Flash Player 10.1 NetGroup class has a post() method that lets you send a message to all members of a group. Has anyone built a simple chat app? You'll need the following: 1. http://help.adobe.com/en_US/FlashPlatform/beta/reference/actionscript/3/ 2. http://labs.adobe.com/technologies/flashplayer10/ 3. http://labs.adobe.com/technologies/stratus/ OR http://www.adobe.com/products/livecycle/collaborationservice/

Solution

More info about the NetConnection, NetGroup, NetStream and GroupSpecifier can be found on http://milkreations.com/?p=98

Detailed explanation

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

<mx:Script>

 <![CDATA[       

  private var netConnection:NetConnection;

  private var netStream:NetStream;

  private var netGroup:NetGroup;

  private var sequenceNumber:uint = 0;   

  private const SERVER:String = "rtmfp://stratus.adobe.com/";

  private const DEVKEY:String = "your developer key";

  private const GROUP_PREFIX:String = "com.maximgladines.p2p";

 

  // Bindable variables are accessible by the flexframework

  [Bindable] private var connected:Boolean = false;

  [Bindable] private var joinedGroup:Boolean = false;

 

   private function doConnect():void

   {

    trace("Connecting to \"" + SERVER + "\" ...");

    // Creating a new NetConnection

 netConnection = new NetConnection();

 netConnection.addEventListener(NetStatusEvent.NET_STATUS, NetStatusHandler);

 // Connecting to the Server with my DeveloperKey

netConnection.connect(SERVER + DEVKEY);

 }

 

  private function doDisconnect():void

 {

  if(netConnection) netConnection.close();

 }

 

 private function onConnect():void

 {

  trace("Connected");

 // Setting connect to true, to disable the connectbutton and

// enable the disconnectbutton

connected = true;

 // Settingup the GroupSpecifier

 var gs:GroupSpecifier;

 gs = new GroupSpecifier(GROUP_PREFIX + txtGroup.text);

 gs.multicastEnabled = true;

 gs.postingEnabled = true;

 gs.serverChannelEnabled = true;

// Creating the NetGroup with the GroupSpecifier settings

netGroup = new NetGroup(netConnection, gs.groupspecWithAuthorizations());

netGroup.addEventListener(NetStatusEvent.NET_STATUS, NetStatusHandler);

// Creating a new NetStream to the NetGroup netStream = new NetStream(netConnection, gs.groupspecWithAuthorizations());

netStream.addEventListener(NetStatusEvent.NET_STATUS, NetStatusHandler);

txtMessages.text +="Joining "+txtGroup.text +"\n"+ "groupID: " + gs.groupspecWithAuthorizations() +"\n";

 }

private function onDisonnect():void

 {

 //Reseting all net-variabes

 txtMessages.text += "Disconnected\n";

 netConnection = null;

 netStream = null;

 netGroup = null;

 connected = false;

 joinedGroup = false;

}  

 

 private function onNetStreamConnect():void

{

 // Setting the client to the current application

 netStream.client = this;

}

 


 private function onNetGroupConnect():void

 {

// Setting joinedGroup true, to enable the sendbutton

 joinedGroup = true;

  // If the user hasn't  given a username create and random one

 if(txtNick.text=="")txtNick.text = "user-"+Math.round(Math.random()*1000);

txtMessages.text +="Connected as "+txtNick.text+"\n";

 }


 private function doPost():void

{

 // Creating a new Message Object

 var message:Object = new Object;

 // Give the Nickname to the message-object

  message.user = txtNick.text;

 // Give the Message to the message-object

message.text = txtSendMessage.text;

 // Add the sequenceNumber so that every message is unique

 message.sequence = sequenceNumber++;

// Set the sender

 message.sender = netConnection.nearID;

 // The NetGroup will post this message to every member in the group

 netGroup.post(message);

 // Add your message to your own messagesWindow

 txtMessages.text += "==> " + message.text + "\n";

 // Clear the messageInput you've just sent

 txtSendMessage.text = "";

 }


 private function onPosting(message:Object):void

{

// If there is an incomming notify (post)

// Add the post to your messagesWindow

 txtMessages.text += String("<" + message.user + "> " + message.text + "\n");

 }


private function NetStatusHandler(e:NetStatusEvent):void

 {

 switch(e.info.code)

{

 case "NetConnection.Connect.Success":

// Connection succes to Server

 onConnect()

 break;

  case "NetConnection.Connect.Closed":

// Connection closed

 onDisonnect();

break;

case "NetStream.Connect.Success":

 // NetStream connected to the server or group or peerID

 onNetStreamConnect();

 break;

  case "NetStream.Connect.Failed":

 // FAIL to server or group or peerID

doDisconnect();

break;

case "NetGroup.Connect.Success":

 // Connecting to the NetGroup was a success!

 onNetGroupConnect()

 break;

case "NetGroup.Connect.Failed":

 // Again FAIL to NetGroup

 doConnect();

 break;

 case "NetGroup.Posting.Notify":

  // If there is an incomming message

  onPosting(e.info.message);

 break;

 default:

break;

 }

 }

 ]]>

 

 </mx:Script>

  <mx:VBox x="10" y="10" width="693.5" height="282" horizontalAlign="right">

   <mx:VBox width="690" height="80" verticalAlign="middle" horizontalAlign="right">

    <mx:HBox width="688.5" height="34" verticalAlign="middle" horizontalAlign="left">

     <mx:Label text="Nick:" width="54.5" textAlign="right" height="18"/>

     <mx:TextInput id="txtNick" width="165"/>

    </mx:HBox>

    <mx:HBox width="688" height="33" horizontalAlign="right" verticalAlign="middle">

     <mx:Label text="Group:"/>

     <mx:TextInput width="449" id="txtGroup"/>

     <mx:Button label="Connect" id="btnConnect" enabled="{!joinedGroup}" click="doConnect()"/>

     <mx:Button label="Disconnect" id="btnDisconnect" enabled="{joinedGroup}" click="doDisconnect()"/>

    </mx:HBox>

   </mx:VBox>

   <mx:HBox width="688" horizontalAlign="right">

    <mx:TextArea width="625" height="143" id="txtMessages" editable="false"/>

   </mx:HBox>

   <mx:HBox width="686" height="37" verticalAlign="middle" horizontalAlign="right">

    <mx:TextInput width="562" id="txtSendMessage"/>

    <mx:Button label="Send" id="btnSend" click="doPost()" enabled="{connected}"/>

   </mx:HBox>

  </mx:VBox>

 </mx:Application>


+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes