Merge branch 'master' into docker_package

This commit is contained in:
wangliwen 2022-07-21 14:28:08 +08:00
commit 53589991ea
1 changed files with 11 additions and 5 deletions

View File

@ -16,6 +16,8 @@ import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* WebSocket服务
@ -24,6 +26,7 @@ import java.util.concurrent.ConcurrentHashMap;
@Component
@ServerEndpoint(value = "/websocket", configurator = WebSocketConfig.class)
public class WebSocketServer {
private static final ExecutorService executor = Executors.newWorkStealingPool();
private static final Logger logger = LoggerFactory.getLogger(WebSocketServer.class);
public WebSocketServer() {
@ -38,7 +41,6 @@ public class WebSocketServer {
@OnOpen
public void open(Session session) {
Long userId = (Long) session.getUserProperties().get(Constant.USER_KEY);
servers.put(session.getId(), new WebSocketData(userId, session));
}
@ -46,7 +48,7 @@ public class WebSocketServer {
public void onClose(Session session) {
//客户端断开连接
servers.remove(session.getId());
log.debug("websocket close, session id" + session.getId());
log.info("websocket close, session id" + session.getId());
}
@OnError
@ -57,7 +59,7 @@ public class WebSocketServer {
@OnMessage
public void onMessage(Session session, String msg) {
log.info("session id: " + session.getId() + "userId{} message" + msg, servers.get(session.getId()).getUserId());
log.info("session id: " + session.getId() + " userId{} message" + msg, servers.get(session.getId()).getUserId());
}
/**
@ -79,7 +81,9 @@ public class WebSocketServer {
public void sendMessage(Long userId, MessageData<?> message) {
servers.values().forEach(info -> {
if (userId.equals(info.getUserId())) {
sendMessage(info.getSession(), message);
executor.execute(() -> {
sendMessage(info.getSession(), message);
});
}
});
}
@ -90,7 +94,9 @@ public class WebSocketServer {
* @param message 消息内容
*/
public void sendMessageAll(MessageData<?> message) {
servers.values().forEach(info -> sendMessage(info.getSession(), message));
servers.values().forEach(info -> executor.execute(() -> {
sendMessage(info.getSession(), message);
}));
}
public void sendMessage(Session session, MessageData<?> message) {