美文网首页
Netty client handler

Netty client handler

作者: 寻常_d32c | 来源:发表于2018-08-01 16:23 被阅读0次

package com.star.netty.handler;

import io.netty.buffer.ByteBuf;import io.netty.buffer.Unpooled;import io.netty.channel.ChannelHandlerContext;import io.netty.channel.SimpleChannelInboundHandler;import io.netty.util.CharsetUtil;/** * @ClassName ClientHandler * @Author wjp * @Date 2018/7/23 13:43 */public class ClientHandler extends SimpleChannelInboundHandler {

  /**

  * 服务器的连接已经建立之后被调用

  * @param ctx

  * @throws Exception

  */

  @Override

  public void channelActive(ChannelHandlerContext ctx) throws Exception {

    ctx.writeAndFlush(Unpooled.copiedBuffer("netty rocks", CharsetUtil.UTF_8));

    //super.channelActive(ctx);

  }

  /**

  * 处理过程中引发异常时被调用

  * @param ctx

  * @param cause

  * @throws Exception

  */

  @Override

  public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {

    super.exceptionCaught(ctx, cause);

  }

  /**

  * 从服务器中接收到一条消息时被调用

  * @param channelHandlerContext

  * @param byteBuf

  * @throws Exception

  */

  @Override

  protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf)

      throws Exception {

    System.out.println("client received:" + byteBuf.toString(CharsetUtil.UTF_8));

  }

}

相关文章

网友评论

      本文标题:Netty client handler

      本文链接:https://www.haomeiwen.com/subject/tnjrvftx.html