多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
~~~ @ChannelHandler.Sharable public class EchoClientHandler extends SimpleChannelInboundHandler<ByteBuf> { @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { // 当被通知Channel是活跃的时候,发送一条消息 ctx.writeAndFlush(Unpooled.copiedBuffer("Netty socks", CharsetUtil.UTF_8)); } /** * 1. 每次接收数据,都会调用这个方法 * 2. 服务器发送的数据可能被分块接收,意味着这个方法会被调用多次 */ protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) throws Exception { // 接收消息 System.out.println("Client : "+ byteBuf.toString(CharsetUtil.UTF_8)); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { // 异常,记录错误,并关闭Channel cause.printStackTrace(); ctx.close(); } } ~~~