Skip to content

Instantly share code, notes, and snippets.

@JamieAP
Created February 16, 2016 13:13
Show Gist options
  • Save JamieAP/175d9cfc54dab8532d56 to your computer and use it in GitHub Desktop.
Save JamieAP/175d9cfc54dab8532d56 to your computer and use it in GitHub Desktop.
/**
* Performs the initial set up of sockets as they connect to Netty.
* Registers the pipeline of handlers that received messages are passed through
*/
public class MySocketInitialiser extends ChannelInitializer<SocketChannel> {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(LineBasedFrameDecoder.class.getName(),
new LineBasedFrameDecoder(256));
pipeline.addLast(StringDecoder.class.getName(),
new StringDecoder(CharsetUtil.UTF_8));
pipeline.addLast(JsonDecoder.class.getName(),
new JsonDecoder<>(Person.class));
pipeline.addLast("stdoutHandler",
new ChannelInboundMessageHandlerAdapter<Person>() {
@Override
public void messageReceived(ChannelHandlerContext ctx, Person person) throws Exception {
System.out.println(
"Your name is " + person.getFirstName() + " " + person.getLastName() + "!"
);
}
}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment