浏览代码

添加跨域配置和新增转发事件

zqq 2 年之前
父节点
当前提交
56253e2e98

+ 13 - 0
mec-websocket/src/main/java/com/ym/mec/web/config/WebMvcConfig.java

@@ -9,6 +9,7 @@ import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.format.FormatterRegistry;
 import org.springframework.http.MediaType;
+import org.springframework.web.servlet.config.annotation.CorsRegistry;
 import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 
@@ -44,4 +45,16 @@ public class WebMvcConfig implements WebMvcConfigurer {
 		return new HttpMessageConverters(converter);
 	}
 
+	@Override
+	public void addCorsMappings(CorsRegistry registry) {
+		registry.addMapping("/**").allowedOrigins("*")
+				// 是否允许证书
+				.allowCredentials(true)
+				// 设置允许的方法
+				.allowedMethods("*")
+				// 设置允许的header属性
+				.allowedHeaders("*")
+				// 跨域允许时间
+				.maxAge(3600);
+	}
 }

+ 40 - 4
mec-websocket/src/main/java/com/ym/mec/web/handler/WhiteboardHandler.java

@@ -1,6 +1,7 @@
 package com.ym.mec.web.handler;
 
 import com.corundumstudio.socketio.AckRequest;
+import com.corundumstudio.socketio.BroadcastOperations;
 import com.corundumstudio.socketio.SocketIOClient;
 import com.corundumstudio.socketio.SocketIONamespace;
 import com.corundumstudio.socketio.annotation.OnConnect;
@@ -13,6 +14,8 @@ import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import org.springframework.stereotype.Component;
 
+import java.util.Collection;
+
 
 @Component
 @OnNamespace("/whiteboard")
@@ -22,14 +25,16 @@ public class WhiteboardHandler {
 
     private Logger logger = LogManager.getLogger(getClass().getName());
 
+
     /**
      * 添加connect事件,当客户端发起连接时调用,本文中将clientid与sessionid存入数据库
      * 方便后面发送消息时查找到对应的目标client
      */
     @OnConnect
     public void onConnect(SocketIOClient client) {
+        //发送初始化房间事件
+        client.sendEvent("init-room");
 
-        client.joinRoom("whiteboard");
     }
 
     /**
@@ -37,10 +42,16 @@ public class WhiteboardHandler {
      */
     @OnDisconnect
     public void onDisconnect(SocketIOClient client) {
-        client.leaveRoom("whiteboard");
         client.disconnect();
     }
 
+    /**
+     * test
+     *
+     * @param client
+     * @param data
+     * @param ackRequest
+     */
     @OnEvent(value = "message")
     public void onEvent(SocketIOClient client, ChatObject data, AckRequest ackRequest) {
         System.out.println("chat1 namespace " + namespace.getName());
@@ -49,8 +60,33 @@ public class WhiteboardHandler {
 
     }
 
-    @OnEvent(value = "send")
-    public void send(SocketIOClient client, ChatObject data, AckRequest ackRequest) {
+    @OnEvent(value = "join-room")
+    public void joinRoom(SocketIOClient client, Object data, AckRequest ackRequest) {
+        BroadcastOperations roomOperations = namespace.getRoomOperations("whiteboard");
+        Collection<SocketIOClient> clients = roomOperations.getClients();
+        client.joinRoom("whiteboard");
+        if (clients.size() > 1) {
+            for (SocketIOClient socketIOClient : clients) {
+                roomOperations.sendEvent("new-user", socketIOClient.getSessionId().toString());
+            }
+        } else {
+            //发送
+            client.sendEvent("first-in-room");
+        }
+        //发送
+        roomOperations.sendEvent("room-user-change", clients);
+
+    }
+
+    @OnEvent(value = "server-broadcast")
+    public void serverBroadcast(SocketIOClient client, Object data, AckRequest ackRequest) {
+        BroadcastOperations roomOperations = namespace.getRoomOperations("whiteboard");
+        roomOperations.sendEvent("client-broadcast", data);
+    }
 
+    @OnEvent(value = "server-volatile-broadcast")
+    public void serverVolatileBroadcast(SocketIOClient client, Object data, AckRequest ackRequest) {
+        BroadcastOperations roomOperations = namespace.getRoomOperations("whiteboard");
+        roomOperations.sendEvent("client-broadcast", data);
     }
 }