Code
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.messaging.AbstractConsumer;
import flash.net.NetConnection;
import mx.controls.Alert;
import flash.events.*;
import mx.collections.ArrayCollection;
private var netConn:NetConnection;
private var rso:SharedObject;
private function btnConnection_click(evt:MouseEvent):void
{
netConn = new NetConnection();
netConn.connect("rtmp://localhost/myapp","zy");
netConn.addEventListener(NetStatusEvent.NET_STATUS,netConn_status);
}
private function netConn_status(evt:NetStatusEvent):void
{
if (evt.info.code == "NetConnection.Connect.Success") {
Alert.show("连接成功");
trace(evt.info.code);
//共享远程对象
rso = SharedObject.getRemote("myRSO",netConn.uri,true);
rso.addEventListener(SyncEvent.SYNC,onRemoteSync);
rso.connect(netConn);
}
}
private function onRemoteSync(evt:SyncEvent):void
{
var tempArr:ArrayCollection = new ArrayCollection();
tempArr = rso.data.msgArr as ArrayCollection;
if (tempArr == null)
{
return;
}
txtMessage.text = "";
for(var i:int=0;i<tempArr.length;i++)
{
var message:String = tempArr.getItemAt(i).toString();
txtMessage.text += message + "\n";
}
}
private function btnSend_click(evt:MouseEvent):void
{
var tempArr:ArrayCollection = new ArrayCollection();
if (rso.data.msgArr != null)
{
this.convertArrayCollection(tempArr,rso.data.msgArr as ArrayCollection);
}
tempArr.addItem(txtNickName.text+":"+txtContent.text);
rso.setProperty("msgArr",tempArr);
this.txtContent.text = "";
}
private function convertArrayCollection(arrNew:ArrayCollection,arrOld:ArrayCollection):void
{
arrNew.removeAll();
for(var i:int=0;i<arrOld.length ;i++)
{
arrNew.addItemAt(arrOld.getItemAt(i),i);
}
}
]]>
</mx:Script>
<mx:Button id="btnConnection" x="10" y="10" label="连接" fontSize="12" click="btnConnection_click(event)"/>
<mx:TextArea x="10" y="42" width="328" height="183" id="txtMessage"/>
<mx:TextInput x="10" y="233" width="43" id="txtNickName"/>
<mx:Button id="btnSend" x="286" y="233" label="发送" fontSize="12" click="btnSend_click(event)" />
<mx:TextInput x="61" y="233" width="217" id="txtContent"/>
</mx:Application>
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.messaging.AbstractConsumer;
import flash.net.NetConnection;
import mx.controls.Alert;
import flash.events.*;
import mx.collections.ArrayCollection;
private var netConn:NetConnection;
private var rso:SharedObject;
private function btnConnection_click(evt:MouseEvent):void
{
netConn = new NetConnection();
netConn.connect("rtmp://localhost/myapp","zy");
netConn.addEventListener(NetStatusEvent.NET_STATUS,netConn_status);
}
private function netConn_status(evt:NetStatusEvent):void
{
if (evt.info.code == "NetConnection.Connect.Success") {
Alert.show("连接成功");
trace(evt.info.code);
//共享远程对象
rso = SharedObject.getRemote("myRSO",netConn.uri,true);
rso.addEventListener(SyncEvent.SYNC,onRemoteSync);
rso.connect(netConn);
}
}
private function onRemoteSync(evt:SyncEvent):void
{
var tempArr:ArrayCollection = new ArrayCollection();
tempArr = rso.data.msgArr as ArrayCollection;
if (tempArr == null)
{
return;
}
txtMessage.text = "";
for(var i:int=0;i<tempArr.length;i++)
{
var message:String = tempArr.getItemAt(i).toString();
txtMessage.text += message + "\n";
}
}
private function btnSend_click(evt:MouseEvent):void
{
var tempArr:ArrayCollection = new ArrayCollection();
if (rso.data.msgArr != null)
{
this.convertArrayCollection(tempArr,rso.data.msgArr as ArrayCollection);
}
tempArr.addItem(txtNickName.text+":"+txtContent.text);
rso.setProperty("msgArr",tempArr);
this.txtContent.text = "";
}
private function convertArrayCollection(arrNew:ArrayCollection,arrOld:ArrayCollection):void
{
arrNew.removeAll();
for(var i:int=0;i<arrOld.length ;i++)
{
arrNew.addItemAt(arrOld.getItemAt(i),i);
}
}
]]>
</mx:Script>
<mx:Button id="btnConnection" x="10" y="10" label="连接" fontSize="12" click="btnConnection_click(event)"/>
<mx:TextArea x="10" y="42" width="328" height="183" id="txtMessage"/>
<mx:TextInput x="10" y="233" width="43" id="txtNickName"/>
<mx:Button id="btnSend" x="286" y="233" label="发送" fontSize="12" click="btnSend_click(event)" />
<mx:TextInput x="61" y="233" width="217" id="txtContent"/>
</mx:Application>