-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
57 lines (43 loc) · 1.25 KB
/
app.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
from SimpleWebSocketServer import SimpleWebSocketServer, WebSocket
import json
import threading
import time
import http.server
import socketserver
PORT = 5000
Handler = http.server.SimpleHTTPRequestHandler
x = 0
y = 0
heading = 0
clients = []
class EchoCSV(WebSocket):
def handleMessage(self):
data = json.loads(self.data)
# data is an array of Objects with params: x, y, and heading
for line in data:
# print(line['x'])
# print(line['y'])
# print(line['heading'])
print(line)
self.sendMessage('hello')
def handleConnected(self):
clients.append(self)
print(self.address, 'connected')
def handleClose(self):
clients.remove(self)
print(self.address, 'closed')
server = SimpleWebSocketServer('', 4000, EchoCSV)
httpd = socketserver.TCPServer(("", PORT), Handler)
print("serving at port", PORT)
t1 = threading.Thread(target = server.serveforever)
t2 = threading.Thread(target= httpd.serve_forever)
t1.daemon = True
t1.start()
t2.daemon = True
t2.start()
while 1:
for client in clients:
client.sendMessage(u"{{ \"x\": {}, \"y\": {}, \"heading\": {} }}".format(x, y, heading))
time.sleep(1)
x = x + 1
y = y + 1