【前言】
之前用TCP模式传输ProtocolBuf模式,后面上了一个websocket传输ProtocolBuf业务,目前基本已经稳定了,现在把编解码器部分记录下来。
【server部件的组装】
核心思想是ProtocolBuf是byte[]流,而websocket对象在Netty自带的编解码中是对象,其中的数据部分是pb序列化后的字节流(即,ws的内容是二进制字节数组)所以在编码器和解码器上需要做转换
protected void initChannel(Channel ch) throws Exception {
ch.pipeline().addLast("readTimeout", new ReadTimeoutHandler(45)); // 长时间不写会断
ch.pipeline().addLast("HttpServerCodec", new HttpServerCodec());
ch.pipeline().addLast("ChunkedWriter", new ChunkedWriteHandler());
ch.pipeline().addLast("HttpAggregator", new HttpObjectAggregator(65535));
ch.pipeline().addLast("WsProtocolDecoder",
new WebSocketServerProtocolHandler(WEBSOCKET_PATH, null, true));
ch.pipeline().addLast("WsBinaryDecoder", new WebSocketFrameDecoder()); // ws解码成字节
ch.pipeline().