Created
February 16, 2016 13:13
-
-
Save JamieAP/175d9cfc54dab8532d56 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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