summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/tools/third_party/websockets/example/tutorial/step1/app.py
diff options
context:
space:
mode:
Diffstat (limited to 'testing/web-platform/tests/tools/third_party/websockets/example/tutorial/step1/app.py')
-rw-r--r--testing/web-platform/tests/tools/third_party/websockets/example/tutorial/step1/app.py65
1 files changed, 65 insertions, 0 deletions
diff --git a/testing/web-platform/tests/tools/third_party/websockets/example/tutorial/step1/app.py b/testing/web-platform/tests/tools/third_party/websockets/example/tutorial/step1/app.py
new file mode 100644
index 0000000000..3b0fbd7868
--- /dev/null
+++ b/testing/web-platform/tests/tools/third_party/websockets/example/tutorial/step1/app.py
@@ -0,0 +1,65 @@
+#!/usr/bin/env python
+
+import asyncio
+import itertools
+import json
+
+import websockets
+
+from connect4 import PLAYER1, PLAYER2, Connect4
+
+
+async def handler(websocket):
+ # Initialize a Connect Four game.
+ game = Connect4()
+
+ # Players take alternate turns, using the same browser.
+ turns = itertools.cycle([PLAYER1, PLAYER2])
+ player = next(turns)
+
+ async for message in websocket:
+ # Parse a "play" event from the UI.
+ event = json.loads(message)
+ assert event["type"] == "play"
+ column = event["column"]
+
+ try:
+ # Play the move.
+ row = game.play(player, column)
+ except RuntimeError as exc:
+ # Send an "error" event if the move was illegal.
+ event = {
+ "type": "error",
+ "message": str(exc),
+ }
+ await websocket.send(json.dumps(event))
+ continue
+
+ # Send a "play" event to update the UI.
+ event = {
+ "type": "play",
+ "player": player,
+ "column": column,
+ "row": row,
+ }
+ await websocket.send(json.dumps(event))
+
+ # If move is winning, send a "win" event.
+ if game.winner is not None:
+ event = {
+ "type": "win",
+ "player": game.winner,
+ }
+ await websocket.send(json.dumps(event))
+
+ # Alternate turns.
+ player = next(turns)
+
+
+async def main():
+ async with websockets.serve(handler, "", 8001):
+ await asyncio.Future() # run forever
+
+
+if __name__ == "__main__":
+ asyncio.run(main())