Handle a connection and dispatch it according to who is connecting.
(websocket)
| 161 | |
| 162 | |
| 163 | async def handler(websocket): |
| 164 | """ |
| 165 | Handle a connection and dispatch it according to who is connecting. |
| 166 | |
| 167 | """ |
| 168 | # Receive and parse the "init" event from the UI. |
| 169 | message = await websocket.recv() |
| 170 | event = json.loads(message) |
| 171 | assert event["type"] == "init" |
| 172 | |
| 173 | if "join" in event: |
| 174 | # Second player joins an existing game. |
| 175 | await join(websocket, event["join"]) |
| 176 | elif "watch" in event: |
| 177 | # Spectator watches an existing game. |
| 178 | await watch(websocket, event["watch"]) |
| 179 | else: |
| 180 | # First player starts a new game. |
| 181 | await start(websocket) |
| 182 | |
| 183 | |
| 184 | async def main(): |