WS error handling

This commit is contained in:
Eden Kirin
2023-03-25 15:57:59 +01:00
parent 4b511c0cb8
commit ee1ce125ff
2 changed files with 13 additions and 13 deletions

View File

@ -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!",
)

View File

@ -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}")