fix: fix connection automatic close error
This commit is contained in:
parent
ab24dfa1d1
commit
74a1dd3f7a
@ -145,56 +145,66 @@ public class ChessController {
|
||||
}
|
||||
}
|
||||
if (payloadJson == null) return new ErrorPacket("请求类型参数错误!");
|
||||
Object payload = payloadJson.to(packetType);
|
||||
String sessionId = session.getId();
|
||||
if (payload instanceof PlayerJoinRequest joinRequest) {
|
||||
Room roomByPlayer = getRoomByPlayer(sessionId);
|
||||
if (roomByPlayer != null) return new ErrorPacket("你已经加入过一个房间了");
|
||||
Room room = rooms.get(joinRequest.roomId());
|
||||
if (room == null) return new ErrorPacket("未知的房间");
|
||||
Optional<String> join = room.join(session, users.get(sessionId));
|
||||
if (join.isPresent()) return new ErrorPacket(join.get());
|
||||
room.broadcast(room.getRoomInfo(users));
|
||||
}
|
||||
if (payload instanceof RoomListRequest) {
|
||||
Collection<Room> values = rooms.values();
|
||||
return new RoomListPacket(values);
|
||||
}
|
||||
if (payload instanceof CreateRoomRequest) {
|
||||
Room room = new Room();
|
||||
UUID roomId = room.getId();
|
||||
rooms.put(roomId, room);
|
||||
return new RoomCreatedPacket(roomId);
|
||||
}
|
||||
if (payload instanceof PlaceChessPieceRequest placeChessPieceRequest) {
|
||||
int x = placeChessPieceRequest.x();
|
||||
int y = placeChessPieceRequest.y();
|
||||
Room roomByPlayer = getRoomByPlayer(sessionId);
|
||||
if (roomByPlayer == null) return new ErrorPacket("你还没加入房间呢");
|
||||
Optional<String> s = roomByPlayer.downPiece(x, y, sessionId);
|
||||
if (s.isPresent()) return new ErrorPacket(s.get());
|
||||
roomByPlayer.broadcast(roomByPlayer.getRoomInfo(users));
|
||||
Optional<Room.WinInfo> winInfo = roomByPlayer.isWin();
|
||||
winInfo.ifPresent(info -> {
|
||||
roomByPlayer.broadcast(new HasPlayerWinPacket(info));
|
||||
if (info.isWhite()) {
|
||||
WebSocketUtils.sendPacketIfPossible(new PlayerWinPacket(), roomByPlayer.getWhiteSession());
|
||||
WebSocketUtils.sendPacketIfPossible(new PlayerLosePacket(), roomByPlayer.getBlackSession());
|
||||
} else {
|
||||
WebSocketUtils.sendPacketIfPossible(new PlayerWinPacket(), roomByPlayer.getBlackSession());
|
||||
WebSocketUtils.sendPacketIfPossible(new PlayerLosePacket(), roomByPlayer.getWhiteSession());
|
||||
}
|
||||
});
|
||||
}
|
||||
if (payload instanceof ResetRoomRequest) {
|
||||
Room roomByPlayer = getRoomByPlayer(sessionId);
|
||||
if (roomByPlayer == null) return new ErrorPacket("你还没加入房间呢");
|
||||
roomByPlayer.requestRestart(session);
|
||||
roomByPlayer.broadcast(roomByPlayer.getRoomInfo(users));
|
||||
Object payload;
|
||||
try {
|
||||
payload = payloadJson.to(packetType);
|
||||
} catch (Exception e) {
|
||||
return new ErrorPacket("请求payload解析失败!");
|
||||
}
|
||||
if (payload == null) return null;
|
||||
return handlePacket(payload, session);
|
||||
} catch (ClassNotFoundException e) {
|
||||
return new ErrorPacket("错误的请求类型");
|
||||
}
|
||||
}
|
||||
|
||||
public Object handlePacket(Object payload, Session session) {
|
||||
String sessionId = session.getId();
|
||||
if (payload instanceof PlayerJoinRequest joinRequest) {
|
||||
Room roomByPlayer = getRoomByPlayer(sessionId);
|
||||
if (roomByPlayer != null) return new ErrorPacket("你已经加入过一个房间了");
|
||||
Room room = rooms.get(joinRequest.roomId());
|
||||
if (room == null) return new ErrorPacket("未知的房间");
|
||||
Optional<String> join = room.join(session, users.get(sessionId));
|
||||
if (join.isPresent()) return new ErrorPacket(join.get());
|
||||
room.broadcast(room.getRoomInfo(users));
|
||||
}
|
||||
if (payload instanceof RoomListRequest) {
|
||||
Collection<Room> values = rooms.values();
|
||||
return new RoomListPacket(values);
|
||||
}
|
||||
if (payload instanceof CreateRoomRequest) {
|
||||
Room room = new Room();
|
||||
UUID roomId = room.getId();
|
||||
rooms.put(roomId, room);
|
||||
return new RoomCreatedPacket(roomId);
|
||||
}
|
||||
if (payload instanceof PlaceChessPieceRequest placeChessPieceRequest) {
|
||||
int x = placeChessPieceRequest.x();
|
||||
int y = placeChessPieceRequest.y();
|
||||
Room roomByPlayer = getRoomByPlayer(sessionId);
|
||||
if (roomByPlayer == null) return new ErrorPacket("你还没加入房间呢");
|
||||
Optional<String> s = roomByPlayer.downPiece(x, y, sessionId);
|
||||
if (s.isPresent()) return new ErrorPacket(s.get());
|
||||
roomByPlayer.broadcast(roomByPlayer.getRoomInfo(users));
|
||||
Optional<Room.WinInfo> winInfo = roomByPlayer.isWin();
|
||||
winInfo.ifPresent(info -> {
|
||||
roomByPlayer.broadcast(new HasPlayerWinPacket(info));
|
||||
if (info.isWhite()) {
|
||||
WebSocketUtils.sendPacketIfPossible(new PlayerWinPacket(), roomByPlayer.getWhiteSession());
|
||||
WebSocketUtils.sendPacketIfPossible(new PlayerLosePacket(), roomByPlayer.getBlackSession());
|
||||
} else {
|
||||
WebSocketUtils.sendPacketIfPossible(new PlayerWinPacket(), roomByPlayer.getBlackSession());
|
||||
WebSocketUtils.sendPacketIfPossible(new PlayerLosePacket(), roomByPlayer.getWhiteSession());
|
||||
}
|
||||
});
|
||||
}
|
||||
if (payload instanceof ResetRoomRequest) {
|
||||
Room roomByPlayer = getRoomByPlayer(sessionId);
|
||||
if (roomByPlayer == null) return new ErrorPacket("你还没加入房间呢");
|
||||
roomByPlayer.requestRestart(session);
|
||||
roomByPlayer.broadcast(roomByPlayer.getRoomInfo(users));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -4,13 +4,15 @@ import com.alibaba.fastjson2.JSONObject;
|
||||
import jakarta.annotation.Nullable;
|
||||
import jakarta.websocket.Session;
|
||||
import lombok.NonNull;
|
||||
import lombok.SneakyThrows;
|
||||
import org.mmga.clubs.entities.chess.packet.BaseWebSocketPacket;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class WebSocketUtils {
|
||||
@SneakyThrows
|
||||
public static void sendJsonMessage(@NonNull JSONObject message, @NonNull Session session) {
|
||||
session.getAsyncRemote().sendText(message.toJSONString());
|
||||
session.getBasicRemote().sendText(message.toJSONString());
|
||||
}
|
||||
|
||||
public static void sendMessage(@NonNull Object object, @NonNull Session session) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user