WS error handling
This commit is contained in:
@ -3,15 +3,12 @@ from starlette import status
|
||||
|
||||
from hopper.api.dependencies import get_game_engine
|
||||
from hopper.api.dto import (
|
||||
BoardDto,
|
||||
DestinationDto,
|
||||
ErrorResponseDto,
|
||||
GameInfoDto,
|
||||
MovePlayerResponseDto,
|
||||
PingResponse,
|
||||
PlayerDto,
|
||||
PlayerInfoResponseDto,
|
||||
PositionDto,
|
||||
StartGameRequestDto,
|
||||
StartGameResponseDto,
|
||||
)
|
||||
@ -25,7 +22,6 @@ router = APIRouter()
|
||||
|
||||
@router.get("/ping", response_model=PingResponse)
|
||||
async def ping() -> PingResponse:
|
||||
await ws_send_game_state()
|
||||
return PingResponse(
|
||||
message="Pong!",
|
||||
)
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
import json
|
||||
import logging
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
import websockets
|
||||
@ -18,13 +19,16 @@ async def ws_send_game_state() -> None:
|
||||
# avoid circular imports
|
||||
from hopper.api.dependencies import get_game_engine
|
||||
|
||||
async with create_ws_client() as websocket:
|
||||
engine = get_game_engine()
|
||||
try:
|
||||
async with create_ws_client() as websocket:
|
||||
engine = get_game_engine()
|
||||
|
||||
game_state = GameStateDto(
|
||||
board=engine.board,
|
||||
destination=engine.board.destination,
|
||||
players=engine.players,
|
||||
layers=engine.get_board_layout().layers,
|
||||
)
|
||||
await websocket.send(json.dumps(game_state.dict()))
|
||||
game_state = GameStateDto(
|
||||
board=engine.board,
|
||||
destination=engine.board.destination,
|
||||
players=engine.players,
|
||||
layers=engine.get_board_layout().layers,
|
||||
)
|
||||
await websocket.send(json.dumps(game_state.dict()))
|
||||
except OSError as ex:
|
||||
logging.error(f"Error sending WS state: {ex}")
|
||||
Reference in New Issue
Block a user