fix: Avoid removeAllListeners on sockets (#174)

This commit is contained in:
Maciej Hirsz
2019-09-03 16:07:01 +02:00
committed by GitHub
parent d297202b7e
commit 5b2c0ee37d
2 changed files with 41 additions and 36 deletions
+26 -28
View File
@@ -99,32 +99,10 @@ export default class Node {
this.lastMessage = timestamp();
this.socket = socket;
socket.on('message', (data) => {
const message = parseMessage(data);
if (!message) {
return;
}
this.onMessage(message);
});
socket.on('close', () => {
console.log(`${this.name} has disconnected`);
this.disconnect();
});
socket.on('error', (error) => {
console.error(`${this.name} has errored`, error);
this.disconnect();
});
socket.on('pong', () => {
this.latency = (timestamp() - this.pingStart) as Types.Milliseconds;
this.pingStart = 0 as Types.Timestamp;
});
socket.on('message', this.onMessageData);
socket.on('close', this.disconnect);
socket.on('error', this.disconnect);
socket.on('pong', this.onPong);
process.nextTick(() => {
// Handle cached messages
@@ -237,14 +215,29 @@ export default class Node {
return +(this.lastBlockAt || 0) as Types.Milliseconds;
}
private disconnect() {
this.socket.removeAllListeners();
private disconnect = () => {
console.log(`${this.name} has disconnected`);
this.socket.removeListener('message', this.onMessageData);
this.socket.removeListener('close', this.disconnect);
this.socket.removeListener('error', this.disconnect);
this.socket.removeListener('pong', this.onPong);
this.socket.close();
this.socket.terminate();
this.events.emit('disconnect');
}
private onMessageData = (data: WebSocket.Data) => {
const message = parseMessage(data);
if (!message) {
return;
}
this.onMessage(message);
}
private onMessage(message: Message) {
this.lastMessage = timestamp();
@@ -422,4 +415,9 @@ export default class Node {
return (+time - +this.lastBlockAt) as Types.Milliseconds;
}
private onPong = () => {
this.latency = (timestamp() - this.pingStart) as Types.Milliseconds;
this.pingStart = 0 as Types.Timestamp;
}
}