刚开始的WS请求是封装在HTTP里的,HTTP头部包含Upgrade:websocket。必须处理此HTTP请求
private void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, DefaultFullHttpResponse res) { if (res.status().code() != 200) { ByteBuf buf = Unpooled.copiedBuffer(res.status().toString(), CharsetUtil.UTF_8); res.content().writeBytes(buf); buf.release(); } //服务端向客户端发送数据 ChannelFuture f = ctx.channel().writeAndFlush(res); if (res.status().code() != 200) { f.addListener(ChannelFutureListener.CLOSE); } }
使用decoderResult就好了
这是原来的代码,这个讲师代码少抄了
我的经验是:我先去找几篇博客看一下然后再来看这个就清晰多了
老师疏忽了应该,你去掉就行了,不影响运行