Ejemplo n.º 1
0
from __future__ import print_function
from flask import Flask, render_template
from flask.ext.tornado import Tornado, RequestHandler
from sockjs.tornado import SockJSConnection, SockJSRouter


app = Flask(__name__)
tornado = Tornado(app)


@app.route('/')
def main():
    return render_template('sockjs.html')


class Socket(SockJSConnection):

    clients = set()

    def on_open(self, info):
        self.clients.add(self)
        print("Client connected")
        self.broadcast(self.clients, "Hello")

    def on_message(self, msg):
        self.broadcast(self.clients, msg)

    def on_close(self):
        self.clients.remove(self)
        print("Client disconnected")
Ejemplo n.º 2
0
from flask import Flask
from flask.ext.tornado import Tornado, RequestHandler


app = Flask(__name__)
tornado = Tornado(app)


@app.route("/")
def main():
    return "Hello, from Flask"


@app.route("/duplicate")
def duplicate():
    return "This route is overwritten by Tornado"


class MainHandler(RequestHandler):
    def get(self):
        self.write("Hello, from Tornado")


tornado.routes([(r"/tornado", MainHandler), (r"/duplicate", MainHandler)])


if __name__ == "__main__":
    app.debug = True
    tornado.run()