fis
fis

Reputation: 87

Closing a net socket does not cause event on server

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.

Upvotes: 2

Views: 752

Answers (1)

fis
fis

Reputation: 87

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.

Upvotes: 1

Related Questions