-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
63 lines (51 loc) · 1.52 KB
/
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import eventlet
import socketio
import sys
import time
STATUS_SEQUENCE = [
"asked_starting",
"starting",
"started",
"asked_booting",
"booting",
"booted",
"ask_connecting",
"connecting",
"connected",
"ok",
]
class Server:
def __init__(self):
verbose = True
self.server = socketio.Server(
logger=verbose, engineio_logger=verbose, cors_allowed_origins=[]
)
self.server.on("connect", self.on_connected)
self.server.on("disconnect", self.on_disconnected)
self.server.on("subscribe_status", self.on_subscribe_status_received)
self.clients = []
self.runner = self.server.start_background_task(self._update_status)
def _update_status(self):
while True:
for status in STATUS_SEQUENCE:
if self.clients:
self.server.emit("status", {"status": status})
self.server.sleep(0.1)
def on_connected(self, sid, environ):
print("connect ", sid)
def on_disconnected(self, sid):
print("disconnect ", sid)
self.clients.remove(sid)
def on_subscribe_status_received(self, sid, data):
print("message ", data)
self.clients.append(sid)
if __name__ == "__main__":
sio = Server()
app = socketio.WSGIApp(sio.server)
# https://eventlet.net/doc/ssl.html
eventlet.wsgi.server(
eventlet.wrap_ssl(
eventlet.listen(("", 5000)), keyfile="server.key", certfile="server.cert"
),
app,
)