Browse Source

SSE基础长链接服务调整

wangqi49 3 months ago
parent
commit
f4c821fc2a

+ 0 - 6
webchat-connect/pom.xml

@@ -40,12 +40,6 @@
             <version>1.1</version>
             <scope>provided</scope>
         </dependency>
-        <!-- tomcat服务队websocket的支持 -->
-        <dependency>
-            <groupId>org.apache.tomcat.embed</groupId>
-            <artifactId>tomcat-embed-websocket</artifactId>
-            <version>9.0.33</version>
-        </dependency>
     </dependencies>
 
     <build>

+ 4 - 0
webchat-connect/src/main/java/com/webchat/connect/WebchatConnectApplication.java

@@ -2,8 +2,12 @@ package com.webchat.connect;
 
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.cloud.openfeign.EnableFeignClients;
+import org.springframework.context.annotation.ComponentScan;
 
+@ComponentScan("com.webchat")
 @SpringBootApplication
+@EnableFeignClients("com.webchat.rmi")
 public class WebchatConnectApplication {
 
     public static void main(String[] args) {

+ 30 - 0
webchat-connect/src/main/java/com/webchat/connect/config/CorsConfig.java

@@ -0,0 +1,30 @@
+package com.webchat.connect.config;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.cors.CorsConfiguration;
+import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
+import org.springframework.web.filter.CorsFilter;
+
+@Configuration
+public class CorsConfig {
+
+    // 当前跨域请求最大有效时长。这里默认1天
+    private static final long MAX_AGE = 24 * 60 * 60;
+
+    private CorsConfiguration buildConfig() {
+        CorsConfiguration corsConfiguration = new CorsConfiguration();
+        corsConfiguration.addAllowedOrigin("*"); // 1 设置访问源地址
+        corsConfiguration.addAllowedHeader("*"); // 2 设置访问源请求头
+        corsConfiguration.addAllowedMethod("*"); // 3 设置访问源请求方法
+        corsConfiguration.setMaxAge(MAX_AGE);
+        return corsConfiguration;
+    }
+
+    @Bean
+    public CorsFilter corsFilter() {
+        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
+        source.registerCorsConfiguration("/**", buildConfig()); // 4 对接口配置跨域设置
+        return new CorsFilter(source);
+    }
+}

+ 14 - 0
webchat-connect/src/main/java/com/webchat/connect/config/WebSocketConfig.java

@@ -0,0 +1,14 @@
+package com.webchat.connect.config;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.socket.server.standard.ServerEndpointExporter;
+
+@Configuration
+public class WebSocketConfig {
+
+    @Bean
+    public ServerEndpointExporter serverEndpointExporter() {
+        return new ServerEndpointExporter();
+    }
+}

+ 1 - 4
webchat-connect/src/main/java/com/webchat/connect/javasse/EventStreamConnectController.java

@@ -25,10 +25,7 @@ public class EventStreamConnectController {
     @GetMapping(path = "/stream", produces = "text/event-stream")
     public SseEmitter stream(HttpServletRequest request) {
         String biz = request.getParameter("biz");
-        String userId = SessionHelper.getCurrentUserId();
-        if (StringUtils.isBlank(userId)) {
-            throw new BusinessException("对话Stream流长链接获取异常,用户未登录!");
-        }
+        String userId = request.getParameter("userId");
         return SseEmitterHelper.get(biz, userId);
     }
 }

+ 2 - 2
webchat-connect/src/main/java/com/webchat/connect/websocket/ChatWebSocketEndPoint.java

@@ -23,7 +23,7 @@ import java.util.concurrent.ConcurrentHashMap;
 @Slf4j
 @Component
 @ServerEndpoint("/connect-service/ws/chat/{userId}")
-public class ChatWebSocketEndPoint {
+public class ChatWebSocketEndPoint{
 
     /**
      * 在线人数
@@ -82,7 +82,7 @@ public class ChatWebSocketEndPoint {
      */
     @OnMessage
     public void onMessage(String message, Session session) {
-        if ("heartbeat".equals(message)) {
+        if ("ping".equals(message)) {
             // 心跳检测
             clients.get(userId).session.getAsyncRemote().sendText("ok");
             return;