Use pooled allocator for AutobahnServer and voidPromises
This commit is contained in:
parent
5de4b23c7a
commit
b5a587fadf
@ -16,7 +16,9 @@
|
||||
package io.netty.example.http.websocketx.autobahn;
|
||||
|
||||
import io.netty.bootstrap.ServerBootstrap;
|
||||
import io.netty.buffer.PooledByteBufAllocator;
|
||||
import io.netty.channel.ChannelFuture;
|
||||
import io.netty.channel.ChannelOption;
|
||||
import io.netty.channel.EventLoopGroup;
|
||||
import io.netty.channel.nio.NioEventLoopGroup;
|
||||
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
||||
@ -34,20 +36,20 @@ public class AutobahnServer {
|
||||
}
|
||||
|
||||
public void run() throws Exception {
|
||||
EventLoopGroup bossGroup = new NioEventLoopGroup();
|
||||
EventLoopGroup workerGroup = new NioEventLoopGroup();
|
||||
EventLoopGroup group = new NioEventLoopGroup();
|
||||
try {
|
||||
ServerBootstrap b = new ServerBootstrap();
|
||||
b.group(bossGroup, workerGroup)
|
||||
b.group(group)
|
||||
.channel(NioServerSocketChannel.class)
|
||||
.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
|
||||
.option(ChannelOption.SO_BACKLOG, 1024)
|
||||
.childHandler(new AutobahnServerInitializer());
|
||||
|
||||
ChannelFuture f = b.bind(port).sync();
|
||||
System.out.println("Web Socket Server started at port " + port);
|
||||
f.channel().closeFuture().sync();
|
||||
} finally {
|
||||
bossGroup.shutdownGracefully();
|
||||
workerGroup.shutdownGracefully();
|
||||
group.shutdownGracefully();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -35,6 +35,7 @@ import io.netty.handler.codec.http.websocketx.WebSocketFrame;
|
||||
import io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker;
|
||||
import io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory;
|
||||
import io.netty.util.CharsetUtil;
|
||||
import io.netty.util.ReferenceCountUtil;
|
||||
import io.netty.util.internal.StringUtil;
|
||||
|
||||
import java.util.logging.Level;
|
||||
@ -59,6 +60,8 @@ public class AutobahnServerHandler extends ChannelInboundHandlerAdapter {
|
||||
handleHttpRequest(ctx, (FullHttpRequest) msg);
|
||||
} else if (msg instanceof WebSocketFrame) {
|
||||
handleWebSocketFrame(ctx, (WebSocketFrame) msg);
|
||||
} else {
|
||||
ReferenceCountUtil.release(msg);
|
||||
}
|
||||
}
|
||||
|
||||
@ -104,13 +107,13 @@ public class AutobahnServerHandler extends ChannelInboundHandlerAdapter {
|
||||
if (frame instanceof CloseWebSocketFrame) {
|
||||
handshaker.close(ctx.channel(), (CloseWebSocketFrame) frame);
|
||||
} else if (frame instanceof PingWebSocketFrame) {
|
||||
ctx.write(new PongWebSocketFrame(frame.isFinalFragment(), frame.rsv(), frame.content()));
|
||||
ctx.write(new PongWebSocketFrame(frame.isFinalFragment(), frame.rsv(), frame.content()), ctx.voidPromise());
|
||||
} else if (frame instanceof TextWebSocketFrame) {
|
||||
ctx.write(frame);
|
||||
ctx.write(frame, ctx.voidPromise());
|
||||
} else if (frame instanceof BinaryWebSocketFrame) {
|
||||
ctx.write(frame);
|
||||
ctx.write(frame, ctx.voidPromise());
|
||||
} else if (frame instanceof ContinuationWebSocketFrame) {
|
||||
ctx.write(frame);
|
||||
ctx.write(frame, ctx.voidPromise());
|
||||
} else if (frame instanceof PongWebSocketFrame) {
|
||||
frame.release();
|
||||
// Ignore
|
||||
@ -139,7 +142,6 @@ public class AutobahnServerHandler extends ChannelInboundHandlerAdapter {
|
||||
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
||||
cause.printStackTrace();
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user