Advertisement
Blackthorn

ping.html (Ping!)

Feb 21st, 2014
566
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 2.18 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset=utf-8 />
  5. <title>Ping!</title>
  6.  
  7. <style>
  8. button {
  9.   width: 100%;
  10.   font-size:2em;
  11.   background:green;
  12.   color:pink;
  13. }
  14.  
  15. div {font-size:1.5em;}
  16. </style>
  17. </head>
  18. <body>
  19.   <button onclick="socketPing()">Ping!</button>
  20.   <script src="http://node.steelcomputers.com:8000/socket.io/socket.io.js"></script>
  21.   <h1 id="msg"></h1>
  22.   <div id="uptime"></div>
  23.   <div id="connected"></div>
  24.   <div id="events"></div>
  25.   <div id="latency"></div>
  26. <script>
  27. var socket = io.connect("http://node.steelcomputers.com", {port: 8000, transports: ["websocket"]});
  28. socket.on("connect", onSocketConnected);
  29. socket.on("disconnect", onSocketDisconnect);
  30. socket.on("pong", onPong);
  31. var timer;
  32.  
  33. function onSocketConnected(){ // Connection to websocket server is established
  34.   msg.innerText = 'Connected to WebSocket server.';
  35.   console.log('Connected to WebSocket server.');
  36. }
  37.  
  38. function onSocketDisconnect(){ // Connection to websocket server is lost
  39.   msg.innerText = 'WebSocket connection lost.';
  40.   console.log('WebSocket connection lost.');      
  41. }
  42.  
  43. function onPong(data){
  44.   console.timeEnd('ping'); // Stop chrome timer, outputs time in console.
  45.   console.log(data); // Dump the returned object to the console.
  46.   uptime.innerText = "The server has been online for " +
  47.     ( data.uptime > 60*60 ? Math.floor(data.uptime/(60*60)) + " hours, " : "" ) +
  48.     ( data.uptime > 60 ? Math.floor((data.uptime % (60*60))/60) + " minutes, " : "" ) + " and " +
  49.     ( data.uptime % 60 ) + " seconds."; // Update the uptime div
  50.   connected.innerText = "There are " + data.connected + " active websocket connections."; // Update DIV
  51.   events.innerText = "" + data.numEvents + " events have fired on the server.";           // Update DIV
  52.   latency.innerText = "Latency: " + ((new Date()).getTime() - timer.getTime()) + "ms";    // Update DIV
  53.   msg.innerText = data.message; // Update the div with the message property on the data object.
  54. }
  55.  
  56. function socketPing(){
  57.   timer = new Date(); // Record the current date-time in global var
  58.   console.time('ping'); // Start a chrome timer (optional/redundant code)
  59.   socket.emit('ping'); // Send ping to server
  60. }
  61. </script>
  62. </body>
  63. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement