close() { clearTimeout(this.timoutId); if (!this.ws) { return; } this._unbindWsEvents(); this.ws.terminate(); this.ws = null; }
// Check for died connections at regular intervals. setInterval(function () { wsServer.clients.forEach(function (connection) { if (connection.isAlive === false) { console.log("Connection died", connection.id); return connection.terminate(); } // Request the client to respond with pong. Client does this automatically. connection.isAlive = false; connection.ping(function () {}); }); }, 30000);
_ping() { clearTimeout(this.pongTimeout); this.pongTimeout = null; this.logger.silly('Sending ping', {category: 'bybit-ws'}); this.ws.send(JSON.stringify({op: 'ping'})); this.pongTimeout = setTimeout(() => { this.logger.info('Pong timeout', {category: 'bybit-ws'}); this._teardown(); this.ws.terminate(); }, this.options.pongTimeout); }
// This creates a super basic "echo" WebSocket server wss.on('connection', function connection(ws) { const banned = Data.parseFile(srcPath + '/../data/banned.json'); if (banned.includes(ws._socket.remoteAddress)) { return ws.terminate(); } // create our adapter const stream = new WebSocketStream(); stream.ipAddress = ws._socket.remoteAddress; // and attach the raw WebSocket stream.attach(ws); // Register all of the input events (login, etc.) state.InputEventManager.attach(stream); stream.write("Establishing WebSocket connection...\n"); Logger.log("New WebSocket client connected..."); // TIP: bundles/pinwheel-input/input-events/intro.js stream.emit(Config.get('introEvent', 'intro'), stream); });
start() { const headers = { Authorization: `Bearer ${this.getJwtToken('GET', '/', '')}`, perMessageDeflate: false, }; this.shouldAttemptReconnect = Boolean(this.reconnectInterval); this.isConnected = false; this.socket = new WebSocket(this.url, this.protocols, { headers }); this.socket.onmessage = this._onMessage.bind(this); this.socket.onopen = this._onOpen.bind(this); this.socket.onerror = this._onError.bind(this); this.socket.onclose = this._onClose.bind(this); interval(async (iteration, _) => { debug('heartbeat check', { iteration }); try { await this.ping(); } catch (err) { this.socket.terminate(); this.start(); } }, 30 * 1000); }
if (error) { console.log("Connection send error", connection.id, "error", error); connection.terminate();
release: function () { if (onError) socket.removeListener('error', onError); if (--refs === 0) socket.terminate(); }, protocol: 'WS'
ws.on('error', (e) => { if (e.code && (e.code === 'ECONNRESET' || e.code === 'ECONNREFUSED')) { ws.terminate(); if (count > 10) { proxRj(new Error('Docker ws server timeout exceeded'));
// This creates a super basic "echo" WebSocket server wss.on('connection', function connection(ws) { const banned = Data.parseFile(srcPath + '/../data/banned.json'); if (banned.includes(ws._socket.remoteAddress)) { return ws.terminate(); } // create our adapter const stream = new WebSocketStream(); stream.ipAddress = ws._socket.remoteAddress; // and attach the raw WebSocket stream.attach(ws); // Register all of the input events (login, etc.) state.InputEventManager.attach(stream); stream.write("Establishing WebSocket connection...\n"); Logger.log("New WebSocket client connected..."); // TIP: bundles/pinwheel-input/input-events/intro.js stream.emit(Config.get('introEvent', 'intro'), stream); });