Commit 501b67f3 by 段启岩

消息更新

parent 5ce23bc4
......@@ -9,10 +9,13 @@ import io.netty.handler.codec.http.websocketx.PongWebSocketFrame;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
/**
......@@ -25,7 +28,7 @@ public class SocketIOServiceImpl implements SocketIOService {
/**
* 用来存已连接的客户端
*/
private static final Map<String, SocketIOClient> CLIENT_MAP = new ConcurrentHashMap<>();
private static final Map<String, Set<SocketIOClient>> CLIENT_MAP = new ConcurrentHashMap<>();
private SocketIOServer socketIOServer;
......@@ -81,7 +84,7 @@ public class SocketIOServiceImpl implements SocketIOService {
}
// 将session存入内存
CLIENT_MAP.put(userId, client);
putClient(userId, client);
log.info("user {} connected to the im server", userId);
// client.sendEvent(PUSH_EVENT, "hello, 欢迎连接服务器!");
});
......@@ -93,7 +96,10 @@ public class SocketIOServiceImpl implements SocketIOService {
if (null != token) {
String userId = tokenManager.getUserId(token);
if (null != userId) {
// CLIENT_MAP.remove(userId);
Set<SocketIOClient> clients = CLIENT_MAP.get(userId);
if (!CollectionUtils.isEmpty(clients)) {
clients.remove(client);
}
}
}
});
......@@ -114,11 +120,17 @@ public class SocketIOServiceImpl implements SocketIOService {
@Override
public void pushMessage(String userId, Object message) {
SocketIOClient client = CLIENT_MAP.get(userId);
log.info("send message to {}", client);
if (null != client) {
client.sendEvent(PUSH_EVENT, message);
Set<SocketIOClient> clients = CLIENT_MAP.get(userId);
log.info("send message to {}", userId);
if (!CollectionUtils.isEmpty(clients)) {
clients.forEach(socketIOClient -> {
socketIOClient.sendEvent(PUSH_EVENT, message);
});;
}
}
private void putClient(String userId, SocketIOClient socketIOClient) {
CLIENT_MAP.getOrDefault(userId, new HashSet<>()).add(socketIOClient);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment