2

I am trying to close a socket and handle the event accordingly on the server. I tried using client.destroy() and client.end() and catching the events 'end', 'close', 'error' and 'timeout' and none of the callbacks are firing.

I use this to kill the socket:

setTimeout(() => { 
   console.log("destroy"); 
   e._destroyed = true; 
   e._client.end(); 
   e._client.destroy(); 
   logger.debug("Test"); 
}, 1000);

and this is the constructor of my connection class:

constructor(socket, id, onData, onEnd)
{
    this._onDataHandler = onData;
    this._onEndHandler = onEnd;
    this._socket = socket;
    this._socket.setEncoding('UTF-8');
    this._socket.on('data', this._onData.bind(this));
    this._socket.on('end', this._onEnd.bind(this));
    this._socket.on('close', this._onEnd.bind(this));
    this._socket.on('error', this._onEnd.bind(this));
    this._socket.on('timeout', this._onEnd.bind(this));
    this._id = id;
}

This is the onEnd method:

_onEnd()
{
    if(this.onEndHandler)
        this._onEndHandler(this);
}

which in turn should call this:

_onEnd(connection) {
    console.log("Killing connection " + connection);
    let index = this._connections.id;
    this._removeConnection(this._subscriptions, index);
    this._connections[index] = null;
}

This is the output I am getting:

destroy [26.05.2017 - 14:05:56] [Timeout.static.main.setTimeout [as _onTimeout] (Core.Main:20:118)] [DEBUG] Test

and nothing further. So the killing connection is never called, besides being bound to all possible "connection-ending" events. Is this expected ? Am I missing a method or some config here ? Or must the error be somewhere else ?

PS: The socket is closed on the client, trying to write to it will give the according error.

Laazo
  • 467
  • 8
  • 22
fis
  • 87
  • 6
  • This could help (`socket.disconnect()`) : https://stackoverflow.com/questions/18126677/node-js-socket-io-close-client-connection – Jeremy Thille Jun 26 '17 at 12:13
  • This gives me `TypeError: e._client.disconnect is not a function`. The socket is created via net.connect(), so it definitely is a socket object. It is also not listed in the official docs: https://nodejs.org/api/net.html – fis Jun 26 '17 at 12:21
  • Oh, I thought you were using Socket.io. Any reason why you're not using it? Cause it's super simple. – Jeremy Thille Jun 26 '17 at 12:25
  • I want to use as few dependencies as possible, and since node already has a net library onboard, I see no reason as not to use it. Still, I think it shouldn't be that hard to orderly close a connection. – fis Jun 26 '17 at 12:31
  • Where is your onEndHandler? Seems like you have two _onEnd methods? – Uirri Jun 26 '17 at 12:40
  • oh god, I missed the _ in if(this.onEndHandler). Sorry guys, was just my stupidity. – fis Jun 26 '17 at 13:02

1 Answers1

1

I had a typo in my code:

_onEnd()
{
    if(this.onEndHandler)
        this._onEndHandler(this);
}

is missing the underscore in the if clause. Now it works as expected.

fis
  • 87
  • 6