-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebsocket_server.py
More file actions
59 lines (52 loc) · 1.57 KB
/
websocket_server.py
File metadata and controls
59 lines (52 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python
import asyncio
import websockets
import json
from cv_track import remote_cv
async def echo(websocket):
async for message in websocket:
print(message)
await websocket.send(message)
async def handler(websocket):
while True:
try:
message = await websocket.recv()
except websockets.ConnectionClosedOK:
break
print(message)
# async def handler2(websocket):
# try:
# print('handler2 called')
# async for message in websocket:
# print(message)
# for player, column, row in [
# ('PLAYER1', 3, 0),
# ('PLAYER2', 3, 1),
# ('PLAYER3', 4, 0),
# ('PLAYER4', 4, 1),
# ('PLAYER5', 2, 0),
# ('PLAYER6', 1, 0),
# ('PLAYER7', 5, 0),
# ]:
# event = {
# "type": "right",
# "player": player,
# "column": column,
# "row": row,
# }
# await websocket.send(json.dumps(event))
# await asyncio.sleep(0.5)
# # event = {
# # "type": "q",
# # "player": 'PLAYER1',
# # }
# # await websocket.send(json.dumps(event))
# except BaseException as err:
# print(f"Unexpected {err=}, {type(err)=}")
# raise
async def main():
async with websockets.serve(remote_cv, "", 8002):
await asyncio.Future() # run forever
if __name__ == "__main__":
asyncio.run(main())
# my_cv()