| 
					
				 | 
			
			
				@@ -0,0 +1,63 @@ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+package com.ym.mec.teacher.handler; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.alibaba.fastjson.JSONObject; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.ym.mec.teacher.interceptor.WebSocketChannelInterceptor; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.slf4j.Logger; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.slf4j.LoggerFactory; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.springframework.messaging.MessageChannel; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.springframework.messaging.SubscribableChannel; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.springframework.web.socket.CloseStatus; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.springframework.web.socket.WebSocketSession; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.springframework.web.socket.messaging.SubProtocolWebSocketHandler; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.util.Map; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.util.Objects; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+/** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ * @Author Joburgess 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ * @Date 2021/6/15 0015 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+public class CustomSubProtocolWebSocketHandler extends SubProtocolWebSocketHandler { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private final Logger LOGGER = LoggerFactory.getLogger(SubProtocolWebSocketHandler.class); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * Create a new {@code SubProtocolWebSocketHandler} for the given inbound and outbound channels. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @param clientInboundChannel the inbound {@code MessageChannel} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @param clientOutboundChannel the outbound {@code MessageChannel} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public CustomSubProtocolWebSocketHandler(MessageChannel clientInboundChannel, SubscribableChannel clientOutboundChannel) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        super(clientInboundChannel, clientOutboundChannel); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * 建立连接成功之后执行的方法 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @param session 处理器 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @throws Exception 异常 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Override 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public void afterConnectionEstablished(WebSocketSession session) throws Exception { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        LOGGER.info("afterConnectionEstablished: connection established -- session --> {}", session); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (Objects.isNull(session) || Objects.isNull(session.getPrincipal())) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            LOGGER.warn("afterConnectionEstablished: session is null or user is null -- session --> {}", session); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            return; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        Map<String, Object> attributes = session.getAttributes(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        String sessionId = session.getId(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        LOGGER.info("afterConnectionEstablished: attributes -->{}, sessionId --> {}", JSONObject.toJSONString(attributes), session); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        attributes.put("clientId", sessionId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        super.afterConnectionEstablished(session); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * 在关闭连接之后调用 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @param session WebSocket处理器 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @param closeStatus 状态 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @throws Exception 异常 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Override 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        LOGGER.info("afterConnectionClosed: close -- closeStatus --> {}", closeStatus); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        super.afterConnectionClosed(session, closeStatus); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+} 
			 |