Skip navigation
bro577569
Currently Being Moderated

NetStream : send text data

Jul 19, 2013 2:24 PM

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 ?

 
Replies
  • Currently Being Moderated
    Jul 21, 2013 8:25 AM   in reply to bro577569

    //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

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points