Advertisement
Guest User

Untitled

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