Commit 501b67f3 by 段启岩

消息更新

parent 5ce23bc4
...@@ -9,10 +9,13 @@ import io.netty.handler.codec.http.websocketx.PongWebSocketFrame; ...@@ -9,10 +9,13 @@ import io.netty.handler.codec.http.websocketx.PongWebSocketFrame;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy; import javax.annotation.PreDestroy;
import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
/** /**
...@@ -25,7 +28,7 @@ public class SocketIOServiceImpl implements SocketIOService { ...@@ -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; private SocketIOServer socketIOServer;
...@@ -81,7 +84,7 @@ public class SocketIOServiceImpl implements SocketIOService { ...@@ -81,7 +84,7 @@ public class SocketIOServiceImpl implements SocketIOService {
} }
// 将session存入内存 // 将session存入内存
CLIENT_MAP.put(userId, client); putClient(userId, client);
log.info("user {} connected to the im server", userId); log.info("user {} connected to the im server", userId);
// client.sendEvent(PUSH_EVENT, "hello, 欢迎连接服务器!"); // client.sendEvent(PUSH_EVENT, "hello, 欢迎连接服务器!");
}); });
...@@ -93,7 +96,10 @@ public class SocketIOServiceImpl implements SocketIOService { ...@@ -93,7 +96,10 @@ public class SocketIOServiceImpl implements SocketIOService {
if (null != token) { if (null != token) {
String userId = tokenManager.getUserId(token); String userId = tokenManager.getUserId(token);
if (null != userId) { 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 { ...@@ -114,11 +120,17 @@ public class SocketIOServiceImpl implements SocketIOService {
@Override @Override
public void pushMessage(String userId, Object message) { public void pushMessage(String userId, Object message) {
SocketIOClient client = CLIENT_MAP.get(userId); Set<SocketIOClient> clients = CLIENT_MAP.get(userId);
log.info("send message to {}", client); log.info("send message to {}", userId);
if (null != client) { if (!CollectionUtils.isEmpty(clients)) {
client.sendEvent(PUSH_EVENT, message); 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