Advertisement
MagnusArias

SAM | Interval

May 12th, 2017
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <meta charset="utf-8" />
  5.     <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
  6.  
  7.     <style type="text/css">
  8.     body {
  9.         margin: 0;
  10.         height: 100%;
  11.     }
  12.     </style>
  13.  
  14.     <script type="text/javascript">
  15.     "use strict";    
  16.     //Place global variables here
  17.  
  18.     var wsUri = "ws://localhost:8080/EchoChamber/echo/1";
  19.     var output;
  20.     var websocket;
  21.     var inter;
  22.  
  23.     function init ()
  24.     {
  25.         testWebSocket();
  26.     }
  27.  
  28.  
  29.     function testWebSocket()
  30.     {
  31.         websocket = new WebSocket(wsUri);
  32.        
  33.         //Two ways of registering events, addEventListener is prefered - can attach multiple handlers to single event
  34.         // websocket.onopen = function(evt) { onOpen(evt) };
  35.         // websocket.onmessage = function(evt) { onMessage(evt) };
  36.         // websocket.onclose = function(evt) { onClose(evt) };
  37.         // websocket.onerror = function(evt) { onError(evt) };
  38.        
  39.         websocket.addEventListener("open", onOpen, false);
  40.         websocket.addEventListener("close", onClose, false);
  41.         websocket.addEventListener("message", onMessage, false);
  42.         websocket.addEventListener("error", onError, false);
  43.  
  44.     }
  45.  
  46.     //Websocket connection was successfully opened
  47.     function onOpen(evt)
  48.     {
  49.         writeToConsole("CONNECTED");
  50.         doSend("WebSocket ");
  51.     }
  52.  
  53.     //Websocket connection was closed
  54.     function onClose(evt)
  55.     {
  56.         writeToConsole("DISCONNECTED");
  57.     }
  58.  
  59.     //Recieved reply from the server
  60.     function onMessage(evt)
  61.     {
  62.         writeToConsole(evt.data);
  63.         if (evt.data.toLowerCase() == "jasio"){
  64.            
  65.             websocket.close();
  66.             clearInterval(inter);
  67.         }
  68.     }
  69.  
  70.     //Error occured
  71.     function onError(evt)
  72.     {
  73.         writeToConsole(evt.data);
  74.     }
  75.  
  76.     //Sent something to websocket server
  77.     function doSend(message)
  78.     {
  79.         var count = 0;
  80.             inter = setInterval(function(){
  81.            
  82.                 websocket.send(message + String(count++));
  83.                
  84.             }, 1000/60);
  85.  
  86.         writeToConsole("SENT: " + message);
  87.     }
  88.  
  89.     //Write the message to the console
  90.     function writeToConsole(message)
  91.     {
  92.         console.log(message);
  93.     }
  94.  
  95.     //run the function only after the whole page has been loaded
  96.     window.addEventListener("load", init, false);
  97.  
  98.     </script>
  99. </head>
  100.  
  101. <body>
  102. </body>
  103. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement