ejherran

web.html

Oct 30th, 2013
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 2.64 KB | None | 0 0
  1. <!DOCTYPE html>
  2.  
  3. <html lang="es">
  4.  
  5.     <head>
  6.         <title>WebSocket Echo Test</title>
  7.         <meta charset="utf-8" />
  8.     </head>
  9.      
  10.     <body>
  11.        
  12.         <header>
  13.             <h1>WEBSOCKET ECHO TEST</h1>
  14.         </header>
  15.  
  16.         <section>
  17.  
  18.             <article>
  19.  
  20.                 <h2>CONSOLE:</h2>
  21.                
  22.                 <p><textarea id="console" cols="49" rows="15" readonly="readonly"></textarea></p>
  23.                
  24.                 <p><input id="msgbox" type="text" size="50" placeholder="Escriba un mensaje, enter para enviar!" /></p>
  25.                
  26.                 <p><input id="connect" type="button" value="CONNECT" /></p>
  27.            
  28.             </article>
  29.            
  30.         </section>
  31.    
  32.     </body>
  33.    
  34.     <script type="text/javascript">
  35.        
  36.         window.onload = $init;
  37.         window.onbeforeunload = $finish
  38.        
  39.         var sock = null;
  40.         var wsuri = "ws://localhost:1234/echo";
  41.         var wscon = false;
  42.        
  43.         function $init()
  44.         {
  45.             document.getElementById("msgbox").focus();
  46.             document.getElementById("msgbox").onkeypress = sender;
  47.            
  48.             document.getElementById("connect").onclick = active;
  49.         }
  50.        
  51.         function $finish()
  52.         {
  53.             if(wscon)
  54.                 sock.send("$exit");
  55.         }
  56.        
  57.         function active()
  58.         {
  59.             sock = new WebSocket(wsuri);
  60.            
  61.             sock.onopen = function()
  62.             {
  63.                 wscon = true;
  64.                 consoleAdd("connected to " + wsuri +"\nType $exit to disconnect!.");
  65.                 document.getElementById("connect").disabled = true;
  66.             }
  67.  
  68.             sock.onclose = function(e)
  69.             {
  70.                 wscon = false;
  71.                 consoleAdd("connection closed (" + e.code + ")\n\n");
  72.                 document.getElementById("connect").disabled = false;
  73.             }
  74.  
  75.             sock.onmessage = function(e)
  76.             {
  77.                 consoleAdd("SERVER >> " + e.data);
  78.             }
  79.         }
  80.        
  81.         function sender(event)
  82.         {
  83.             if(event.keyCode == 13 && wscon)
  84.                sock.send(this.value);
  85.         }
  86.        
  87.         function consoleAdd(msg)
  88.         {
  89.             document.getElementById("console").value += msg + "\n";
  90.             document.getElementById('console').scrollTop = document.getElementById('console').scrollHeight;
  91.            
  92.             document.getElementById("msgbox").value = "";
  93.             document.getElementById("msgbox").focus();
  94.         }
  95.    
  96.     </script>
  97.    
  98. </html>
Advertisement
Add Comment
Please, Sign In to add comment