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.dependencies import get_game_engine
|
||||||
from hopper.api.dto import (
|
from hopper.api.dto import (
|
||||||
BoardDto,
|
|
||||||
DestinationDto,
|
DestinationDto,
|
||||||
ErrorResponseDto,
|
ErrorResponseDto,
|
||||||
GameInfoDto,
|
GameInfoDto,
|
||||||
MovePlayerResponseDto,
|
MovePlayerResponseDto,
|
||||||
PingResponse,
|
PingResponse,
|
||||||
PlayerDto,
|
|
||||||
PlayerInfoResponseDto,
|
PlayerInfoResponseDto,
|
||||||
PositionDto,
|
|
||||||
StartGameRequestDto,
|
StartGameRequestDto,
|
||||||
StartGameResponseDto,
|
StartGameResponseDto,
|
||||||
)
|
)
|
||||||
@ -25,7 +22,6 @@ router = APIRouter()
|
|||||||
|
|
||||||
@router.get("/ping", response_model=PingResponse)
|
@router.get("/ping", response_model=PingResponse)
|
||||||
async def ping() -> PingResponse:
|
async def ping() -> PingResponse:
|
||||||
await ws_send_game_state()
|
|
||||||
return PingResponse(
|
return PingResponse(
|
||||||
message="Pong!",
|
message="Pong!",
|
||||||
)
|
)
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
import json
|
import json
|
||||||
|
import logging
|
||||||
from contextlib import asynccontextmanager
|
from contextlib import asynccontextmanager
|
||||||
|
|
||||||
import websockets
|
import websockets
|
||||||
@ -18,6 +19,7 @@ async def ws_send_game_state() -> None:
|
|||||||
# avoid circular imports
|
# avoid circular imports
|
||||||
from hopper.api.dependencies import get_game_engine
|
from hopper.api.dependencies import get_game_engine
|
||||||
|
|
||||||
|
try:
|
||||||
async with create_ws_client() as websocket:
|
async with create_ws_client() as websocket:
|
||||||
engine = get_game_engine()
|
engine = get_game_engine()
|
||||||
|
|
||||||
@ -28,3 +30,5 @@ async def ws_send_game_state() -> None:
|
|||||||
layers=engine.get_board_layout().layers,
|
layers=engine.get_board_layout().layers,
|
||||||
)
|
)
|
||||||
await websocket.send(json.dumps(game_state.dict()))
|
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