-
Notifications
You must be signed in to change notification settings - Fork 31
/
wsck.py
39 lines (29 loc) · 959 Bytes
/
wsck.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
import tornado.websocket
import logging
class WebSocketHandler(tornado.websocket.WebSocketHandler):
waiters = set()
def check_origin(self, origin):
return True
def open(self):
WebSocketHandler.waiters.add(self)
def on_message(self, message):
WebSocketHandler.send_updates(message)
def on_close(self):
WebSocketHandler.waiters.remove(self)
@classmethod
def send_updates(cls, data):
for waiter in cls.waiters:
try:
waiter.write_message(data)
except:
logging.error("Error sending message", exc_info=True)
class Application(tornado.web.Application):
def __init__(self):
handlers = [
(r"/", WebSocketHandler)
]
tornado.web.Application.__init__(self, handlers)
if __name__ == "__main__":
app = Application()
app.listen(8888)
tornado.ioloop.IOLoop.current().start()