SHARE
TWEET

Untitled

a guest Apr 27th, 2018 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. from flask_socketio import SocketIO, join_room, send, emit, disconnect
  3. from flask import Flask, render_template, request
  4.  
  5. robotAIapp = Flask(__name__)
  6. socketio = SocketIO(robotAIapp)
  7.  
  8. @robotAIapp.route('/wsLogin.html')
  9. def wsLogin():
  10.     return render_template('wsLogin.html')
  11.  
  12. @socketio.on('connect')
  13. def connect_handler():
  14.     # check if token was passed to connect
  15.     token = request.args.get('token')
  16.     id = 'Joe'
  17.     join_room(token)
  18.     emit('join_room', id + ' has connected to this room.', room=token)
  19.  
  20. if __name__ == "__main__":
  21.     socketio.run(robotAIapp, debug=True)
  22.    
  23. <!DOCTYPE html>
  24. <html lang="en">
  25. <head>
  26.   <meta charset="UTF-8">
  27.   <title>Flask SocketIO Test</title>
  28. </head>
  29. <body>
  30.   <p>Some sample code to make sure Flask-SocketIO works.</p>
  31.   <button onclick="connectWS()">Connect</button>
  32.   <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.6/socket.io.min.js"></script>
  33.  
  34.   <script type="text/javascript" charset="utf-8">
  35.     // connect to web socket server
  36.     function connectWS() {
  37.         //var socket = io.connect('http://' + document.domain + ':' + location.port);
  38.         var socket = io.connect('http://ec2-13-54-68-85.ap-southeast-2.compute.amazonaws.com/ws?token=1234567890qwertyuiop',
  39.             {rememberTransport: false, query: "token=1234567890POIUYTREWQ" }
  40.         );
  41.  
  42.         // verify our websocket connection is established
  43.         socket.on('connect', function() {
  44.             console.log('Websocket connected!');
  45.         });
  46.  
  47.         // message handler for 'join_room' messages
  48.         socket.on('join_room', function(msg) {
  49.             console.log('join_room ' + msg);
  50.         });
  51.     }
  52.   </script>
  53. </body>
  54. </html>
  55.    
  56. #Redirect web socket connections
  57.     #--------------------------------------------------------------
  58.     location /socket.io {
  59.         proxy_http_version 1.1;
  60.         proxy_set_header Upgrade $http_upgrade;
  61.         proxy_set_header Connection "Upgrade";
  62.         proxy_pass http://127.0.0.1:5000/socket.io;
  63.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top