瀏覽代碼

feat(websocket): 集成WebSocket服务器并启动- 添加 WebSocketServerInitializer 用于初始化 Netty 通道- 在 ZksyApplication 中实现 CommandLineRunner 接口以启动 WebSocket 服务器
- 注入 ChatService 并传递给 WebSocketServer 用于处理业务逻辑
- 配置 WebSocket服务器监听端口8081
- 添加必要的 Netty 处理器,如 HttpServerCodec、HttpObjectAggregator 等
- 实现 WebSocket 握手认证处理器 AuthHandshakeHandler- 添加 WebSocketServerHandler 用于处理 WebSocket 消息

nahida 8 月之前
父節點
當前提交
54694eb84a

+ 20 - 5
zksy-admin/src/main/java/com/zksy/ZksyApplication.java

@@ -1,6 +1,9 @@
 package com.zksy;
 package com.zksy;
 
 
-import org.mybatis.spring.annotation.MapperScan;
+import com.zksy.server.WebSocketServer;
+import com.zksy.service.ChatService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.CommandLineRunner;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@@ -11,12 +14,24 @@ import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
  * @author zksy
  * @author zksy
  */
  */
 @SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
 @SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
-public class ZksyApplication
-{
-    public static void main(String[] args)
-    {
+public class ZksyApplication implements CommandLineRunner {
+
+    @Autowired
+    private ChatService chatService;
+    public static void main(String[] args) {
         // System.setProperty("spring.devtools.restart.enabled", "false");
         // System.setProperty("spring.devtools.restart.enabled", "false");
         SpringApplication.run(ZksyApplication.class, args);
         SpringApplication.run(ZksyApplication.class, args);
         System.out.println("中科盛阳管理平台启动成功");
         System.out.println("中科盛阳管理平台启动成功");
     }
     }
+
+    @Override
+    public void run(String... args) throws Exception {
+        new Thread(() -> {
+            try {
+                new WebSocketServer(8081, chatService).start();
+            } catch (InterruptedException e) {
+                e.printStackTrace();
+            }
+        }).start();
+    }
 }
 }

+ 33 - 0
zksy-ws/src/main/java/com/zksy/server/WebSocketServerInitializer.java

@@ -0,0 +1,33 @@
+package com.zksy.server;
+
+import com.zksy.service.ChatService;
+import io.netty.channel.ChannelInitializer;
+import io.netty.channel.socket.SocketChannel;
+import io.netty.handler.codec.http.HttpObjectAggregator;
+import io.netty.handler.codec.http.HttpServerCodec;
+import io.netty.handler.codec.http.cors.CorsConfig;
+import io.netty.handler.codec.http.cors.CorsConfigBuilder;
+import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
+import io.netty.handler.stream.ChunkedWriteHandler;
+
+public class WebSocketServerInitializer extends ChannelInitializer<SocketChannel> {
+
+    private final ChatService chatService;
+
+    public WebSocketServerInitializer(ChatService chatService) {
+        this.chatService = chatService;
+    }
+
+
+    @Override
+    protected void initChannel(SocketChannel ch) {
+        ch.pipeline()
+                .addLast(new HttpServerCodec())
+                .addLast(new HttpObjectAggregator(65536))
+                .addLast(new ChunkedWriteHandler())
+                .addLast(new AuthHandshakeHandler())
+                .addLast(new WebSocketServerProtocolHandler("/ws"))
+                // 给业务 handler 起个名字,方便动态 addBefore
+                .addLast("handler", new WebSocketServerHandler(chatService));
+    }
+}