Skip to main content
daslicht
Known Participant
December 13, 2010
Question

Simple Text Chat without ServerSide code

  • December 13, 2010
  • 1 reply
  • 1478 views

Hi,

I just created a simple example of a text chat.

But it is only working In one way ?

The first instance I open can send to all other instances but the other instances cant send at all?

What am I missing?

<?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" minWidth="955"                   minHeight="600"                  creationComplete="application1_creationCompleteHandler(event)"                   >      <fx:Script>           <![CDATA[                import mx.events.FlexEvent;                [Bindable] private var nc:NetConnection;                [Bindable] private var ns_out:NetStream;                [Bindable] private var ns_in:NetStream;                private var clientA:Object;                                private var clientB:Object;                                                protected function button1_clickHandler(event:MouseEvent):void                {                                     }                                protected function button2_clickHandler(event:MouseEvent):void                {                     var o:Object = new Object();                          o.name = name_input.text;                          o.message = message_input.text;                                          ns_out.send("@setDataFrame", "onMetaData", o );                }                protected function application1_creationCompleteHandler(event:FlexEvent):void                {                     //connect to the Flash Media Server                     nc = new NetConnection();                     nc.client = this;                     nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);                     nc.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);                     nc.objectEncoding = ObjectEncoding.AMF0;                     nc.connect("rtmp://fmsserverURL.com/live/");                                     }                                public function onBWDone():void{                }                                                private function netStatusHandler(event:NetStatusEvent):void                {                     trace('net status '+event.info.code);                                          if(event.info.code == "NetConnection.Connect.Success")                     {                          trace("connected", nc.connected);                          ns_out = new NetStream(nc);                          ns_in = new NetStream(nc);                                                    clientA = new Object();                          clientA.onMetaData = onMetaData_handler;                          clientA.onCuePoint = onCuePoint_handler                          clientA.onBWDone = onBWDone;                                                    clientB = new Object();                          clientB.onMetaData = onMetaData_handler;                          clientB.onCuePoint = onCuePoint_handler                          clientB.onBWDone = onBWDone;                                                    //ns.bufferTime=3;                          ns_in.addEventListener(NetStatusEvent.NET_STATUS, netStatus_handler);                          ns_out.addEventListener(NetStatusEvent.NET_STATUS, netStatus_handler);                                                    ns_in.client = clientA;                          ns_out.client = clientB;                          ns_out.publish('room1','live');                          ns_in.play('room1');                                               }                }                                private function netStatus_handler(e:NetStatusEvent):void                {                     //     trace(ns.time)                     //     trace(e.info.code);                                     }                                                //MetaData                private function onMetaData_handler ( infoObject:Object ):void {                     trace ('onMetaData: '+infoObject);                     chat_box.text+= infoObject.name+' : '+infoObject.message+'\n';                     traceObject(infoObject);                }                                private function onCuePoint_handler( item:Object ):void {}                                private function securityErrorHandler(e:SecurityErrorEvent):void                {                     trace("nc error", e.text);                }                                private function traceObject(o:Object):void{                     trace('\n');                     for(var val:* in o){                          trace('   [' + typeof(o[val]) + '] ' + val + ' => ' + o[val]);                     }                     trace('\n');                }           ]]>      </fx:Script>      <s:TextInput id="chat_box" x="54" y="54" width="268" height="206"/>      <s:Button x="368" y="329" label="connect" click="button1_clickHandler(event)"/>      <s:Button x="251" y="271" label="send" click="button2_clickHandler(event)"/>      <s:TextInput id='name_input' x="55" y="26"/>      <s:TextInput id="message_input" x="53" y="270"/> </s:Application>

    This topic has been closed for replies.

    1 reply

    daslicht
    daslichtAuthor
    Known Participant
    December 13, 2010

    Ok small success here,

    ns.public only works for one client, so when closing th eout netstream immediatelly after sending it's working

    December 13, 2010

    Hmm... seems like a less than ideal approach. What if two users try sending at the same time? One will fail.

    If you have to do this with streaming server, I'd open a new stream for each client, and use an http service to keep track of clients as they come and go (having the .swf poll the service every N seconds to get the client list and list of associated streams)

    Or, you could just use a different server application for text chat.

    daslicht
    daslichtAuthor
    Known Participant
    December 13, 2010

    Yeah, I know it was just a test, since I could believe that it is not possible with a 1000 Dollar Server.

    Polling is lame, server push is more attractive.