1 /* 2 * 覆盖了 channelRead0() 事件处理方法。 3 * 每当从服务端读到客户端写入信息时, 4 * 其中如果你使用的是 Netty 5.x 版本时, 5 * 需要把 channelRead0() 重命名为messageReceived() 6 */ 7 @Override 8 protected void channelRead0(ChannelHandlerContext arg0, String arg1) 9 throws Exception { 10 // TODO Auto-generated method stub 11 12 } 13 14 /* 15 * 覆盖channelActive 方法在channel被启用的时候触发(在建立连接的时候) 16 * 覆盖了 channelActive() 事件处理方法。服务端监听到客户端活动 17 */ 18 public void channelActive(ChannelHandlerContext ctx) throws Exception { 19 // TODO Auto-generated method stub 20 super.channelActive(ctx); 21 } 22 23 /* 24 * (non-Javadoc) 25 * 覆盖了 handlerAdded() 事件处理方法。 26 * 每当从服务端收到新的客户端连接时 27 */ 28 public void handlerAdded(ChannelHandlerContext ctx) throws Exception { 29 // TODO Auto-generated method stub 30 super.handlerAdded(ctx); 31 } 32 33 /* 34 * (non-Javadoc) 35 * .覆盖了 handlerRemoved() 事件处理方法。 36 * 每当从服务端收到客户端断开时 37 */ 38 public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { 39 // TODO Auto-generated method stub 40 super.handlerRemoved(ctx); 41 } 42 43 /* 44 * exceptionCaught() 事件处理方法是当出现 Throwable 对象才会被调用, 45 * 即当 Netty 由于 IO 错误或者处理器在处理事件时抛出的异常时。 46 * 在大部分情况下,捕获的异常应该被记录下来并且把关联的 channel 给关闭掉。 47 * 然而这个方法的处理方式会在遇到不同异常的情况下有不同的实现, 48 * 比如你可能想在关闭连接之前发送一个错误码的响应消息。 49 */ 50 public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) 51 throws Exception { 52 // TODO Auto-generated method stub 53 super.exceptionCaught(ctx, cause); 54 }