Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var WebSocket = require('ws');
  2.  
  3. class WebsocketClient {
  4.     constructor(headers) {
  5.         this.uri_ = "";
  6.         this.con_ = null;
  7.  
  8.         this.headers_ = headers || {};
  9.  
  10.         this.reconnect_timeout_ref_ = null;
  11.         this.reconnect_timeout_ = 500;
  12.  
  13.  
  14.         this.onText = null;
  15.         this.onBytes = null;
  16.         this.onConnect = null;
  17.         this.onDisconnect = null;
  18.     }
  19.  
  20.     isConnected() {
  21.         return this.con_ && (this.con_.readyState === this.con_.OPEN);
  22.     }
  23.  
  24.     writeBytes(bytes) {
  25.         if (!this.isConnected()) {
  26.             return;
  27.         }
  28.         this.con_.send(bytes);
  29.     }
  30.  
  31.     writeText(text) {
  32.         if (!this.isConnected()) {
  33.             return;
  34.         }
  35.         this.con_.send(text);
  36.     }
  37.  
  38.     sendPing() {
  39.         this.writeText("2");
  40.     }
  41.  
  42.     startReconnectTimer(instant = false) {
  43.         if (this.reconnect_timeout_ref_) {
  44.             return;
  45.         }
  46.  
  47.         const self = this;
  48.         let timer_cb = function () {
  49.             self.reconnect_timeout_ref_ = null;
  50.             self.reconnect_timeout_ *= 2;
  51.             if (self.reconnect_timeout_ > 15000) {
  52.                 self.reconnect_timeout_ = 15000;
  53.             }
  54.  
  55.             self.setUpConnection();
  56.             self.startReconnectTimer();
  57.         };
  58.  
  59.         if (instant) {
  60.             timer_cb();
  61.         }
  62.         else {
  63.             //console.log("trying to reconnect ws in " + this.reconnect_timeout_.toString() + "ms...");
  64.             this.reconnect_timeout_ref_ = setTimeout(timer_cb, this.reconnect_timeout_);
  65.         }
  66.     }
  67.  
  68.     stopReconnectTimer() {
  69.         if (this.reconnect_timeout_ref_) {
  70.             clearInterval(this.reconnect_timeout_ref_);
  71.             this.reconnect_timeout_ref_ = null;
  72.             this.reconnect_timeout_ = 500;
  73.  
  74.             console.log("stopped reconnect timer!");
  75.         }
  76.     }
  77.  
  78.     connect(uri) {
  79.         this.uri_ = uri;
  80.  
  81.         this.startReconnectTimer(true);
  82.     }
  83.  
  84.     clearConnection() {
  85.         if (!this.con_) {
  86.             return;
  87.         }
  88.  
  89.         this.con_.onopen = function () { };
  90.         this.con_.onerror = function (error) { };
  91.         this.con_.onmessage = function (e) { };
  92.         this.con_.onclose = function () { };
  93.         this.con_.close();
  94.         this.con_ = null;
  95.  
  96.         this.onDisconnect && this.onDisconnect();
  97.     }
  98.  
  99.     close() {
  100.         this.stopReconnectTimer();
  101.         this.clearConnection();
  102.     }
  103.  
  104.     setUpConnection() {
  105.         this.clearConnection();
  106.  
  107.         const self = this;
  108.  
  109.         this.con_ = new WebSocket(this.uri_, {
  110.             headers: self.headers_
  111.         });
  112.         this.con_.binaryType = "arraybuffer";
  113.  
  114.         this.con_.onopen = function () {
  115.             self.onConOpened();
  116.         };
  117.  
  118.         this.con_.onerror = function (err) {
  119.             self.onConError(err);
  120.         };
  121.  
  122.         /*
  123.         this.ng_zone_.runOutsideAngular(() => {
  124.             this.con_.onmessage = function (e: MessageEvent) {
  125.                 self.onConMessageReceived(e);
  126.             };
  127.         });
  128.         */
  129.  
  130.         this.con_.onmessage = function (e) {
  131.             self.onConMessageReceived(e);
  132.         };
  133.  
  134.  
  135.         this.con_.onclose = function () {
  136.             self.onConClosed();
  137.         }
  138.     }
  139.  
  140.     onConOpened() {
  141.         //console.log("ws connected!");
  142.         this.stopReconnectTimer();
  143.         this.onConnect && this.onConnect();
  144.     }
  145.  
  146.     onConError(err) {
  147.         //console.log("ws error!", err);
  148.         this.clearConnection();
  149.         this.startReconnectTimer();
  150.     }
  151.  
  152.     onConClosed() {
  153.         //console.log("ws closed");
  154.         this.clearConnection();
  155.         this.startReconnectTimer();
  156.     }
  157.  
  158.     onConMessageReceived(e) {
  159.         var data = e.data;
  160.  
  161.         if (typeof data === "string") {
  162.             this.onText && this.onText(data);
  163.         }
  164.         else {
  165.             this.onBytes && this.onBytes(data);
  166.         }
  167.     }
  168. }
  169.  
  170. module.exports = WebsocketClient;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement