| 
					
				 | 
			
			
				@@ -0,0 +1,75 @@ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+package com.steerinfo.ems.Utils; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.springframework.stereotype.Component; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.springframework.web.bind.annotation.RestController; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import javax.websocket.OnClose; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import javax.websocket.OnMessage; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import javax.websocket.OnOpen; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import javax.websocket.Session; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import javax.websocket.server.PathParam; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import javax.websocket.server.ServerEndpoint; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.util.HashMap; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.util.Map; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.util.concurrent.CopyOnWriteArraySet; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ * @author Shadow 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ * @create 2021-09-18 14:54 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ * @project xt-ems-api 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+@ServerEndpoint("/websocket/{shopId}") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+@RestController 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+@Component 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+public class WebSocket { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private Session session; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private static CopyOnWriteArraySet<WebSocket> webSockets =new CopyOnWriteArraySet<>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private static Map<String,Session> sessionPool = new HashMap<String,Session>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @OnOpen 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public void onOpen(Session session, @PathParam(value="shopId")String shopId) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        this.session = session; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        webSockets.add(this); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        sessionPool.put(shopId, session); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        System.out.println("【websocket消息】有新的连接,总数为:"+webSockets.size()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @OnClose 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public void onClose() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        webSockets.remove(this); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        System.out.println("【websocket消息】连接断开,总数为:"+webSockets.size()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @OnMessage 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public void onMessage(String message) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        System.out.println("【websocket消息】收到客户端消息:"+message); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public void sendAllMessage(String message) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        for(WebSocket webSocket : webSockets) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            System.out.println("【websocket消息】广播消息:"+message); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                webSocket.session.getAsyncRemote().sendText(message); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } catch (Exception e) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                e.printStackTrace(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public void sendOneMessage(String shopId, String message) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        Session session = sessionPool.get(shopId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (session != null) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                session.getAsyncRemote().sendText(message); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } catch (Exception e) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                e.printStackTrace(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+} 
			 |