聊天室信息发送

This commit is contained in:
chanbook 2022-08-06 20:19:30 +08:00
parent 396d93123c
commit 0a1f878c6f
1 changed files with 13 additions and 4 deletions

View File

@ -15,6 +15,7 @@ import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.Iterator;
import java.util.Objects;
import java.util.Optional;
@ -37,10 +38,10 @@ public class ChatRoom {
*/
@OnOpen
public void onOpen(Session session, @PathParam("userId") Long userId) {
if (!getRoomCache().isExistUser(userId)) {
sendMessage("用户不在房间中,拒绝连接", session);
return;
}
// if (!getRoomCache().isExistUser(userId)) {
// sendMessage("用户不在房间中,拒绝连接", session);
// return;
// }
String sessionId = session.getId().toLowerCase();
sessionUserCache.put(sessionId, userId);
sessionCache.put(sessionId, session);
@ -65,6 +66,14 @@ public class ChatRoom {
}
Room room = getRoomCache().getRoomByUser(userId);
if (Objects.isNull(room)) {
for (Session sendSession : sessionCache) {
UserMessageVo messageVo = UserMessageVo.builder()
.id(userId)
.message(message)
.build();
String text = JSONUtil.toJsonStr(messageVo);
this.sendMessage(text, sendSession);
}
return;
}
Optional<RoomUserVo> userVo = room.getUserList().stream().filter(v -> v.getId().equals(userId)).findFirst();