johnclot69 1 сар өмнө
parent
commit
0da4cb1917

+ 10 - 5
incubator-core/src/main/java/com/incubator/core/network/NettySystem.java

@@ -29,6 +29,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);
@@ -36,11 +37,15 @@ public class NettySystem extends BaseSystem {
     private final ServerBootstrap bootstrap = new ServerBootstrap();
     private EventLoopGroup bossGroup;
     private EventLoopGroup 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.createEventLoopGroup(BOSS_THREADS, "Netty-Boss");
         this.workerGroup = this.createEventLoopGroup(WORKER_THREADS, "Netty-Worker");
@@ -49,7 +54,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();
@@ -111,8 +116,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();

+ 1 - 1
incubator-game/src/main/java/com/incubator/game/Game.java

@@ -29,7 +29,7 @@ public class Game {
         // 构建 WorldConfiguration,注册 GameSystem
         WorldConfiguration config = new WorldConfigurationBuilder()
                 .with(new MessageHandlerSystem())
-                .with(new NettySystem())
+                .with(new NettySystem(9000))
                 .build();
 
         // 创建 World 对象(服务端主实体)