AS3与JAVA、Netty Socket AMF通信 粘包处理

amf是Adobe独家开发的数据封装格式,Flash可以直接访问,而java要使用它,则需要相关的类库

需要从blazeds中提取,可以到下面的地址下载

下载地址:http://sourceforge.net/adobe/blazeds/wiki/Home/

首先我们来定义通信包的结构,为了避免粘包断包的问题,我们如下定义包的结构

这样在接收数据的时候,我们判定内容是否大于长度,若大于则可以读取。

发送数据的时候,我们先写入内容的长度,然后在写入内容

AS3发送数据到Server

//向服务端发送数据  
        public function sendMessage(obj:Object):Boolean{  
            trace("-----发送数据-----");  
            if (socket.connected) {  
                var objByte:ByteArray = new ByteArray();  
                objByte.writeObject(obj);   
                var msgByte:ByteArray = new ByteArray();  
                msgByte.writeInt(objByte.length);//写入正文长度  
                msgByte.writeBytes(objByte, 0, objByte.length);//写入正文   
                socket.writeBytes(msgByte);//向socket发送数据  
                socket.flush();  
                return true;  
            }else {  
                return false;  
            }  
              
        }



AS3接收Server数据

//读取服务器数据  
        private function readMessage():void {  
            trace("-----读取数据-----");  
            //如果没读取过头部并且可读取  
            if (!this.isReadHead && this.socket.bytesAvailable > 4) {  
                    //trace("-----读取头部-----");  
                    var lenByte:ByteArray = new ByteArray();  
                    this.socket.readBytes(lenByte, 0, 4);  
                    this.messageLen = lenByte.readInt();  
                    this.isReadHead = true;  
                      
            }  
              
            //如果已经读取过头部并且当前消息大于等于包长度  
            if (this.isReadHead && this.socket.bytesAvailable >= this.messageLen) {  
                //trace("-----读取正文-----");  
                var msgByte:ByteArray = new ByteArray();  
                this.socket.readBytes(msgByte, 0, this.messageLen);  
                this.isReadHead = false;  
                var msg:Object = msgByte.readObject();  
                this.disPatch(msg);//处理msg  
                //如果socket中仍然有数据可以处理  
                if (this.socket.bytesAvailable > 4) {  
                    this.readMessage();  
                }  
            }  
              
              
              
        }
Server(Netty)发送数据到->AS3
public class Encode extends OneToOneEncoder {  
  
    @Override  
    protected Object encode(ChannelHandlerContext ctx, Channel channel,  
            Object msg) throws Exception {  
        // TODO Auto-generated method stub  
        if(!( msg instanceof ASObject)){  
            return msg;  
        }  
          
        System.out.println("encode");  
        msg = (ASObject)msg;  
        ByteArrayOutputStream out = new ByteArrayOutputStream();  
        Amf3Output amf3Output = new Amf3Output(SerializationContext.getSerializationContext());  
        amf3Output.setOutputStream(out);  
        amf3Output.writeObject(msg);  
  
        byte content[] = out.toByteArray();  
        ChannelBuffer buf = ChannelBuffers.buffer(content.length+4);  
        buf.writeInt(content.length);  
        buf.writeBytes(content);  
        System.out.println("endcode ok");  
        return buf;  
    }  
  
}


Server(Netty)接收AS3数据


public class DecodeHandle extends FrameDecoder {

	@Override
	protected Object decode(ChannelHandlerContext ctx, Channel channel,
			ChannelBuffer buffer) throws Exception {
		System.out.println("start decodeing");
		if(buffer.readableBytes()<4){
			return null;
		}
		buffer.markReaderIndex();
		int needBytes = buffer.readInt();
		if(buffer.readableBytes()<needBytes){
			buffer.resetReaderIndex();
			return null;
		}
		byte[] content = new byte[buffer.readableBytes()];
		buffer.readBytes(content);
		Amf3Input amf3Input = new Amf3Input(SerializationContext.getSerializationContext());
		amf3Input.setInputStream(new ByteArrayInputStream(content));
		System.out.println("decode end");
		return amf3Input.readObject();
	}

}




你可能感兴趣的:(AS3与JAVA、Netty Socket AMF通信 粘包处理)