/
server.py
51 lines (35 loc) · 968 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python
import tornado.ioloop
import tornado.web
import time
import asyncio
import websockets
import tornado.platform.asyncio
tornado.platform.asyncio.AsyncIOMainLoop().install()
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.render("views/index.html", title = "GPS")
def make_app():
return tornado.web.Application([
(r"/", MainHandler),
])
app = make_app()
app.listen(8888)
print("APP is listening on *8888")
clients = set()
async def hello(websocket, path):
global clients
clients.add(websocket)
while True:
name = await websocket.recv()
#print("< {}".format(name))
print(name)
print(clients)
greeting = name
await asyncio.wait([ws.send(greeting) for ws in clients])
print(greeting)
time.sleep(10)
start_server = websockets.serve(hello, 'localhost', 8765)
print("Listening on *8765")
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()