cvmts: Fix WebSocket errors causing process crashes
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
import { WebSocket } from 'ws';
|
||||
import NetworkClient from '../NetworkClient.js';
|
||||
import EventEmitter from 'events';
|
||||
import pino from 'pino';
|
||||
|
||||
export default class WSClient extends EventEmitter implements NetworkClient {
|
||||
socket: WebSocket;
|
||||
ip: string;
|
||||
private logger = pino({ name: "CVMTS.WebsocketClient" });
|
||||
|
||||
constructor(ws: WebSocket, ip: string) {
|
||||
super();
|
||||
@@ -20,6 +22,10 @@ export default class WSClient extends EventEmitter implements NetworkClient {
|
||||
this.emit('msg', buf.toString('utf-8'));
|
||||
});
|
||||
|
||||
this.socket.on('error', (err: Error) => {
|
||||
this.logger.error(err, 'WebSocket recv error');
|
||||
})
|
||||
|
||||
this.socket.on('close', () => {
|
||||
this.emit('disconnect');
|
||||
});
|
||||
@@ -35,11 +41,13 @@ export default class WSClient extends EventEmitter implements NetworkClient {
|
||||
|
||||
send(msg: string): Promise<void> {
|
||||
return new Promise((res, rej) => {
|
||||
if (!this.isOpen()) res();
|
||||
if (!this.isOpen()) return res();
|
||||
|
||||
this.socket.send(msg, (err) => {
|
||||
if (err) {
|
||||
rej(err);
|
||||
this.logger.error(err, 'WebSocket send error');
|
||||
this.close();
|
||||
res();
|
||||
return;
|
||||
}
|
||||
res();
|
||||
@@ -49,11 +57,13 @@ export default class WSClient extends EventEmitter implements NetworkClient {
|
||||
|
||||
sendBinary(msg: Uint8Array): Promise<void> {
|
||||
return new Promise((res, rej) => {
|
||||
if (!this.isOpen()) res();
|
||||
if (!this.isOpen()) return res();
|
||||
|
||||
this.socket.send(msg, (err) => {
|
||||
if (err) {
|
||||
rej(err);
|
||||
this.logger.error(err, 'WebSocket send error');
|
||||
this.close();
|
||||
res();
|
||||
return;
|
||||
}
|
||||
res();
|
||||
|
||||
Reference in New Issue
Block a user