T J*_*T J 8 javascript node.js socket.io
我正在使用node.js和socket.io开发一个简单的聊天应用程序.
我正在尝试终止连接,例如当用户选择离开命名空间或类似于logout时,它不会退出应用程序或触发重新加载.
我已经检查了这个问题@ GitHub,以及这些问题,
他们提出不同的方法,如disconnect,close等.
根据我自己的实验,基于这些,
这两种disconnect,close方法设置套接字的connected属性设置为false和disconnected属性设置为true,你可以看到下面.
我还注意到destroy套接字原型中的一个方法:
有人可以描述这些方法的确切含义,以及它们彼此之间的差异......?
旁注:如果有人可以分享对这些方法的文档的任何引用,那就太棒了
Har*_*ner 11
这些是VSCODE 中的打字稿定义:
/**
* Disconnects the socket manually
* @return This Socket
*/
close():Socket;
/**
* @see close()
*/
disconnect():Socket;
Run Code Online (Sandbox Code Playgroud)
不太有帮助。查看 Socket.IO 源代码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');
}
};
Run Code Online (Sandbox Code Playgroud)
对于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();
};
Run Code Online (Sandbox Code Playgroud)
看起来像是在进行额外的断开连接工作后进行disconnect的呼叫close。我建议disconnect您在想要关闭时致电,而不是仅仅致电close.
| 归档时间: |
|
| 查看次数: |
1381 次 |
| 最近记录: |