Dan Albert af4a718fc7 Fix server shut down on exit.
If there's a websocket being waited on the shut down won't actually
happen. Add a new event for shut down and send it to break the websocket
out of its loop.
2022-02-25 17:12:00 -08:00

26 lines
733 B
Python

from fastapi import APIRouter
from fastapi.encoders import jsonable_encoder
from starlette.websockets import WebSocket
from .eventstream import EventStream
from .models import GameUpdateEventsJs
from .. import GameContext
router: APIRouter = APIRouter()
@router.websocket("/eventstream")
async def event_stream(websocket: WebSocket) -> None:
await websocket.accept()
while True:
if not (events := await EventStream.get()).empty:
if events.shutting_down:
await websocket.close()
return
await websocket.send_json(
jsonable_encoder(
GameUpdateEventsJs.from_events(events, GameContext.get())
)
)