The difference between disconnecting, closing, and destroying socket.io methods

I am working on a simple chat application using and .

I am trying to end the connection, for example, when the user decided to leave the namespace or something similar to logging out, which does not exit the application or does not start the reboot.

I checked this @ GitHub issue, as well as these issues,

They offer different methods such as disconnect , close , etc.

In accordance with my own experiments based on them,

Both disconnect , close methods set the socket connected property to false and the disconnected property true, as you can see below.

enter image description here

I also noticed the destroy method in the socket prototype:

enter image description here

Can someone describe what exactly these methods are for and how they differ from each other ??


Side note: it would be great if someone could share a documentation link for these methods

+6
source share
1 answer

These are the typescript definitions in VSCODE:

  /** * Disconnects the socket manually * @return This Socket */ close():Socket; /** * @see close() */ disconnect():Socket; 

Not too helpful. Looking at the source code of Socket.IO for close :

 /** * Closes the underlying connection. * * @api private */ Client.prototype.close = function(){ if ('open' == this.conn.readyState) { debug('forcing transport close'); this.conn.close(); this.onclose('forced server close'); } }; 

And for disconnect :

 /** * Disconnects from all namespaces and closes transport. * * @api private */ Client.prototype.disconnect = function(){ for (var id in this.sockets) { if (this.sockets.hasOwnProperty(id)) { this.sockets[id].disconnect(); } } this.sockets = {}; this.close(); }; 

It seems that disconnect calls close after doing the extra work of disconnecting. I would recommend calling disconnect when you want to close, rather than just calling close .

+1
source

Source: https://habr.com/ru/post/1236184/


All Articles