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
+15 -8
View File
@@ -24,10 +24,10 @@ export default class Feed {
this.id = nextId(); this.id = nextId();
this.socket = socket; this.socket = socket;
socket.on('message', (data) => this.handleCommand(data.toString())); socket.on('message', this.handleCommand);
socket.on('error', () => this.disconnect()); socket.on('error', this.disconnect);
socket.on('close', () => this.disconnect()); socket.on('close', this.disconnect);
socket.on('pong', () => this.waitingForPong = false); socket.on('pong', this.onPong);
} }
public static feedVersion(): FeedMessage.Message { public static feedVersion(): FeedMessage.Message {
@@ -230,8 +230,8 @@ export default class Feed {
this.socket.send(data, this.handleError); this.socket.send(data, this.handleError);
} }
private handleCommand(cmd: string) { private handleCommand = (data: WebSocket.Data) => {
const [tag, payload] = cmd.split(':', 2) as [string, Maybe<string>]; const [tag, payload] = data.toString().split(':', 2) as [string, Maybe<string>];
if (!payload) { if (!payload) {
return; return;
@@ -272,10 +272,17 @@ export default class Feed {
} }
} }
private disconnect() { private disconnect = () => {
this.socket.removeAllListeners(); this.socket.removeListener('message', this.handleCommand);
this.socket.removeListener('error', this.disconnect);
this.socket.removeListener('close', this.disconnect);
this.socket.removeListener('pong', this.onPong);
this.socket.terminate(); this.socket.terminate();
this.events.emit('disconnect'); this.events.emit('disconnect');
} }
private onPong = () => {
this.waitingForPong = false;
}
} }
+26 -28
View File
@@ -99,32 +99,10 @@ export default class Node {
this.lastMessage = timestamp(); this.lastMessage = timestamp();
this.socket = socket; this.socket = socket;
socket.on('message', (data) => { socket.on('message', this.onMessageData);
const message = parseMessage(data); socket.on('close', this.disconnect);
socket.on('error', this.disconnect);
if (!message) { socket.on('pong', this.onPong);
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;
});
process.nextTick(() => { process.nextTick(() => {
// Handle cached messages // Handle cached messages
@@ -237,14 +215,29 @@ export default class Node {
return +(this.lastBlockAt || 0) as Types.Milliseconds; return +(this.lastBlockAt || 0) as Types.Milliseconds;
} }
private disconnect() { private disconnect = () => {
this.socket.removeAllListeners(); 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.close();
this.socket.terminate(); this.socket.terminate();
this.events.emit('disconnect'); this.events.emit('disconnect');
} }
private onMessageData = (data: WebSocket.Data) => {
const message = parseMessage(data);
if (!message) {
return;
}
this.onMessage(message);
}
private onMessage(message: Message) { private onMessage(message: Message) {
this.lastMessage = timestamp(); this.lastMessage = timestamp();
@@ -422,4 +415,9 @@ export default class Node {
return (+time - +this.lastBlockAt) as Types.Milliseconds; return (+time - +this.lastBlockAt) as Types.Milliseconds;
} }
private onPong = () => {
this.latency = (timestamp() - this.pingStart) as Types.Milliseconds;
this.pingStart = 0 as Types.Timestamp;
}
} }