|
|
@@ -0,0 +1,127 @@
|
|
|
+package org.springblade.client.handler;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Random;
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
+
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang.reflect.FieldUtils;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.web.socket.*;
|
|
|
+import org.springframework.web.socket.handler.AbstractWebSocketHandler;
|
|
|
+import org.springframework.web.socket.sockjs.transport.session.WebSocketServerSockJsSession;
|
|
|
+@Slf4j
|
|
|
+@Component
|
|
|
+public class WebSocketHandler extends AbstractWebSocketHandler {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 存储sessionId和webSocketSession
|
|
|
+ * 需要注意的是,webSocketSession没有提供无参构造,不能进行序列化,也就不能通过redis存储
|
|
|
+ * 在分布式系统中,要想别的办法实现webSocketSession共享
|
|
|
+ */
|
|
|
+ private static Map<String, WebSocketSession> sessionMap = new ConcurrentHashMap<>();
|
|
|
+ private static Map<String, String> userMap = new ConcurrentHashMap<>();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取sessionId
|
|
|
+ */
|
|
|
+ private String getSessionId(WebSocketSession session) {
|
|
|
+ if (session instanceof WebSocketServerSockJsSession) {
|
|
|
+ // sock js 连接
|
|
|
+ try {
|
|
|
+ return ((WebSocketSession) FieldUtils.readField(session, "webSocketSession", true)).getId();
|
|
|
+ } catch (IllegalAccessException e) {
|
|
|
+ throw new RuntimeException("get sessionId error");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return session.getId();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * webSocket连接创建后调用
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void afterConnectionEstablished(WebSocketSession session) {
|
|
|
+ // 获取参数
|
|
|
+ String user = String.valueOf(session.getAttributes().get("user"));
|
|
|
+ String sessionId = getSessionId(session);
|
|
|
+ userMap.put(user, getSessionId(session));
|
|
|
+ sessionMap.put(sessionId, session);
|
|
|
+ log.info("【当前socket链接人数】:==>"+sessionMap.size());
|
|
|
+ send(user);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 接收到消息会调用
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
|
|
|
+ if (message instanceof TextMessage) {
|
|
|
+
|
|
|
+ } else if (message instanceof BinaryMessage) {
|
|
|
+
|
|
|
+ } else if (message instanceof PongMessage) {
|
|
|
+
|
|
|
+ } else {
|
|
|
+ System.out.println("Unexpected WebSocket message type: " + message);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 连接出错会调用
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void handleTransportError(WebSocketSession session, Throwable exception) {
|
|
|
+ sessionMap.remove(getSessionId(session));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 连接关闭会调用
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void afterConnectionClosed(WebSocketSession session, CloseStatus status) {
|
|
|
+ sessionMap.remove(getSessionId(session));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean supportsPartialMessages() {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 后端发送消息
|
|
|
+ */
|
|
|
+ public void sendMessage(String user, String message) {
|
|
|
+ String sessionId = userMap.get(user);
|
|
|
+ WebSocketSession session = sessionMap.get(sessionId);
|
|
|
+ try {
|
|
|
+ log.info("【websocket消息】 单点消息:"+message);
|
|
|
+ session.sendMessage(new TextMessage(message));
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void send(String user)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ while(true)
|
|
|
+ {
|
|
|
+ int max=175;
|
|
|
+ int min=10;
|
|
|
+ Random random = new Random();
|
|
|
+ int x = random.nextInt(max) % (max - min + 1) + min;
|
|
|
+ int y = random.nextInt(max) % (max - min + 1) + min;
|
|
|
+ String mark="["+x+","+y+"]";
|
|
|
+ sendMessage(user,mark);
|
|
|
+ Thread.sleep(6000);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (InterruptedException e)
|
|
|
+ {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|