sendOverSocket(msg) {
    //Lazy socket initialization
    if (!this.socketOpen) {
      //Resolve Promise on connect
      this.socketOpen = new Promise((resolve, reject) => {
        this.send(new message.EventsUrl()).then((response) => {
          return response.entity.urls;
        }, () => null).then((urls) => {
          let url;
          if (urls) {
            let prefix = this.secure? "wss://": "ws://";
            urls = urls.filter((url) => {
              return url.indexOf(prefix) == 0;
            });

            let len = urls.length;
            url = urls[Math.floor(Math.random() * len)];
          } else {
            url = (this.secure ? 'wss://' : 'ws://') + this.host + ':' + this.port + this.basePath + '/events'
          }

          this.socket = this.createWebSocket(url);
          this.socket.onmessage = this.socketListener.bind(this);

          this.socket.onopen = resolve;
          this.socket.onerror = reject;

          //Reset socket on close
          this.socket.onclose = () => {
            this.socket = null;
            this.socketOpen = null;
          };
        }).catch(reject);
      });
    }

    this.socketOpen.then(() => {
      var jsonMessage = JSON.stringify(msg);
      this.socket.send(jsonMessage);
    });
  }
/**
   * Sends a websocket message over a lazily initialized websocket connection.
   * @param {Object} msg
   * @param {string} msg.topic
   * @param {string} msg.token
   */
Sends a websocket message over a lazily initialized websocket connection.
Sends a websocket message over a lazily initialized websocket connection .
sendOverSocket(msg) {
    if (!this.socketOpen) {
      this.socketOpen = new Promise((resolve, reject) => {
        this.send(new message.EventsUrl()).then((response) => {
          return response.entity.urls;
        }, () => null).then((urls) => {
          let url;
          if (urls) {
            let prefix = this.secure? "wss://": "ws://";
            urls = urls.filter((url) => {
              return url.indexOf(prefix) == 0;
            });
            let len = urls.length;
            url = urls[Math.floor(Math.random() * len)];
          } else {
            url = (this.secure ? 'wss://' : 'ws://') + this.host + ':' + this.port + this.basePath + '/events'
          }
          this.socket = this.createWebSocket(url);
          this.socket.onmessage = this.socketListener.bind(this);
          this.socket.onopen = resolve;
          this.socket.onerror = reject;
          this.socket.onclose = () => {
            this.socket = null;
            this.socketOpen = null;
          };
        }).catch(reject);
      });
    }
    this.socketOpen.then(() => {
      var jsonMessage = JSON.stringify(msg);
      this.socket.send(jsonMessage);
    });
  }
sendOverSocket ( msg ) { if ( ! this . socketOpen ) { this . socketOpen = new Promise ( ( resolve , reject ) => { this . send ( new message . EventsUrl ( ) ) . then ( ( response ) => { return response . entity . urls ; } , ( ) => null ) . then ( ( urls ) => { let url ; if ( urls ) { let prefix = this . secure ? "wss://" : "ws://" ; urls = urls . filter ( ( url ) => { return url . indexOf ( prefix ) == 0 ; } ) ; let len = urls . length ; url = urls [ Math . floor ( Math . random ( ) * len ) ] ; } else { url = ( this . secure ? 'wss://' : 'ws://' ) + this . host + ':' + this . port + this . basePath + '/events' } this . socket = this . createWebSocket ( url ) ; this . socket . onmessage = this . socketListener . bind ( this ) ; this . socket . onopen = resolve ; this . socket . onerror = reject ; this . socket . onclose = ( ) => { this . socket = null ; this . socketOpen = null ; } ; } ) . catch ( reject ) ; } ) ; } this . socketOpen . then ( ( ) => { var jsonMessage = JSON . stringify ( msg ) ; this . socket . send ( jsonMessage ) ; } ) ; }
Sends a websocket message over a lazily initialized websocket connection.
Sends a websocket message over a lazily initialized websocket connection .
//Lazy socket initialization //Resolve Promise on connect //Reset socket on close