-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebTunnelRunner.py
More file actions
42 lines (34 loc) · 1.18 KB
/
WebTunnelRunner.py
File metadata and controls
42 lines (34 loc) · 1.18 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
import asyncio
import datetime
import random
import websockets
import random
packets = {
"SET_BOUNDS": "sb",
"FEEDBACK_VALUE": "fv" #aka true value
}
async def consumer_handler(websocket, msg):
async for message in websocket:
print('client sent: ' + message)
async def producer_handler(websocket, path):
while True:
message = await sendUpdate()
await websocket.send(message)
async def sendUpdate():
await asyncio.sleep(random.random() * 1)
#send random values intermittenly amongst the different widgets
return packets["FEEDBACK_VALUE"] + '%widget' + str(random.randint(1,3)) +'%' + str(random.randint(1, 100))
async def server(websocket, path):
consumer_task = asyncio.ensure_future(
consumer_handler(websocket, path))
producer_task = asyncio.ensure_future(
producer_handler(websocket, path))
done, pending = await asyncio.wait(
[consumer_task, producer_task],
return_when=asyncio.FIRST_COMPLETED,
)
for task in pending:
task.cancel()
start_server = websockets.serve(server, "127.0.0.1", 5679)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()