|
@@ -30,6 +30,7 @@ import java.util.concurrent.ThreadFactory;
|
|
|
*/
|
|
|
public class NettySystem extends BaseSystem {
|
|
|
|
|
|
+ private final int port;
|
|
|
private static final int BOSS_THREADS = 1;
|
|
|
// private static final int BOSS_THREADS = Math.max(1, Runtime.getRuntime().availableProcessors() / 2);
|
|
|
private static final int WORKER_THREADS = Math.max(1, Runtime.getRuntime().availableProcessors() * 2);
|
|
@@ -37,11 +38,15 @@ public class NettySystem extends BaseSystem {
|
|
|
private final ServerBootstrap bootstrap = new ServerBootstrap();
|
|
|
private IoEventLoopGroup bossGroup;
|
|
|
private IoEventLoopGroup workerGroup;
|
|
|
- private Channel channel;
|
|
|
+ private Channel serverChannel;
|
|
|
+
|
|
|
+ public NettySystem(int port) {
|
|
|
+ this.port = port;
|
|
|
+ }
|
|
|
|
|
|
@Override
|
|
|
protected void initialize() {
|
|
|
- InetSocketAddress address = new InetSocketAddress("0.0.0.0", 9000);
|
|
|
+ InetSocketAddress address = new InetSocketAddress("0.0.0.0", this.port);
|
|
|
// 使用单线程 bossGroup,高并发时 workerGroup 使用默认线程数
|
|
|
this.bossGroup = this.createIoEventLoopGroup(BOSS_THREADS, "Netty-Boss");
|
|
|
this.workerGroup = this.createIoEventLoopGroup(WORKER_THREADS, "Netty-Worker");
|
|
@@ -50,7 +55,7 @@ public class NettySystem extends BaseSystem {
|
|
|
try {
|
|
|
ChannelFuture future = this.bootstrap.bind(address).sync();
|
|
|
|
|
|
- this.channel = future.channel();
|
|
|
+ this.serverChannel = future.channel();
|
|
|
System.out.println("WebSocket server started at ws://" + address.getHostName() + ":" + address.getPort() + "/ws");
|
|
|
} catch (InterruptedException e) {
|
|
|
Thread.currentThread().interrupt();
|
|
@@ -128,8 +133,8 @@ public class NettySystem extends BaseSystem {
|
|
|
@Override
|
|
|
protected void dispose() {
|
|
|
try {
|
|
|
- if (this.channel != null) {
|
|
|
- this.channel.close().sync();
|
|
|
+ if (this.serverChannel != null) {
|
|
|
+ this.serverChannel.close().sync();
|
|
|
}
|
|
|
if (this.bossGroup != null) {
|
|
|
this.bossGroup.shutdownGracefully().sync();
|