Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from flask import Flask, render_template, redirect, url_for
- from flask_socketio import SocketIO, emit
- import json
- def load_config():
- # configuration
- return json.load(open('/etc/geekdj/config.json'))
- def load_users():
- return json.load(open('logged_in.json'))
- config = load_config()
- geekdj = Flask(__name__)
- geekdj.config["DEBUG"] = config["debug"]
- geekdj.config["SECRET_KEY"] = config["secret_key"]
- geekdj.config.from_envvar("FLASKR_SETTINGS", silent=True)
- socketio = SocketIO(geekdj)
- @geekdj.route('/')
- def index():
- return render_template('index.html')
- # SocketIO functions
- @socketio.on('connect')
- def chat_connect():
- print ('connected')
- @socketio.on('disconnect')
- def chat_disconnect():
- print ("Client disconnected")
- @socketio.on('broadcast')
- def chat_broadcast(message):
- print ("test")
- emit("chat", {'data': message['data']})
- if __name__ == "__main__":
- socketio.run(geekdj, port=8000)
- <script src="//cdn.socket.io/socket.io-1.4.5.js"></script>
- <script type="text/javascript" charset="utf-8">
- $(document).ready(function(){
- // the socket.io documentation recommends sending an explicit package upon connection
- // this is specially important when using the global namespace
- var socket = io.connect('http://localhost:8000');
- socket.on('connection', function(socket) {
- socket.emit('foo', {foo: "bar"});
- socket.join("test");
- });
- socket.on('joined', function(data) {
- console.log('Joined room!');
- console.log(data["room"]);
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement