|
@@ -2,8 +2,10 @@ package com.fjhx.service;
|
|
|
|
|
|
import cn.hutool.extra.spring.SpringUtil;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
-import com.fjhx.entity.SendEntity;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.fjhx.entity.MessageEntity;
|
|
|
import com.fjhx.event.*;
|
|
|
+import lombok.Getter;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.context.ApplicationContext;
|
|
|
import org.springframework.stereotype.Component;
|
|
@@ -19,6 +21,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
|
|
@ServerEndpoint("/webStock/{userId}")
|
|
|
@Component
|
|
|
@Slf4j
|
|
|
+@Getter
|
|
|
public class WebSocketServer {
|
|
|
|
|
|
private static final ApplicationContext applicationContext = SpringUtil.getApplicationContext();
|
|
@@ -26,7 +29,7 @@ public class WebSocketServer {
|
|
|
/**
|
|
|
* concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。
|
|
|
*/
|
|
|
- private static final ConcurrentHashMap<String, WebSocketServer> webSocketMap = new ConcurrentHashMap<>();
|
|
|
+ private static final ConcurrentHashMap<String, ConcurrentHashMap<String, WebSocketServer>> webSocketMap = new ConcurrentHashMap<>();
|
|
|
/**
|
|
|
* 与某个客户端的连接会话,需要通过它来给客户端发送数据
|
|
|
*/
|
|
@@ -35,6 +38,10 @@ public class WebSocketServer {
|
|
|
* 接收userId
|
|
|
*/
|
|
|
private String userId;
|
|
|
+ /**
|
|
|
+ * sessionId
|
|
|
+ */
|
|
|
+ private String sessionId;
|
|
|
|
|
|
/**
|
|
|
* 连接建立成功调用的方法
|
|
@@ -42,12 +49,18 @@ public class WebSocketServer {
|
|
|
@OnOpen
|
|
|
public void onOpen(@RequestBody Session session, @PathParam("userId") String userId) {
|
|
|
this.session = session;
|
|
|
+ this.sessionId = session.getId();
|
|
|
this.userId = userId;
|
|
|
- webSocketMap.remove(userId);
|
|
|
- webSocketMap.put(userId, this);
|
|
|
+
|
|
|
+ synchronized (this) {
|
|
|
+ ConcurrentHashMap<String, WebSocketServer> sessionIdWebSocketServerMap =
|
|
|
+ webSocketMap.computeIfAbsent(userId, k -> new ConcurrentHashMap<>());
|
|
|
+
|
|
|
+ sessionIdWebSocketServerMap.put(sessionId, this);
|
|
|
+ }
|
|
|
|
|
|
// 发布建立连接连接事件
|
|
|
- applicationContext.publishEvent(new WebSocketOnOpenEvent(this, userId));
|
|
|
+ applicationContext.publishEvent(new WebSocketOnOpenEvent(this));
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -58,7 +71,7 @@ public class WebSocketServer {
|
|
|
webSocketMap.remove(userId);
|
|
|
|
|
|
// 发布建立关闭连接事件
|
|
|
- applicationContext.publishEvent(new WebSocketOnCloseEvent(this, userId));
|
|
|
+ applicationContext.publishEvent(new WebSocketOnCloseEvent(this));
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -69,7 +82,7 @@ public class WebSocketServer {
|
|
|
@OnMessage
|
|
|
public void onMessage(String message) {
|
|
|
// 推送消息事件
|
|
|
- applicationContext.publishEvent(new WebSocketOnMessageEvent(this, userId, message));
|
|
|
+ applicationContext.publishEvent(new WebSocketOnMessageEvent(this, message));
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -78,7 +91,7 @@ public class WebSocketServer {
|
|
|
@OnError
|
|
|
public void onError(Throwable error) {
|
|
|
// 发布连接异常事件
|
|
|
- applicationContext.publishEvent(new WebSocketOnErrorEvent(this, userId, error));
|
|
|
+ applicationContext.publishEvent(new WebSocketOnErrorEvent(this, error));
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -86,11 +99,12 @@ public class WebSocketServer {
|
|
|
*
|
|
|
* @param message 消息内容
|
|
|
*/
|
|
|
- public void sendMessage(String message) {
|
|
|
- SendEntity sendEntity = new SendEntity();
|
|
|
+ public void sendMessage(JSONObject message) {
|
|
|
+ MessageEntity sendEntity = new MessageEntity();
|
|
|
sendEntity.setUserId(userId);
|
|
|
+ sendEntity.setSessionId(sessionId);
|
|
|
sendEntity.setCreateTime(new Date());
|
|
|
- sendEntity.setMessage(message);
|
|
|
+ sendEntity.setData(message);
|
|
|
|
|
|
try {
|
|
|
this.session.getBasicRemote().sendText(JSON.toJSONString(sendEntity));
|
|
@@ -106,15 +120,18 @@ public class WebSocketServer {
|
|
|
* @param userId 发送用户
|
|
|
* @param message 消息内容
|
|
|
*/
|
|
|
- public static void sendInfo(String userId, String message) {
|
|
|
- WebSocketServer webSocketServer = webSocketMap.get(userId);
|
|
|
+ public static void sendInfo(String userId, JSONObject message) {
|
|
|
+ ConcurrentHashMap<String, WebSocketServer> sessionIdWebSocketServerMap = webSocketMap.get(userId);
|
|
|
|
|
|
- if (webSocketServer == null) {
|
|
|
+ if (sessionIdWebSocketServerMap == null) {
|
|
|
applicationContext.publishEvent(new WebSocketUserOfflineEvent(userId, message));
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- webSocketServer.sendMessage(message);
|
|
|
+ for (WebSocketServer webSocketServer : sessionIdWebSocketServerMap.values()) {
|
|
|
+ webSocketServer.sendMessage(message);
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -122,7 +139,7 @@ public class WebSocketServer {
|
|
|
*
|
|
|
* @param message 消息内容
|
|
|
*/
|
|
|
- public static void sendInfoGroup(String message) {
|
|
|
+ public static void sendInfoGroup(JSONObject message) {
|
|
|
if (webSocketMap.size() > 0) {
|
|
|
for (String userId : WebSocketServer.webSocketMap.keySet()) {
|
|
|
sendInfo(userId, message);
|