|
| 1 | +# Adapted from https://github.com/facebook/tornado/tree/master/demos/websocket |
| 2 | + |
| 3 | +import logging |
| 4 | +import tornado.escape |
| 5 | +import tornado.ioloop |
| 6 | +import tornado.options |
| 7 | +import tornado.web |
| 8 | +import tornado.websocket |
| 9 | +import os.path |
| 10 | +import uuid |
| 11 | + |
| 12 | +from tornado.options import define, options |
| 13 | + |
| 14 | +define("port", default=8888, help="run on the given port", type=int) |
| 15 | + |
| 16 | + |
| 17 | +class Application(tornado.web.Application): |
| 18 | + def __init__(self): |
| 19 | + handlers = [ |
| 20 | + (r"/", MainHandler), |
| 21 | + (r"/post", PostHandler), |
| 22 | + (r"/chatsocket", ChatSocketHandler), |
| 23 | + ] |
| 24 | + settings = dict( |
| 25 | + template_path=os.path.join(os.path.dirname(__file__), "templates"), |
| 26 | + static_path=os.path.join(os.path.dirname(__file__), "static"), |
| 27 | + ) |
| 28 | + tornado.web.Application.__init__(self, handlers, **settings) |
| 29 | + |
| 30 | + |
| 31 | +class MainHandler(tornado.web.RequestHandler): |
| 32 | + def get(self): |
| 33 | + self.render("index.html", messages=ChatSocketHandler.cache) |
| 34 | + |
| 35 | + |
| 36 | +class PostHandler(tornado.web.RequestHandler): |
| 37 | + def post(self): |
| 38 | + message = self.request.body |
| 39 | + logging.info("got message %r", message) |
| 40 | + chat = { |
| 41 | + "id": str(uuid.uuid4()), |
| 42 | + "body": message |
| 43 | + } |
| 44 | + chat["html"] = tornado.escape.to_basestring( |
| 45 | + self.render_string("message.html", message=chat)) |
| 46 | + |
| 47 | + ChatSocketHandler.update_cache(chat) |
| 48 | + ChatSocketHandler.send_updates(chat) |
| 49 | + |
| 50 | + |
| 51 | +class ChatSocketHandler(tornado.websocket.WebSocketHandler): |
| 52 | + waiters = set() |
| 53 | + cache = [] |
| 54 | + cache_size = 200 |
| 55 | + |
| 56 | + def allow_draft76(self): |
| 57 | + # for iOS 5.0 Safari |
| 58 | + return True |
| 59 | + |
| 60 | + def open(self): |
| 61 | + ChatSocketHandler.waiters.add(self) |
| 62 | + |
| 63 | + def on_close(self): |
| 64 | + ChatSocketHandler.waiters.remove(self) |
| 65 | + |
| 66 | + @classmethod |
| 67 | + def update_cache(cls, chat): |
| 68 | + cls.cache.append(chat) |
| 69 | + if len(cls.cache) > cls.cache_size: |
| 70 | + cls.cache = cls.cache[-cls.cache_size:] |
| 71 | + |
| 72 | + @classmethod |
| 73 | + def send_updates(cls, chat): |
| 74 | + logging.info("sending message to %d waiters", len(cls.waiters)) |
| 75 | + for waiter in cls.waiters: |
| 76 | + try: |
| 77 | + waiter.write_message(chat) |
| 78 | + except: |
| 79 | + logging.error("Error sending message", exc_info=True) |
| 80 | + |
| 81 | + def on_message(self, message): |
| 82 | + logging.info("got message %r", message) |
| 83 | + parsed = tornado.escape.json_decode(message) |
| 84 | + chat = { |
| 85 | + "id": str(uuid.uuid4()), |
| 86 | + "body": parsed["body"], |
| 87 | + } |
| 88 | + chat["html"] = tornado.escape.to_basestring( |
| 89 | + self.render_string("message.html", message=chat)) |
| 90 | + |
| 91 | + ChatSocketHandler.update_cache(chat) |
| 92 | + ChatSocketHandler.send_updates(chat) |
| 93 | + |
| 94 | + |
| 95 | +def main(): |
| 96 | + tornado.options.parse_command_line() |
| 97 | + app = Application() |
| 98 | + app.listen(options.port) |
| 99 | + tornado.ioloop.IOLoop.instance().start() |
| 100 | + |
| 101 | + |
| 102 | +if __name__ == "__main__": |
| 103 | + main() |
0 commit comments