-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
46 lines (32 loc) · 1016 Bytes
/
main.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
45
46
import asyncio
import json
import struct
import websockets
import time
import config
last = time.time()
class Receiver:
def connection_made(self, transport):
self.transport = transport
def datagram_received(self, message, addr):
global last
ip = addr[0]
print(f"Message from {ip}, time since last {(time.time() - last)*1000:.2f}ms")
last = time.time()
data[ip] = struct.unpack("3f", message)
async def send(websocket, _):
async for message in websocket:
if message == "get":
print("Sending data")
await websocket.send(json.dumps(data, sort_keys=True))
data = {}
def main():
loop = asyncio.get_event_loop()
udpsock = loop.create_datagram_endpoint(
lambda: Receiver(),
local_addr=(config.HOST, config.RECEIVE_PORT))
loop.run_until_complete(udpsock)
loop.run_until_complete(websockets.serve(
send, config.HOST, config.SEND_PORT))
loop.run_forever()
asyncio.run(main())