Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Bottle app on Tornado

View gist:2497341
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
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
from tornado.web import Application, StaticFileHandler
from tornado.websocket import WebSocketHandler
 
from bottle import get, app
 
@get('/')
def index():
return 'Welcome'
 
class MessageDispatcher(object):
clients = []
 
@classmethod
def add_client(cls):
clients.add(cls)
 
@classmethod
def remove_client(cls):
clients.remove(cls)
 
@classmethod
def broadcast(cls, message):
for c in clients:
c.send(message)
 
class WSHandler(WebSocketHandler):
clients = []
 
def open(self):
logging.info('Client connected')
MessageDispatcher.add_client(self)
 
def on_message(self, message):
logging.log('Received message')
if message == 'close':
MessageDispatcher.remove_client(self)
self.close()
 
def on_close(self):
logging.info('Client disconnected')
MessageDispatcher.remove_client(self)
 
if __name__ == "__main__":
# How do you mount Bottle's app here?
HTTPServer(Application([(r"/ws", WSHandler),
(r"/css/(.*)", StaticFileHandler, {"path": "./css/"}),
(r"/js/(.*)", StaticFileHandler, {"path": "./js/"}),
(r"/img/(.*)", StaticFileHandler, {"path": "./img/"}),
("/(.*)", MOUNT_APP_HERE)])
).listen(1024)
IOLoop.instance().start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.