💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、豆包、星火、月之暗面及文生图、文生视频 广告
~~~ @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(); } } ~~~