0
0
Fork 0

Switch from deprecated ClusterWS/cws to ws package (#15932)

* Switch from deprecated ClusterWS/cws to ws package

Fixes #15184

Co-authored-by: Edho Arief <me@nanaya.pro>

* Make bufferutil and utf-8-validate optional dependencies

Co-authored-by: Edho Arief <me@nanaya.pro>
This commit is contained in:
Claire 2021-03-24 09:37:41 +01:00 committed by GitHub
parent c3aef491d6
commit 49814d5799
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 10 deletions

View file

@ -9,9 +9,9 @@ const redis = require('redis');
const pg = require('pg');
const log = require('npmlog');
const url = require('url');
const { WebSocketServer } = require('@clusterws/cws');
const uuid = require('uuid');
const fs = require('fs');
const WebSocket = require('ws');
const env = process.env.NODE_ENV || 'development';
const alwaysRequireAuth = process.env.LIMITED_FEDERATION_MODE === 'true' || process.env.WHITELIST_MODE === 'true' || process.env.AUTHORIZED_FETCH === 'true';
@ -766,7 +766,7 @@ const startWorker = (workerId) => {
});
});
const wss = new WebSocketServer({ server, verifyClient: wsVerifyClient });
const wss = new WebSocket.Server({ server, verifyClient: wsVerifyClient });
/**
* @typedef StreamParams
@ -999,6 +999,12 @@ const startWorker = (workerId) => {
req.requestId = uuid.v4();
req.remoteAddress = ws._socket.remoteAddress;
ws.isAlive = true;
ws.on('pong', () => {
ws.isAlive = true;
});
/**
* @type {WebSocketSession}
*/
@ -1048,7 +1054,17 @@ const startWorker = (workerId) => {
}
});
wss.startAutoPing(30000);
setInterval(() => {
wss.clients.forEach(ws => {
if (ws.isAlive === false) {
ws.terminate();
return;
}
ws.isAlive = false;
ws.ping('', false, true);
});
}, 30000);
attachServerWithConfig(server, address => {
log.info(`Worker ${workerId} now listening on ${address}`);