Created
March 4, 2014 19:00
-
-
Save djds23/9353220 to your computer and use it in GitHub Desktop.
tutorial for flask-socketio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, render_template | |
from flask.ext.socketio import SocketIO, emit | |
app = Flask(__name__) | |
app.config['SECRET_KEY'] = 'secret!' | |
socketio = SocketIO(app) | |
@app.route('/') | |
def index(): | |
return render_template('index.html') | |
@socketio.on('my event', namespace='/test') | |
def test_message(message): | |
emit('my response', {'data':message['data']}) | |
@socketio.on('my broadcast event', namespace='/test') | |
def test_message(message): | |
emit('my response', {'data': message['data']}, broadcast=True) | |
@socketio.on('connect', namespace='/test') | |
def test_connect(): | |
emit('my response', {'data': 'Connected'}) | |
@socketio.on('disconnect', namespace='/test') | |
def test_disconnect(): | |
print('Client disconnected') | |
if __name__='__main__': | |
socketio.run(app, host='127.0.0.1:8002') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment