forked from DeepNav/ControlSystem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WsServer.py
38 lines (28 loc) · 1014 Bytes
/
WsServer.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
import logging
import time
import json
from SimpleWebSocketServer import SimpleWebSocketServer, WebSocket
# a web socket server to push data to client
class WsServer(object):
def __init__(self, port):
self.ws_clients = []
ws_server = self
class DataPusher(WebSocket):
def handleMessage(self):
pass
def handleConnected(self):
logging.info('ws_server: client connected')
ws_server.ws_clients.append(self)
def handleClose(self):
ws_server.ws_clients.remove(self)
logging.info('ws_server: client closed')
self.server = SimpleWebSocketServer('', port, DataPusher)
def start_server(self):
self.server.serveforever()
logging.info("ws_server started")
def broadcast(self, msg):
msg.update({
"ts": time.time()
})
for client in self.ws_clients:
client.sendMessage(unicode(json.dumps(msg)))