Simple Text Chat without ServerSide code
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>
