-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathflasknado.py
37 lines (29 loc) · 950 Bytes
/
flasknado.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
"""Flasknado! - A simple example of using Flask and Tornado
together.
"""
from __future__ import print_function
from flask import Flask, render_template
from tornado.wsgi import WSGIContainer
from tornado.web import Application, FallbackHandler
from tornado.websocket import WebSocketHandler
from tornado.ioloop import IOLoop
class WebSocket(WebSocketHandler):
def open(self):
print("Socket opened.")
def on_message(self, message):
self.write_message("Received: " + message)
print("Received message: " + message)
def on_close(self):
print("Socket closed.")
app = Flask('flasknado')
@app.route('/')
def index():
return render_template('index.html')
if __name__ == "__main__":
container = WSGIContainer(app)
server = Application([
(r'/websocket/', WebSocket),
(r'.*', FallbackHandler, dict(fallback=container))
])
server.listen(8080)
IOLoop.instance().start()