Parcourir la source

框架内dev插件增加webscoket封装,gateway增加webscoket支持

zhaosongshan il y a 7 mois
Parent
commit
0c0ed9768d

+ 16 - 0
pom.xml

@@ -28,6 +28,7 @@
         <!-- snowy-base -->
         <snowy.version>2.0.0</snowy.version>
         <spring-framework.version>5.3.26</spring-framework.version>
+        <spring.boot.starter.websocket>3.5.3</spring.boot.starter.websocket>
         <spring.boot-version>2.5.12</spring.boot-version>
         <spring.cloud-version>2020.0.6</spring.cloud-version>
         <spring.cloud.bootstrap-version>3.1.3</spring.cloud.bootstrap-version>
@@ -1033,6 +1034,21 @@
                 <artifactId>sa-token-reactor-spring-boot-starter</artifactId>
                 <version>${sa.token.version}</version>
             </dependency>
+            <dependency>
+                <groupId>org.springframework.boot</groupId>
+                <artifactId>spring-boot-starter-websocket</artifactId>
+                <version>${spring.boot.starter.websocket}</version>
+                <exclusions>
+                    <exclusion>
+                        <groupId>ch.qos.logback</groupId>
+                        <artifactId>logback-classic</artifactId>
+                    </exclusion>
+                    <exclusion>
+                        <groupId>ch.qos.logback</groupId>
+                        <artifactId>logback-core</artifactId>
+                    </exclusion>
+                </exclusions>
+            </dependency>
         </dependencies>
     </dependencyManagement>
 

+ 0 - 6
snowy-modules/snowy-web-app/src/main/java/vip/xiaonuo/web/SnowyWebApp.java

@@ -70,10 +70,4 @@ public class SnowyWebApp {
     public AppStartupListener appStartupListener(){
         return new AppStartupListener();
     }
-
-    @Bean
-    public ServerEndpointExporter serverEndpointExporter() {
-        log.info("正在启动开启websocket支持") ;
-        return new ServerEndpointExporter();
-    }
 }

+ 1 - 1
snowy-modules/snowy-web-app/src/main/java/vip/xiaonuo/web/core/config/GlobalConfigure.java

@@ -181,7 +181,7 @@ public class GlobalConfigure implements WebMvcConfigurer {
             "/resourceFile/downloadfile",
             "/resourceFile/batchDownloadFile",
 
-            "/test/auto",
+            "/webSocket/**",
 
             /* 资源中心 */
             "/disk/resourcecentre/page",

+ 28 - 1
snowy-plugin/snowy-plugin-dev/snowy-plugin-dev-func/pom.xml

@@ -43,5 +43,32 @@
             <groupId>vip.xiaonuo</groupId>
             <artifactId>snowy-plugin-ten-api</artifactId>
         </dependency>
+
+
+
+        <!-- logback-classic -->
+        <dependency>
+            <groupId>ch.qos.logback</groupId>
+            <artifactId>logback-classic</artifactId>
+        </dependency>
+        <!-- logback-classic -->
+        <dependency>
+            <groupId>ch.qos.logback</groupId>
+            <artifactId>logback-core</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-websocket</artifactId>
+            <exclusions>
+                <exclusion>
+                    <groupId>ch.qos.logback</groupId>
+                    <artifactId>logback-classic</artifactId>
+                </exclusion>
+                <exclusion>
+                    <groupId>ch.qos.logback</groupId>
+                    <artifactId>logback-core</artifactId>
+                </exclusion>
+            </exclusions>
+        </dependency>
     </dependencies>
-</project>
+</project>

+ 34 - 0
snowy-plugin/snowy-plugin-dev/snowy-plugin-dev-func/src/main/java/vip/xiaonuo/dev/core/websocket/WebSocketConfig.java

@@ -0,0 +1,34 @@
+package vip.xiaonuo.dev.core.websocket;
+
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.socket.server.standard.ServerEndpointExporter;
+
+/**
+ * @Author ZSS
+ * @Date 2024-05-28 08:39
+ * @Note:
+ **/
+@Configuration
+@Slf4j
+public class WebSocketConfig {
+
+    /**
+     * 给spring容器注入这个ServerEndpointExporter对象
+     * 相当于xml:
+     * <beans>
+     * <bean/>
+     * </beans>
+     * <p>
+     * 检测所有带有@serverEndpoint注解的bean并注册他们。
+     *
+     * @return ServerEndpointExporter对象
+     */
+    @Bean
+    public ServerEndpointExporter serverEndpointExporter() {
+        log.info("WebSocketConfig 注入!");
+        return new ServerEndpointExporter();
+    }
+}
+

+ 82 - 0
snowy-plugin/snowy-plugin-dev/snowy-plugin-dev-func/src/main/java/vip/xiaonuo/dev/core/websocket/WebSocketServer.java

@@ -0,0 +1,82 @@
+package vip.xiaonuo.dev.core.websocket;
+
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.stereotype.Component;
+
+import javax.websocket.*;
+import javax.websocket.server.PathParam;
+import javax.websocket.server.ServerEndpoint;
+import java.io.IOException;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicInteger;
+
+/**
+ * @Author ZSS
+ * @Date 2024-05-28 08:39
+ * @Note: WebSocketServer
+ **/
+@ServerEndpoint("/webSocket/{clientId}")
+@Component
+@Slf4j
+public class WebSocketServer {
+
+    private static final AtomicInteger ONLINE_COUNT = new AtomicInteger(0);
+    private static Map<String, WebSocketServer> clients = new ConcurrentHashMap<>();
+    private Session session;
+    private String clientId;
+
+    @OnOpen
+    public void onOpen(@PathParam("clientId") String clientId, Session session) throws IOException {
+        this.clientId = clientId;
+        this.session = session;
+
+        clients.put(clientId, this);
+        // 在线数加1
+        int cnt = ONLINE_COUNT.incrementAndGet();
+        log.info("{}加入连接,当前连接数为:{}", clientId, cnt);
+
+    }
+
+    @OnClose
+    public void onClose() throws IOException {
+        clients.remove(clientId);
+        int cnt = ONLINE_COUNT.decrementAndGet();
+        log.info("有连接关闭,当前连接数为:{}", cnt);
+    }
+
+    @OnMessage
+    public void onMessage(String message) throws IOException {
+        log.info("来自客户端的消息:{}", message);
+        sendInfo(session, message);
+    }
+
+    @OnError
+    public void onError(Session session, Throwable error) {
+        error.printStackTrace();
+    }
+
+    public void sendMessage(String message, String clientId) throws Exception {
+        Session session = null;
+        for (WebSocketServer item : clients.values()) {
+            if (item.clientId.equals(clientId)) {
+                session = item.session;
+            }
+        }
+        if (session == null) {
+            throw new Exception("找不到对应的seesion!");
+        }
+        sendInfo(session, message);
+    }
+
+    public void sendMessageAll(String message) throws IOException {
+        for (WebSocketServer item : clients.values()) {
+            sendInfo(item.session, message);
+        }
+    }
+
+    private void sendInfo(Session session, String message) throws IOException {
+        session.getAsyncRemote().sendText(message);
+    }
+}

+ 0 - 11
snowy-plugin/snowy-plugin-sys/snowy-plugin-sys-func/pom.xml

@@ -42,16 +42,5 @@
             <groupId>vip.xiaonuo</groupId>
             <artifactId>snowy-plugin-disk-api</artifactId>
         </dependency>
-
-        <dependency>
-            <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-starter-websocket</artifactId>
-        </dependency>
-
-        <dependency>
-            <groupId>org.apache.tomcat.embed</groupId>
-            <artifactId>tomcat-embed-websocket</artifactId>
-            <version>9.0.72</version> <!-- 和你的 tomcat-embed-core 保持一致 -->
-        </dependency>
     </dependencies>
 </project>

+ 21 - 1
snowy-server/snowy-gateway-app/src/main/java/vip/xiaonuo/gateway/config/GatewayConfigure.java

@@ -35,6 +35,7 @@ import org.springframework.cloud.openfeign.support.SpringDecoder;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.context.annotation.Import;
+import org.springframework.context.annotation.Primary;
 import org.springframework.data.redis.connection.RedisConnectionFactory;
 import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
@@ -43,6 +44,10 @@ import org.springframework.http.MediaType;
 import org.springframework.http.converter.HttpMessageConverter;
 import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
 import org.springframework.stereotype.Component;
+import org.springframework.web.reactive.socket.client.TomcatWebSocketClient;
+import org.springframework.web.reactive.socket.client.WebSocketClient;
+import org.springframework.web.reactive.socket.server.RequestUpgradeStrategy;
+import org.springframework.web.reactive.socket.server.upgrade.TomcatRequestUpgradeStrategy;
 import vip.xiaonuo.auth.core.enums.SaClientTypeEnum;
 import vip.xiaonuo.auth.core.util.StpClientLoginUserUtil;
 import vip.xiaonuo.auth.core.util.StpClientUtil;
@@ -146,7 +151,8 @@ public class GatewayConfigure {
             /* 资源中心 */
             "/api/webapp/disk/resourcecentre/page",
             "/api/webapp/disk/resourcecentre/detail",
-            "/api/webapp/disk/courseauditrecord/addViewCount"
+            "/api/webapp/disk/courseauditrecord/addViewCount",
+            "/webSocket/**"
 
 
     };
@@ -408,4 +414,18 @@ public class GatewayConfigure {
                 });
     }
 
+    @Bean
+    @Primary
+    public RequestUpgradeStrategy requestUpgradeStrategy() {
+        return new TomcatRequestUpgradeStrategy();
+    }
+
+    @Bean
+    @Primary
+    public WebSocketClient webSocketClient() {
+        return new TomcatWebSocketClient();
+    }
+
+
+
 }