How to close TCP socket in Node.js

Published:
Published:

The reason behind "connect, disconnect and then connect again" behavior might be that the old socket was still listening because it was half closed. Read this.

So it depends on the protocol and if the server is a rude guy and in case of any error it immediately closes the connection, then it must use the destroy function.

import { Transform } from 'stream';

class Protocol extends Transform {
  constructor(socket) {
    super();
    this._socket = socket;
  }

  disconnect() {
    if (this._socket) {
      this._socket.destroy();
      delete this._socket;
    }
  }

  // override from stream.Transform
  _transform(chunk, encoding, done) {
    // ...
  }
}

Rate this page