daily pastebin goal
81%
SHARE
TWEET

Untitled

a guest Mar 26th, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const url = 'ws://localhost:1337';
  2. const connection = new WebSocket(url);
  3.  
  4. connection.onopen = () => {
  5.     console.log('websocket connection established');
  6.     connection.send('hey here is the client');
  7.     connection.send(JSON.stringify({'prop1': 'v1'}));
  8. }
  9.  
  10. connection.onerror = function (error) {
  11.     console.log(`WebSocket error: ${error}`)
  12. }
  13.  
  14. connection.onmessage = (e) => {
  15.     console.log(e.data);
  16.     logInfo(e.data);
  17. }
  18.  
  19. function getServerTime(state) {
  20.     var action = {
  21.         'action': 'sendservertime',
  22.         'state': state
  23.     };
  24.     connection.send(JSON.stringify(action));
  25. }
  26.  
  27. function logInfo(msg) {
  28.     log(msg);
  29. }
  30.  
  31. function logError(msg) {
  32.     log(msg, 'errLabel');
  33. }
  34.  
  35. function log(msg, classLabel) {
  36.     if (typeof (classLabel) === "string") {
  37.         classLabel = 'class="' + classLabel + '"';
  38.     } else {
  39.         classLabel = '';
  40.     }
  41.     let logElem = document.querySelector(".log");
  42.     let timeStamp = new Date();
  43.     //let timeStr = time.toLocaleTimeString();
  44.     let timeStr = pad(timeStamp.getHours()) + ':' + pad(timeStamp.getMinutes()) + ':' + pad(timeStamp.getSeconds());
  45.     logElem.innerHTML = '<label ' + classLabel + '>' + timeStr + ': ' + msg + '</label><br/>' + logElem.innerHTML;
  46. }
  47.  
  48. function pad(number) {
  49.     if (number < 10) {
  50.         return '0' + number;
  51.     }
  52.     return number;
  53. }
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