Skip to content

Instantly share code, notes, and snippets.

@melin
Created December 22, 2013 11:47
Show Gist options
  • Save melin/8081255 to your computer and use it in GitHub Desktop.
Save melin/8081255 to your computer and use it in GitHub Desktop.
netty server client handler
ChannelInboundHandlerAdapter
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
System.out.println(ìServer received: ì + msg);
ctx.write(msg) #2
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) {
ctx.writeAndFlush(Unpooled.EMPTY_BUFFER)
.addListener(ChannelFutureListener.CLOSE); #3
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx,
Throwable cause) {
cause.printStracktrace(); #4
ctx.close(); #5
}
SimpleChannelInboundHandlerAdapter
@Override
public void channelActive(ChannelHandlerContext ctx) {
ctx.write(Unpooled.copiedBuffer(ìNetty rocks!ì,
CharsetUtil.UTF_8); #2
}
@Override
public void channelRead0(ChannelHandlerContext ctx, ByteBuf in) {
System.out.println(ìClient received: ì + ByteBufUtil
.hexDump(in.readBytes(in.readableBytes())));
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
cause.printStracktrace();
ctx.close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment