Guest User

Untitled

a guest
Mar 20th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Engine {
  2.     constructor(url) {
  3.         this.socket = new WebSocket(url);
  4.         this.socketQueueId = 0;
  5.         this.socketQueue = {};
  6.  
  7.         this.income_handler = null;
  8.  
  9.         this.socket.onmessage = (event) => {
  10.             let response = {};
  11.             try {
  12.                 response = JSON.parse(event.data);
  13.             } catch(e) {
  14.                 // string received
  15.                 console.log('Engine:', event.data);
  16.             }
  17.  
  18.             // RPC responses
  19.             if (typeof(response['cmd_id']) != 'undefined' && typeof(this.socketQueue['i_'+response['cmd_id']]) == 'function') {
  20.                 // action response
  21.                 let execFunc = this.socketQueue['i_'+response['cmd_id']];
  22.                 execFunc(response);
  23.                 delete this.socketQueue['i_'+response['cmd_id']];
  24.             }
  25.             else {
  26.                 // non RPC responses
  27.                 if (response != 'undefined' && Object.keys(response).length != 0) {
  28.  
  29.                     switch (response.entity == '--SERVICE--') {
  30.                         case true:
  31.                             // engine internal responses
  32.                             if (response.action == 'init') {
  33.                                 console.log('Engine v', response.data.version);
  34.                             }
  35.                             break;
  36.                         case false:
  37.                             switch (response.action != null && response.entity != null) {
  38.                                 case true:
  39.                                     this.income_handler(response);
  40.                                     break;
  41.                                 case false:
  42.                                     // errors and other
  43.                                     console.log('Engine:', response.msg, response.data);
  44.                             }
  45.                             break;
  46.                     }
  47.                 }
  48.                 else {
  49.                     console.log(response);
  50.                 }
  51.             }
  52.         };
  53.         this.socket.onopen = (event) => {
  54.             console.log('Socket open.');
  55.         };
  56.         this.socket.onclose = () => {
  57.             console.log('Socket close.');
  58.         }
  59.     }
  60.  
  61.     send(message, callback) {
  62.         this.waitForConnection(() => {
  63.  
  64.             if (typeof callback !== 'undefined') {
  65.                 this.socketQueueId++;
  66.                 this.socketQueue['i_'+this.socketQueueId] = callback;
  67.                 message.cmd_id = this.socketQueueId;
  68.                 this.socket.send(JSON.stringify(message));
  69.             }
  70.  
  71.         }, 1000);
  72.     };
  73.  
  74.     waitForConnection(send_func, interval) {
  75.         if (this.socket.readyState === 1) {
  76.             send_func();
  77.         } else {
  78.             let that = this;
  79.             setTimeout(function () {
  80.                 that.waitForConnection(send_func, interval);
  81.             }, interval);
  82.         }
  83.     };
  84.  
  85.     action(message) {
  86.         let inst = this;
  87.         return new Promise((resolve, reject) => {
  88.             inst.send(message, (response) => {resolve(response)});
  89.         });
  90.     }
  91.  
  92.     set_income_handler(handler) {
  93.         this.income_handler = handler;
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment