use of io.netty.handler.codec.string.LineEncoder in project chuidiang-ejemplos by chuidiang.
the class ServerWithNettyHandlers method run.
public void run() throws Exception {
// (1)
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
// (2)
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup).channel(// (3)
NioServerSocketChannel.class).childHandler(new // (4)
ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new LineBasedFrameDecoder(1000));
ch.pipeline().addLast(new LineEncoder(Charset.defaultCharset()));
ch.pipeline().addLast(new StringDecoder(Charset.defaultCharset()));
ch.pipeline().addLast(serverHandler);
}
}).option(ChannelOption.SO_BACKLOG, // (5)
128).childOption(ChannelOption.SO_KEEPALIVE, // (6)
true);
// Bind and start to accept incoming connections.
// (7)
ChannelFuture f = b.bind(port).sync();
// Wait until the server socket is closed.
// In this example, this does not happen, but you can do that to gracefully
// shut down your server.
f.channel().closeFuture().sync();
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
Aggregations