-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWirelessTelemServer.py
63 lines (49 loc) · 1.9 KB
/
WirelessTelemServer.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import asyncio
import websockets
from datetime import datetime
import datetime
import json
# Contains list of all connected users
USERS = set()
def get_server(ip, port):
print('Serving websocket server on ' + ip + ':' + str(port) + ' ...')
return websockets.serve(on_client_connect, ip, port)
# Main server entry point
async def on_client_connect(websocket, path):
connection_addr = (str(websocket.remote_address[0]) + ' : ' +
str(websocket.remote_address[1]))
print('Connected to client at ' + connection_addr +
' on ' + str(datetime.datetime.now()))
await websocket.send(json.dumps({'type': 'connected', 'payload': 'true',
'timestamp': str(datetime.datetime.now())
}))
global USERS
try:
# Track all connected users to broadcast new data whenever available.
USERS.add(websocket)
# Recieve handler is used to monitor messages from the clients.
await recieve_data_loop(websocket)
# Client disconnects whenever this handler returns.
USERS.remove(websocket)
finally:
print("Client disconnected.")
# Broadcast data to all users connected on the socket
async def send_data(data):
global USERS
if USERS:
payload = json.dumps(data, default=str)
await asyncio.wait([user.send(payload) for user in USERS])
# This function runs in its own loop to handle recieving any messages
async def recieve_data_loop(websocket):
while websocket.open:
try:
message = await websocket.recv()
print('Message from client: ' + str(message))
except Exception:
print('Client disconnected!')
return
if __name__ == "__main__":
ip = '127.0.0.1'
port = 5000
asyncio.get_event_loop().run_until_complete(get_server(ip, port))
asyncio.get_event_loop().run_forever()