| | |
| | | package com.anyun.im_lib.netty; |
| | | |
| | | import android.util.Log; |
| | | |
| | | import com.anyun.im_lib.HeartbeatRespHandler; |
| | | import com.anyun.im_lib.LoginAuthRespHandler; |
| | | import com.anyun.im_lib.interf.IMSClientInteface; |
| | | |
| | | import io.netty.buffer.ByteBuf; |
| | | import io.netty.buffer.Unpooled; |
| | | import io.netty.channel.Channel; |
| | | import io.netty.channel.ChannelInitializer; |
| | | import io.netty.channel.ChannelPipeline; |
| | | import io.netty.handler.codec.FixedLengthFrameDecoder; |
| | | import io.netty.handler.codec.LengthFieldBasedFrameDecoder; |
| | | import io.netty.handler.codec.LineBasedFrameDecoder; |
| | | import io.netty.handler.codec.DelimiterBasedFrameDecoder; |
| | | |
| | | |
| | | /** |
| | | * MyApplication2 |
| | |
| | | */ |
| | | public class TCPChannelInitializerHandler extends ChannelInitializer<Channel> { |
| | | |
| | | private static final String TAG = TCPChannelInitializerHandler.class.getSimpleName(); |
| | | |
| | | private IMSClientInteface imsClient; |
| | | |
| | | public TCPChannelInitializerHandler(NettyTcpClient nettyTcpClient) { |
| | |
| | | @Override |
| | | protected void initChannel(Channel channel) throws Exception { |
| | | ChannelPipeline pipeline = channel.pipeline(); |
| | | Log.i(TAG, "initChannel: "); |
| | | |
| | | //netty提供的自定义长度解码器,解决TP拆包/粘包问题 |
| | | |
| | |
| | | // 第五个参数为4,表示最终的取到的目标数据包,抛弃最前面的4个字节数据,长度域的值被抛弃。 |
| | | // |
| | | // 为了更加清楚的说明一下上面的规则,调整一下例子中的代码。在写入通道前,在数据 |
| | | pipeline.addLast(new LengthFieldBasedFrameDecoder(1024,3 ,2,1,0)); |
| | | // pipeline.addLast(new LengthFieldBasedFrameDecoder(1024,3 ,2,1,0)); |
| | | |
| | | // pipeline.addLast(new FixedLengthFrameDecoder(10)); // 测试用 固定长度消息 |
| | | |
| | | // pipeline.addLast(new LineBasedFrameDecoder(1024)); |
| | | |
| | | byte[] bytes = new byte[]{0x7e}; |
| | | ByteBuf byteBuf = Unpooled.copiedBuffer(bytes); |
| | | pipeline.addLast(new DelimiterBasedFrameDecoder(1024,byteBuf)); |
| | | |
| | | //握手认证消息相应处理handler |
| | | pipeline.addLast(LoginAuthRespHandler.class.getSimpleName(), new LoginAuthRespHandler(imsClient)); |
| | | |