Socket+AMF3粘包问题
问题:
服务端Mina 前台接收Flex? ?
参照的原型为http://www.klstudio.com/post/202.html
<[????? this.isReadHead = true;
??? }
??? //如果读了头部,并且当前可读长度大于等于消息长度,则开始读取
??? if (isReadHead && this.sk.bytesAvailable >= this.msgLen)
????? {
????? var objByte:ByteArray = new ByteArray();
????? sk.readBytes(objByte, 0, this.msgLen);
????? this.isReadHead = false;
????? //objByte.uncompress();
????? var obj:Object = objByte.readObject();
????? this.readMsg(obj);?? //读完了,可以去解释了
????? }
??? //如果是读过头,则如果当前消息大于消息长度则再次调用读取,否则则判断是否可读头部
????? this.sk.bytesAvailable > 0 && this.readData();
????????????
?? }?
至于server端,其原理是一样的,都是读的时候先读头,发的时候也是先发个头。
补充server端代码(C#,解码类为FluorineFx,仅供参考思路)
server -> as3 发送
??? public void send(object obj)
??????????? {
??????????????? try
??????????????? {
??????????????????? ByteArray objByte = new ByteArray();
??????????????????? objByte.WriteObject(obj);
?????????????????? // objByte.Compress();
??????????????????? byte[] objBuff = new byte[objByte.Length];
??????????????????? objByte.Position = 0;
??????????????????? objByte.ReadBytes(objBuff, (uint)0, (uint)objBuff.Length);
?????????????????
??????????????????? ByteArray msgByte = new ByteArray();
??????????????????? msgByte.WriteInt(objBuff.Length);
??????????????????? msgByte.WriteBytes(objBuff, 0, objBuff.Length);
??????????????????? byte[] msgBuff = new byte[msgByte.Length];
??????????????????? msgByte.Position = 0;
??????????????????? msgByte.ReadBytes(msgBuff, (uint)0, (uint)msgBuff.Length);
??????????????????? //发送
?????????????????? this.ng.sk.BeginSend(msgBuff, 0, msgBuff.Length, SocketFlags.None, onSendOver, this.ng);
?????
??????????????? }
??????????????? catch
??????????????? {
??????????????????? Core.mainForm.addLog("发送失败");
??????????????? }
??????????? }