1 Reply Latest reply on Jul 21, 2013 8:25 AM by fB3

    NetStream : send text data

    bro577569

      hi,

       

      I try to do a really simple chat with NetStream. I would like to send a text on my netstream (I don't want to use camera, only text !)

       

      I run a red5 server and I use flex 4.5

       

      As I my new, I don't understand what is the best way to do this. I try code to send video streaming and it works but I can't send text.

       

      Is somebody have a really simple example to do this ?

        • 1. Re: NetStream : send text data
          fB3 Level 2

          //Simple solution,

          import flash.events.*

           

          public var chat_so:SharedObject;

          public var nc:NetConnection;

          public var username:String;

           

          public function createConnection():void

          {

              nc = new NetConnection();

              nc.objectEncoding = ObjectEncoding.AMF0;

              nc.addEventListener( NetStatusEvent.NET_STATUS, netStatusHandler );

              nc.connect( "rtmp://path_to_red5/oflaDemo",username);

          };

           

          //Then in the netStatusHandler you would need something like this....

          public function netStatusHandler( event:NetStatusEvent ):void

          {       

              switch( event.info.code )

               {

                  case "NetConnection.Connect.Success":

                       connectComponents()

                  break;

                  case "NetConnection.Connect.Rejected":

                  break;                   

              }

           

          };

           

          //This sets up a SyncEvent handler...

          public function connectComponents():void

          {

              SharedObject.defaultObjectEncoding  = flash.net.ObjectEncoding.AMF0;

              chat_so = SharedObject.getRemote("chat_so", nc.uri, false);

              chat_so.addEventListener( SyncEvent.SYNC, usersSyncHandler_chat );

              chat_so.connect( nc );

             

          };

           

          //This fires when the sharedObject is updated...

          public function usersSyncHandler_chat( event:SyncEvent ):void

          {   

                  var results:Object = event.target.data;

           

                  for( var a:String in results )

              {  

                  if (  results[ a ] != undefined )

                  {

                       //this will add a message to a text component...

                      chatCanvas.htmlText += "" + results[ a ];

                      chatCanvas.validateNow();

                      chatCanvas.verticalScrollPosition = chatCanvas.maxVerticalScrollPosition;

                  }

              }

          };

           

           

          //this button or key listener function will send a message by updating a sharedObject on all clients...

          public function sendMessage():void

          {

                //grab message from an input text component.

                var msg:String = chatInputTxt.text;

                //send the msg...

                chat_so.setProperty("textValue", username + ": " + msg );

           

                //clear text from chatInputTxt.text

                chatInputTxt.text = "";

          }

           

           

           

           

           

          HTH