I would like to share my mistake on the same issue:
public createClient() { this.client = new net.Socket(); this.client.connect(this.port, this.ip, () => { this.emit("connect"); }); this.client.on("data", (data) => { this.emit("data", data); }); this.client.on("close",function () => { this.emit("close"); }); }
in the close event, using a normal function, and this (the cause of the error) leads to the closing cycle of the close event, resulting in a stack size error.
this.client.on("close", () => { this.emit("close"); });
Using arrow func. (or by copying real this into something like _this and using the inside function) the problem is solved.
It is actually unaware of the context of the function (in the callback) or negligence ..
source share