Guest User

Untitled

a guest
Sep 14th, 2015
120
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!doctype html>
  2. <head>
  3.     <meta charset="utf-8">
  4.     <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  5.     <title>socket.io Flask client</title>
  6.     <meta name="description" content="">
  7.     <meta name="viewport" content="width=device-width">
  8. </head>
  9. <body>
  10.   <input id="cmd" type="text" />
  11.   <textarea id="value"></textarea><button id="sendBtn" type="button">Send</button>
  12.   <div id="console"></div>
  13.  
  14.   <script src="/js/socket.io.min.js"></script>
  15.   <script>
  16.       window.onload = function () {
  17.     // Connect to socket.io
  18.  
  19. {#          document.domain + ':' + location.port#}
  20.           console.log(location.port);
  21.     socket = io.connect('wss://' + document.domain  + ':' + location.port + '/echo');
  22.    
  23.     document.getElementById("sendBtn").addEventListener('click', function() {
  24.         var cmdVal = document.getElementById("cmd").value;
  25.         var valVal = document.getElementById("value").value;
  26.        
  27.         socket.emit(cmdVal,  { msg: valVal });
  28.     });
  29.    
  30.     // React to a received message
  31.     socket.on('answer', function (data){
  32.       console.log("data::", data);
  33.       document.getElementById("console").innerHTML = data.message;
  34.       console.log(data);
  35.     });
  36.     socket.on('connect', function (){
  37.       socket.emit('{ "action" : "/questions", "data":{} }');
  38.     });
  39.     socket.on('ping', function (data) {
  40.       // Modify the DOM to show the message
  41.       document.getElementById("msg").innerHTML = data.msg;
  42.       // Send a message back to the server
  43.       socket.emit('pong', {
  44.         msg: "The web browser also knows socket.io."
  45.       });
  46.     });
  47.       };
  48.     </script>
  49.   </body>
RAW Paste Data