1
0
Fork 0
management/front/dkha-web-sz-main/node_modules/webpack-dev-server/lib/servers/WebsocketServer.js

73 lines
1.6 KiB
JavaScript
Raw Permalink Normal View History

2023-12-18 13:12:25 +08:00
'use strict';
/* eslint-disable
class-methods-use-this
*/
const ws = require('ws');
const BaseServer = require('./BaseServer');
module.exports = class WebsocketServer extends BaseServer {
constructor(server) {
super(server);
this.wsServer = new ws.Server({
2024-01-16 21:26:16 +08:00
noServer: true,
2023-12-18 13:12:25 +08:00
path: this.server.sockPath,
});
2024-01-16 21:26:16 +08:00
this.server.listeningApp.on('upgrade', (req, sock, head) => {
if (!this.wsServer.shouldHandle(req)) {
return;
}
this.wsServer.handleUpgrade(req, sock, head, (connection) => {
this.wsServer.emit('connection', connection, req);
});
});
2023-12-18 13:12:25 +08:00
this.wsServer.on('error', (err) => {
this.server.log.error(err.message);
});
2024-01-16 21:26:16 +08:00
const noop = () => {};
setInterval(() => {
this.wsServer.clients.forEach((socket) => {
if (socket.isAlive === false) {
return socket.terminate();
}
socket.isAlive = false;
socket.ping(noop);
});
}, this.server.heartbeatInterval);
2023-12-18 13:12:25 +08:00
}
send(connection, message) {
// prevent cases where the server is trying to send data while connection is closing
if (connection.readyState !== 1) {
return;
}
connection.send(message);
}
close(connection) {
connection.close();
}
// f should be passed the resulting connection and the connection headers
onConnection(f) {
this.wsServer.on('connection', (connection, req) => {
2024-01-16 21:26:16 +08:00
connection.isAlive = true;
connection.on('pong', () => {
connection.isAlive = true;
});
2023-12-18 13:12:25 +08:00
f(connection, req.headers);
});
}
onConnectionClose(connection, f) {
connection.on('close', f);
}
};