Commit 94fe404f authored by zhushanglei's avatar zhushanglei

websocket和tcpsocket

parent 8cedea05
......@@ -34,10 +34,6 @@ public class TcpServer {
private static Logger logger = LoggerFactory.getLogger(TcpServer.class);
// 服务器地址端口
// private static final String IP = "192.168.0.112";
// private static final String IP = "192.168.0.107";
// public static String IP = "8.143.203.103";
private static final int PORT = 9095;
......@@ -61,46 +57,10 @@ public class TcpServer {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup);
b.channel(NioServerSocketChannel.class);
// b.childOption(ChannelOption.SO_REUSEADDR, true); //快速复用端口
// b.childOption(ChannelOption.CONNECT_TIMEOUT_MILLIS,1000);
b.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
// ChannelPipeline pipeline = ch.pipeline();
// Decode是对发送的信息进行编码、
// @param maxFrameLength 帧的最大长度
// @param lengthFieldOffset length字段偏移的地址
// @param lengthFieldLength length字段所占的字节
// @param lengthAdjustment 修改帧数据长度字段中定义的值,
// 可以为负数 因为有时候我们习惯把头部记入长度,若为负数,则说明要推后多少个字段
// @param initialBytesToStrip 解析时候跳过多少个长度
// pipeline.addLast(new StringEncoder());
// pipeline.addLast(new StringDecoder());
// pipeline.addLast(new TcpServerHandler());
//因为基于http协议,使用http的编码和解码器
// pipeline.addLast(new HttpServerCodec());
// //是以块方式写,添加ChunkedWriteHandler处理器
// pipeline.addLast(new ChunkedWriteHandler());
// // http数据聚合器 用于将大数据量分段传输的数据 聚合
// pipeline.addLast(new HttpObjectAggregator(8192));
// // websocket协议处理器
// pipeline.addLast(new WebSocketServerProtocolHandler("/ws"));
// // 自定义的业务处理
// pipeline.addLast(new TcpServerHandler());
// ch.pipeline().addLast("active",new ChannelActiveHandler() );
// //Socket 连接心跳检测
// ch.pipeline().addLast("idleStateHandler", new IdleStateHandler(60, 0, 0));
//
// ch.pipeline().addLast("socketChoose",new SocketChooseHandler());
//
// //注意,这个专门针对 Socket 信息的解码器只能放在 SocketChooseHandler 之后,否则会导致 webSocket 连接出错
// ch.pipeline().addLast("myDecoder",new LengthFieldBasedFrameDecoder(ByteOrder.LITTLE_ENDIAN,1024*1024,0,4,0,4,true));
// ch.pipeline().addLast("commonhandler",new DeviceServerHandler());
ch.pipeline().addLast("socketChoose",new SocketChooseHandler());
ch.pipeline().addLast(new StringDecoder(CharsetUtil.UTF_8));
ch.pipeline().addLast(new StringEncoder(CharsetUtil.UTF_8));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment