final WebSocketServerHandler webSocketHandler = new WebSocketServerHandler(svc);
try {
final NioEventLoopGroup bossGroup = new NioEventLoopGroup();
final NioEventLoopGroup workerGroup = new NioEventLoopGroup();
final ChannelFuture channelFuture = bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer() {
@Override
protected void initChannel(Channel ch) throws Exception {
if (useSecureWebSocket) {
final SslHandler sslHandler = SslHandlerFactory.buildSslHandler(esc);
ch.pipeline().addLast("ssl", sslHandler);
}
ch.pipeline().addLast("codec-http", new HttpServerCodec());
ch.pipeline().addLast("aggregator", new HttpObjectAggregator(65536));
ch.pipeline().addLast("handler", webSocketHandler);
}
}).bind(port).sync();
svc.addShutdownHook(new Runnable() {
@Override
public void run() {
try {
webSocketHandler.stop();
channelFuture.channel().close();
log.info("web socket server stopped.");
}
catch (Exception e) {
throw new RuntimeException(e);
}