-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.py
33 lines (25 loc) · 914 Bytes
/
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
import json
from SimpleWebSocketServer import SimpleWebSocketServer, WebSocket
clients = {}
class SimpleEcho(WebSocket):
def _send_den_message(self, data):
data['id'] = self.address
for _, client in clients.items():
client.sendMessage(json.dumps(data))
def handleMessage(self):
# echo message back to client
print('{}-{}'.format(self.address, self.data))
data = json.loads(self.data)
data['event'] = 'position'
self._send_den_message(data)
def handleConnected(self):
clients[self.address] = self
print(self.address, 'connected')
def handleClose(self):
print(self.address, 'closed')
del clients[self.address]
data = {'event': 'delete'}
self._send_den_message(data)
if __name__ == '__main__':
server = SimpleWebSocketServer('', 8000, SimpleEcho)
server.serveforever()