Advertisement
Guest User

Untitled

a guest
Feb 13th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. from flask import Flask, render_template, redirect, url_for
  2. from flask_socketio import SocketIO, emit
  3. import json
  4.  
  5. def load_config():
  6. # configuration
  7. return json.load(open('/etc/geekdj/config.json'))
  8.  
  9. def load_users():
  10. return json.load(open('logged_in.json'))
  11.  
  12. config = load_config()
  13.  
  14. geekdj = Flask(__name__)
  15.  
  16. geekdj.config["DEBUG"] = config["debug"]
  17. geekdj.config["SECRET_KEY"] = config["secret_key"]
  18. geekdj.config.from_envvar("FLASKR_SETTINGS", silent=True)
  19.  
  20. socketio = SocketIO(geekdj)
  21.  
  22. @geekdj.route('/')
  23. def index():
  24. return render_template('index.html')
  25.  
  26. # SocketIO functions
  27.  
  28. @socketio.on('connect')
  29. def chat_connect():
  30. print ('connected')
  31.  
  32. @socketio.on('disconnect')
  33. def chat_disconnect():
  34. print ("Client disconnected")
  35.  
  36. @socketio.on('broadcast')
  37. def chat_broadcast(message):
  38. print ("test")
  39. emit("chat", {'data': message['data']})
  40.  
  41. if __name__ == "__main__":
  42. socketio.run(geekdj, port=8000)
  43.  
  44. <script src="//cdn.socket.io/socket.io-1.4.5.js"></script>
  45. <script type="text/javascript" charset="utf-8">
  46. $(document).ready(function(){
  47.  
  48. // the socket.io documentation recommends sending an explicit package upon connection
  49. // this is specially important when using the global namespace
  50. var socket = io.connect('http://localhost:8000');
  51.  
  52. socket.on('connection', function(socket) {
  53. socket.emit('foo', {foo: "bar"});
  54. socket.join("test");
  55. });
  56.  
  57. socket.on('joined', function(data) {
  58. console.log('Joined room!');
  59. console.log(data["room"]);
  60. });
  61. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement