|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# Based on the chat demo from https://github.com/tornadoweb/tornado/blob/d6819307ee050bbd8ec5deb623e9150ce2220ef9/demos/websocket/chatdemo.py#L1 |
| 4 | +# Original License: |
| 5 | +# |
| 6 | +# Copyright 2009 Facebook |
| 7 | +# |
| 8 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 9 | +# not use this file except in compliance with the License. You may obtain |
| 10 | +# a copy of the License at |
| 11 | +# |
| 12 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | +# |
| 14 | +# Unless required by applicable law or agreed to in writing, software |
| 15 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 16 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 17 | +# License for the specific language governing permissions and limitations |
| 18 | +# under the License. |
| 19 | + |
| 20 | + |
| 21 | +import logging |
| 22 | +import json |
| 23 | +import tornado.escape |
| 24 | +import tornado.ioloop |
| 25 | +import tornado.options |
| 26 | +import tornado.web |
| 27 | +import tornado.websocket |
| 28 | +import os.path |
| 29 | +import uuid |
| 30 | + |
| 31 | +from tornado.options import define, options |
| 32 | + |
| 33 | +define("port", default=8888, help="run on the given port", type=int) |
| 34 | + |
| 35 | + |
| 36 | +class Application(tornado.web.Application): |
| 37 | + def __init__(self): |
| 38 | + handlers = [ |
| 39 | + (r"/", MainHandler), |
| 40 | + (r"/echosocket", EchoWebSocket), |
| 41 | + (r"/subprotocolsocket", SubprotocolWebSocket), |
| 42 | + ] |
| 43 | + settings = dict( |
| 44 | + cookie_secret="__RANDOM_VALUE__", |
| 45 | + template_path=os.path.join(os.path.dirname(__file__), "templates"), |
| 46 | + static_path=os.path.join(os.path.dirname(__file__), "static"), |
| 47 | + xsrf_cookies=True, |
| 48 | + ) |
| 49 | + super(Application, self).__init__(handlers, **settings) |
| 50 | + |
| 51 | + |
| 52 | +class MainHandler(tornado.web.RequestHandler): |
| 53 | + def get(self): |
| 54 | + self.write("Hello, world!") |
| 55 | + |
| 56 | + |
| 57 | +class EchoWebSocket(tornado.websocket.WebSocketHandler): |
| 58 | + def on_message(self, message): |
| 59 | + self.write_message(message) |
| 60 | + |
| 61 | + |
| 62 | +class SubprotocolWebSocket(tornado.websocket.WebSocketHandler): |
| 63 | + def __init__(self, *args, **kwargs): |
| 64 | + self._subprotocols = None |
| 65 | + super().__init__(*args, **kwargs) |
| 66 | + |
| 67 | + def select_subprotocol(self, subprotocols): |
| 68 | + self._subprotocols = subprotocols |
| 69 | + return None |
| 70 | + |
| 71 | + def on_message(self, message): |
| 72 | + self.write_message(json.dumps(self._subprotocols)) |
| 73 | + |
| 74 | + |
| 75 | +def main(): |
| 76 | + tornado.options.parse_command_line() |
| 77 | + app = Application() |
| 78 | + app.listen(options.port) |
| 79 | + tornado.ioloop.IOLoop.current().start() |
| 80 | + |
| 81 | + |
| 82 | +if __name__ == "__main__": |
| 83 | + main() |
0 commit comments