-
Notifications
You must be signed in to change notification settings - Fork 0
/
ws_server.py
44 lines (38 loc) · 1.2 KB
/
ws_server.py
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
#!/usr/bin/env python
from action import Action
from client import Client
import engine
import asyncio
import websockets
from ujson import loads
from www.cv import SERVER_IP
async def handle_client(websocket):
print("New connection from", websocket.remote_address)
asyncio.create_task(keep_alive(websocket))
meta = loads(await websocket.recv())
client = Client(websocket, meta.get('id', None))
if meta['display']:
print('Is display.')
await engine.add_new_display(client)
else:
engine.add_camera()
print('Is camera only')
try:
while True:
posture_txt = await websocket.recv()
posture = loads(posture_txt)
await engine.handle_posture(client, posture)
except websockets.ConnectionClosedError:
print('Connection closed')
return
await asyncio.Future()
async def keep_alive(websocket):
try:
while True:
await websocket.ping()
await asyncio.sleep(1)
except:
pass
async def start_websocket_server():
async with websockets.serve(handle_client, SERVER_IP, 8001):
await asyncio.Future() # run forever