|
@@ -0,0 +1,122 @@
|
|
|
+package com.aizuda.boot.modules.ws;
|
|
|
+
|
|
|
+import com.baomidou.kisso.security.token.SSOToken;
|
|
|
+import jakarta.websocket.*;
|
|
|
+import jakarta.websocket.server.ServerEndpoint;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.collections.MapUtils;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
+
|
|
|
+/**
|
|
|
+ * websocket 消息服务端
|
|
|
+ *
|
|
|
+ * @author 青苗
|
|
|
+ * @since 2023-10-03
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Component
|
|
|
+@ServerEndpoint(value = "/ws", configurator = WsEndpointConfigurator.class)
|
|
|
+public class WsMessageServer {
|
|
|
+ private static Map<String, WsSession> ONLINE_SESSIONS = new ConcurrentHashMap<>();
|
|
|
+ private static Map<String, String> TOKEN_SESSION_IDS = new ConcurrentHashMap<>();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 客户端打开连接
|
|
|
+ */
|
|
|
+ @OnOpen
|
|
|
+ public void onOpen(Session session) {
|
|
|
+ if (null != ONLINE_SESSIONS.get(session.getId())) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ SSOToken ssoToken = null;
|
|
|
+ Map<String, Object> userProperties = session.getUserProperties();
|
|
|
+ if (MapUtils.isNotEmpty(userProperties)) {
|
|
|
+ String accessToken = (String) userProperties.get("accessToken");
|
|
|
+ if (StringUtils.isNotBlank(accessToken)) {
|
|
|
+ if (StringUtils.isNotBlank(accessToken)) {
|
|
|
+ ssoToken = SSOToken.parser(accessToken, false);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (null == ssoToken) {
|
|
|
+ try {
|
|
|
+ // 未授权关闭接入会话
|
|
|
+ session.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ }
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ final String sessionId = session.getId();
|
|
|
+ ONLINE_SESSIONS.put(sessionId, new WsSession(ssoToken.getId(), session));
|
|
|
+ TOKEN_SESSION_IDS.put(ssoToken.getId(), sessionId);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 客户端发送消息
|
|
|
+ *
|
|
|
+ * @param session 连接会话
|
|
|
+ * @param message 客户端发送过来的消息内容
|
|
|
+ */
|
|
|
+ @OnMessage
|
|
|
+ public void onMessage(Session session, String message) {
|
|
|
+ if (Objects.equals("ping", message)) {
|
|
|
+ try {
|
|
|
+ this.sendText(session, "pong");
|
|
|
+ } catch (IOException e) {
|
|
|
+ // to do nothing
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 关闭连接
|
|
|
+ */
|
|
|
+ @OnClose
|
|
|
+ public void onClose(Session session) {
|
|
|
+ WsSession wsSession = ONLINE_SESSIONS.get(session.getId());
|
|
|
+ if (null != wsSession) {
|
|
|
+ ONLINE_SESSIONS.remove(session.getId());
|
|
|
+ // 删除在线用户会话
|
|
|
+ // this.getUserSessionService().removeBySid(session.getId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通信发生异常
|
|
|
+ */
|
|
|
+ @OnError
|
|
|
+ public void onError(Session session, Throwable error) {
|
|
|
+ log.info("WebSocket onError sessionId={}", session.getId());
|
|
|
+ error.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送文本消息给指定 userId 连接客户端
|
|
|
+ *
|
|
|
+ * @param userId 用户ID
|
|
|
+ * @param text 文本消息
|
|
|
+ */
|
|
|
+ public void sendText(String userId, String text) throws IOException {
|
|
|
+ String sessionId = TOKEN_SESSION_IDS.get(userId);
|
|
|
+ WsSession wsSession = ONLINE_SESSIONS.get(sessionId);
|
|
|
+ if (null != wsSession) {
|
|
|
+ this.sendText(wsSession.getSession(), text);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送文本消息
|
|
|
+ *
|
|
|
+ * @param session 连接会话
|
|
|
+ * @param text 文本消息
|
|
|
+ */
|
|
|
+ private void sendText(Session session, String text) throws IOException {
|
|
|
+ session.getBasicRemote().sendText(text);
|
|
|
+ }
|
|
|
+}
|