-
Notifications
You must be signed in to change notification settings - Fork 0
/
wschat.py
executable file
·81 lines (65 loc) · 2.33 KB
/
wschat.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python3
import os.path
import sys
from tornado import gen
from tornado.ioloop import IOLoop
from tornado.web import RequestHandler, StaticFileHandler, Application, url
from tornado.websocket import WebSocketHandler
class MessageBuffer(object):
def __init__(self):
self.messages = []
self.sockets = set()
def add_socket(self, sock):
print("Client connected; socket added")
self.sockets.add(sock)
def remove_socket(self, sock):
print("Client disconnected; socket removed")
self.sockets.remove(sock)
def add_message(self, msg):
self.messages.append(msg);
def send_messages(self):
while len(self.messages):
msg = self.messages.pop(0)
for socket in self.sockets:
socket.write_message(msg)
class ChatWebSocket(WebSocketHandler):
def initialize(self, msgbuf):
self.msgbuf = msgbuf
def open(self):
print("WebSocket opened")
self.msgbuf.add_socket(self)
def on_message(self, message):
self.msgbuf.add_message(message)
IOLoop.current().add_callback(self.msgbuf.send_messages)
def on_close(self):
print("WebSocket closed")
self.msgbuf.remove_socket(self)
class IndexHandler(RequestHandler):
def initialize(self, js_incl=None, css_incl=None):
if isinstance(js_incl, str):
self.js_incl = [js_incl]
elif isinstance(css_incl, list):
self.js_incl = js_incl
else:
raise TypeError('Expected string or list for js_incl')
if isinstance(css_incl, str):
self.css_incl = [css_incl]
elif isinstance(css_incl, list):
self.css_incl = css_incl
else:
raise TypeError('Expected string or list for css_incl')
def get(self):
self.render('index.t', css_incl=self.css_incl, js_incl=self.js_incl)
def main():
msgbuf = MessageBuffer()
app = Application([
url(r'/', IndexHandler, {'js_incl':'chat', 'css_incl':'chat'}),
url(r'/socket', ChatWebSocket, dict(msgbuf=msgbuf)),
url(r'/css/(.*)', StaticFileHandler, {'path': 'css'}),
url(r'/js/(.*)', StaticFileHandler, {'path': 'js'}),
],
template_path='t/', autoreload=True)
app.listen(8888)
IOLoop.current().start()
if __name__ == '__main__':
main()