0 Replies Latest reply on Feb 28, 2011 7:10 AM by Skarbas

    Problem making 2 separate message sending in one app

    Skarbas

      I have made a simple application which should be a chat and some other text sending, but not to chat window.

      I have added netGroup.removeEventListener and added another EventListener with another handler, but still my messsage when submitting (not chatting) goes to chat window.

      Maybe somebody can tell why?

      What am I doing wrong?

      Already 2 weeks on the same spot!

      Tried the netStream.publish, but still with no luck.

      And I cannot find any examples on web.

      Any help would be appreciated.

       

       

      <?xml version="1.0" encoding="utf-8"?>

      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="100%" height="100%" applicationComplete="OnApplicationComplete()">

      <mx:Script>

      <![CDATA[

      private var netConnection:NetConnection = null;

      private var netStream:NetStream = null;

      private var netGroup:NetGroup = null;

      private var video:Video = null;

      private var sequenceNumber:uint = 0;

      private var resizeTimer:Timer = null;

      private var sendStream:NetStream;

      private var recvStream:NetStream;

       

       

      private const SERVER:String = "rtmfp://p2p.rtmfp.net/";

       

      [Bindable] private var connected:Boolean = false;

      [Bindable] private var joinedGroup:Boolean = false;

       

      private function OnApplicationComplete():void

      {

      userName.text = "user " + int(Math.random() * 65536);

       

      groupName.text = "channel" + (int(Math.random() * 899) + 101);

       

      }

       

      private function StatusMessage(msg:Object):void

      {

      statusLog.text += msg;

      statusLog.verticalScrollPosition = statusLog.textHeight;

      statusLog.validateNow();

      }

      public function NetStatusHandler2(event:NetStatusEvent):void{

       

      switch(event.info.code)

      {

       

      case "NetGroup.Posting.Notify": // e.info.message, e.info.messageID

      postStream(event.info.score);

      break;

      }

      }

      private function NetStatusHandler(e:NetStatusEvent):void

      {

      StatusMessage(e.info.code);

      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":

      OnDisconnect();

      break;

       

      case "NetStream.Connect.Success": // e.info.stream

      OnNetStreamConnect();

      break;

       

      case "NetStream.Connect.Rejected": // e.info.stream

      case "NetStream.Connect.Failed": // e.info.stream

      DoDisconnect();

      break;

       

      case "NetGroup.Connect.Success": // e.info.group

      OnNetGroupConnect();

      break;

       

      case "NetGroup.Connect.Rejected": // e.info.group

      case "NetGroup.Connect.Failed": // e.info.group

      DoDisconnect();

      break;

       

      case "NetGroup.Posting.Notify": // e.info.message, e.info.messageID

      OnPosting(e.info.message);

      break;

       

       

      case "NetStream.MulticastStream.Reset":

      case "NetStream.Buffer.Full":

      case "NetGroup.SendTo.Notify": // e.info.message, e.info.from, e.info.fromLocal

      case "NetGroup.LocalCoverage.Notify": //

      case "NetGroup.Neighbor.Connect": // e.info.neighbor

      case "NetGroup.Neighbor.Disconnect": // e.info.neighbor

      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

      {

      StatusMessage("Connecting to \"" + SERVER + "\" ...\n");

      netConnection = new NetConnection();

      netConnection.addEventListener(NetStatusEvent.NET_STATUS, NetStatusHandler);

      netConnection.connect(SERVER);

      }

       

      private function OnConnect():void

      {

      var groupSpecifier:GroupSpecifier;

       

      StatusMessage("Connected\n");

      connected = true;

       

      groupSpecifier = new GroupSpecifier("max2009lab/" + groupName.text);

      groupSpecifier.multicastEnabled = true;

      groupSpecifier.postingEnabled = true;

      groupSpecifier.serverChannelEnabled = true;

       

      netStream = new NetStream(netConnection, groupSpecifier.groupspecWithAuthorizations());

      netStream.addEventListener(NetStatusEvent.NET_STATUS, NetStatusHandler);

       

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

      netGroup.addEventListener(NetStatusEvent.NET_STATUS, NetStatusHandler);

       

       

      StatusMessage("Join \"" + groupSpecifier.groupspecWithAuthorizations() + "\"\n");

       

      }

       

      private function OnNetStreamConnect():void

      {

      netStream.client = this;

       

      }

       

      private function OnNetGroupConnect():void

      {

      joinedGroup = true;

      }

       

      private function DoDisconnect():void

      {

      if(netConnection)

      netConnection.close();

       

      }

       

      private function OnDisconnect():void

      {

      StatusMessage("Disconnected\n");

      netConnection = null;

      netStream = null;

      netGroup = null;

      connected = false;

      joinedGroup = false;

      }

       

      private function ClearChatText():void

      {

      chatText.text = "";

      }

       

      private function DoPost():void

      {

       

      var message:Object = new Object;

       

      message.user = userName.text;

      message.text = chatText.text;

      message.sequence = sequenceNumber++;

      message.sender = netConnection.nearID;

       

      netGroup.post(message);

       

      StatusMessage("==> " + chatText.text + "\n");

       

      chatText.callLater(ClearChatText);

      }

       

      private function OnPosting(message:Object):void

      {

      StatusMessage("<" + message.user + "> " + message.text + "\n");

      }

       

      public function doSubmit(): void {

      netGroup.removeEventListener(NetStatusEvent.NET_STATUS, NetStatusHandler);

      netGroup.addEventListener(NetStatusEvent.NET_STATUS, NetStatusHandler2);

      var message2:Object = new Object;

       

      message2.user = userName.text;

      message2.text = textInput.text;

      message2.sequence = sequenceNumber++;

      message2.sender = netConnection.nearID;

       

      netGroup.post(message2);

       

       

       

      }

      public function postStream(message2:Object):void {

       

      remoteText.text += message2.text + "\n"

      netGroup.removeEventListener(NetStatusEvent.NET_STATUS, NetStatusHandler2);

      }

       

       

      public function onPlayStatus(info:Object):void {}

      public function onMetaData(info:Object):void {}

      public function onCuePoint(info:Object):void {}

      public function onTextData(info:Object):void {}

       

      public function ValidateConnectAllowed(isConnected:Boolean, groupNameText:String):Boolean

      {

      return (!isConnected) && (groupNameText.length > 0);

      }

      ]]>

      </mx:Script>

       

      <mx:VBox top="10" right="10" left="10" bottom="10" verticalGap="6">

      <mx:HBox width="100%">

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

      <mx:TextInput id="groupName" width="100%" text="default" enabled="{!connected}"/>

      <mx:Button label="Connect" click="DoConnect()" enabled="{ValidateConnectAllowed(connected, groupName.text)}" />

      <mx:Button label="Disconnect" click="DoDisconnect()" enabled="{connected}" />

      </mx:HBox>

      <mx:HBox width="100%" horizontalAlign="center" height="100">

      <mx:TextArea id="ownText" height="92"/>

      <mx:TextArea id="remoteText" height="92"/>

      </mx:HBox>

      <mx:VBox height="54" width="651" horizontalAlign="center">

      <mx:TextArea id="textInput" height="22" enabled="{joinedGroup}" />

      <mx:Button label="Submit" click="doSubmit()" />

      </mx:VBox>

      <mx:HBox width="100%">

      <mx:TextInput id="userName" width="160" />

      <mx:TextInput id="chatText" width="405" enabled="{joinedGroup}" />

      <mx:Button label="Chat" click="DoPost()"/>

      </mx:HBox>

      <mx:TextArea id="statusLog" width="100%" height="93"/>

      </mx:VBox>

      </mx:Application>