下面是代码
flash socket 发对象
private function SendObject(params:Object=null):void
{
if(params != null){
var bytes:ByteArray = new ByteArray();
bytes.writeObject(params);//写入对象
bytes.compress(); //压缩
socketsendarr(SgsProtocol.OBJECT_MESSAGE,bytes);
}
//socket 发送字节数组,cmd是命令
private function socketsendarr(cmd:int,message:ByteArray):void
{
if (connected)
{
var buf:ByteArray = new ByteArray();
buf.writeByte(cmd);
buf.writeBytes(message);
socket.writeShort(buf.length);
socket.writeBytes(buf);
socket.flush();
debugMessage("send sgs:"+cmd.toString()+" 大小"+(buf.length+2));
}
}
//发了对象就要解析了
前面一部分先把命令先读了,后面的是对象
public function ObjectMsg(o:ByteArray):void
{
var bytes:ByteArray = new ByteArray();
o.readBytes(bytes, 0, o.bytesAvailable);//读对象
bytes.uncompress();//解压
var evt:HDEvent = new HDEvent(HDEvent.OnObject, bytes.readObject())
HdClient.dispatchEvent(evt)
//触发事件
}