fix: fix connection close bug

This commit is contained in:
wzp 2024-12-17 12:36:49 +08:00
parent 2f3f5806c3
commit 70eff21bd9
2 changed files with 9 additions and 2 deletions

View File

@ -63,6 +63,7 @@ public class ChessController {
Optional<Integer> i = jwtUtils.verifyTokenSafe(token);
if (i.isEmpty()) {
WebSocketUtils.sendPacket(new ErrorPacket("token验证失败"), session);
log.info("closed by token verifier");
session.close();
messageQueue.clear();
return;
@ -91,6 +92,7 @@ public class ChessController {
WebSocketUtils.sendPacket(roomByPlayer.getRoomInfo(users), session);
}
Session remove = sessions.remove(existsUser);
log.info("session closed by another session");
remove.close();
}
for (String s : messageQueue) {
@ -215,6 +217,7 @@ public class ChessController {
log.info("session closed {}", sessionId);
sessions.remove(sessionId);
users.remove(sessionId);
pingPong.remove(session);
}
@SneakyThrows
@ -222,6 +225,7 @@ public class ChessController {
public static void connectionManager() {
for (Session value : sessions.values()) {
if (!pingPong.getOrDefault(value, true)) {
log.info("closed by connectionManager");
value.close();
continue;
}

View File

@ -77,15 +77,18 @@ public class Room {
public void leave(Session session) {
String sessionId = session.getId();
if (Objects.nonNull(this.blackSession) && this.blackSession.getId().equals(sessionId)) {
WebSocketUtils.sendPacket(new PlayerLeavePacket(sessionId), whiteSession);
WebSocketUtils.sendPacketIfPossible(new PlayerLeavePacket(sessionId), whiteSession);
this.blackSession = null;
this.setState(RoomState.WAITING);
}
if (Objects.nonNull(this.whiteSession) && this.whiteSession.getId().equals(sessionId)) {
WebSocketUtils.sendPacket(new PlayerLeavePacket(sessionId), blackSession);
WebSocketUtils.sendPacketIfPossible(new PlayerLeavePacket(sessionId), blackSession);
this.whiteSession = null;
this.setState(RoomState.WAITING);
}
if (Objects.isNull(this.blackSession) && Objects.isNull(this.whiteSession)) {
this.setState(RoomState.CREATED);
}
}
public RoomInfoPacket getRoomInfo(Map<String, User> users) {