How to use the ws.CLOSED function in ws

To help you get started, we’ve selected a few ws examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github traumverloren / speech-to-image-necklace / app.js View on Github external
currentInterval = setInterval(() => {
        // if current word hasn't changed, don't send another request to image search api
        if (currentWord != nextWord) {
          currentWord = nextWord;
          findImage(currentWord);
        }
        try {
          if (ws.readyState != WebSocket.CLOSED) {
            // send image url to client
            ws.send(currentImageURL);
          }
        } catch (e) {
          console.log(e);
        }
      }, 1000);
    }
github OgarProject / Ogar / src / GameServer.js View on Github external
for (var i = 0; i < l; i++) {
            buffer[i] = array[o + i];
        }

        return buffer;
    }

    //if (this.readyState == WebSocket.OPEN && (this._socket.bufferSize == 0) && packet.build) {
    if (this.readyState == WebSocket.OPEN && packet.build) {
        var buf = packet.build();
        this.send(buf, { binary: true });
    } else if (!packet.build) {
        // Do nothing
    } else {
        this.readyState = WebSocket.CLOSED;
        this.emit('close');
        this.removeAllListeners();
    }
};
github imxeno / tradingview-scraper / src / TradingViewAPI.ts View on Github external
return new Promise((resolve, reject) => {
      const each = 10;
      const runs = 3000 / each; // time in ms divided by above

      if (this.ws.readyState === WebSocket.CLOSED) {
        this._resetWebSocket();
      }

      const interval = setInterval(() => {
        if (this.ws.readyState === WebSocket.OPEN && this.sessionRegistered) {
          this._getTicker(tickerName, resolve, reject);
          clearInterval(interval);
        } else if (!runs) {
          reject("WebSocket connection is closed.");
          clearInterval(interval);
        }
      }, each);
    });
  }
github Azure / azure-relay-node / hyco-https / lib / _hyco_outgoing.js View on Github external
function _writeResponsePreamble(callback) {
  const conn = this.connection;
  if (conn && conn.readyState === WS.CLOSED) {
    // The socket was destroyed. If we're still trying to write to it,
    // then we haven't gotten the 'close' event yet.
    return false;
  }

  var hdrs = {};
  for( var k in this._headers) {
      //if ( k === "set-cookie" || k === "etag") continue;
      if ( this._headers[k] instanceof Array ) {
        hdrs[k] = this._headers[k][0];
      } else {
        hdrs[k] = this._headers[k];
      }
  }

  var response = { response : {
github streamr-dev / streamr-client-javascript / src / Connection.js View on Github external
connect() {
        if (this.state === Connection.State.CONNECTING) {
            return Promise.reject(new Error('Already connecting!'))
        } else if (this.state === Connection.State.CONNECTED) {
            return Promise.reject(new Error('Already connected!'))
        }
        if (!this.socket || this.socket.readyState === WebSocket.CLOSED) {
            try {
                this.socket = new WebSocket(this.options.url)
            } catch (err) {
                this.emit('error', err)
                return Promise.reject(err)
            }
        }
        this.socket.binaryType = 'arraybuffer'
        this.socket.events = new EventEmitter()
        this.socket.onopen = () => this.socket.events.emit('open')
        this.socket.onclose = () => this.socket.events.emit('close')

        this.updateState(Connection.State.CONNECTING)

        this.socket.events.on('open', () => {
            debug('Connected to ', this.options.url)
github hprose / hprose-nodejs / lib / client / WebSocketClient.js View on Github external
var future = new Future();
        _futures[id] = future;
        if (context.timeout > 0) {
            future = future.timeout(context.timeout).catchError(function(e) {
                delete _futures[id];
                --_count;
                close();
                throw e;
            },
            function(e) {
                return e instanceof TimeoutError;
            });
        }
        if (ws === null ||
            ws.readyState === WebSocket.CLOSING ||
            ws.readyState === WebSocket.CLOSED) {
            connect();
        }
        if (_count < 100) {
            ++_count;
            _ready.then(function() { send(id, request); });
        }
        else {
            _requests.push([id, request]);
        }
        if (context.oneway) { future.resolve(); }
        return future;
    }
    function close() {
github ethereum / mist / modules / ethereumNodeRemote.js View on Github external
async send(method, params = [], retry = false) {
    if (!Array.isArray(params)) {
      params = [params];
    }

    if (
      !this.ws ||
      !this.ws.readyState ||
      this.ws.readyState === WebSocket.CLOSED
    ) {
      ethereumNodeRemoteLog.warn(
        `Remote websocket connection not open, attempting to reconnect and retry ${method}...`
      );
      return new Promise(resolve => {
        this.start().then(() => {
          resolve(this.send(method, params, retry));
        });
      });
    }

    if (this.ws.readyState !== WebSocket.OPEN) {
      if (this.ws.readyState === WebSocket.CONNECTING) {
        ethereumNodeRemoteLog.error(
          `Can't send method ${method} because remote WebSocket is connecting`
        );
github wix / Detox / detox / src / client / AsyncWebSocket.js View on Github external
return new Promise((resolve, reject) => {
      if (this.ws) {
        this.ws.onclose = (message) => {
          this.ws = null;
          resolve(message);
        };

        if (this.ws.readyState !== WebSocket.CLOSED) {
          this.ws.close();
        } else {
          this.ws.onclose();
        }
      } else {
        reject(new Error(`websocket is closed, init the by calling 'open()'`));
      }
    });
  }

ws

Simple to use, blazing fast and thoroughly tested websocket client and server for Node.js

MIT
Latest version published 6 months ago

Package Health Score

89 / 100
Full package analysis