Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js socket.io强制断开客户端连接_Node.js_Sockets_Websocket_Socket.io - Fatal编程技术网

Node.js socket.io强制断开客户端连接

Node.js socket.io强制断开客户端连接,node.js,sockets,websocket,socket.io,Node.js,Sockets,Websocket,Socket.io,我可能在这里做错了什么,但我无法改变我的想法 以下操作不起作用: 客户端 $("#disconnectButton").click(function() { socket.emit("disconnect"); }); $("#disconnectButton").click(function() { socket.emit("whatever"); }); 服务器 socket.on("disconnect", function() { console.log("this ne

我可能在这里做错了什么,但我无法改变我的想法

以下操作不起作用:

客户端

$("#disconnectButton").click(function() {
  socket.emit("disconnect");
});
$("#disconnectButton").click(function() {
  socket.emit("whatever");
});
服务器

socket.on("disconnect", function() {
  console.log("this never gets called");
});
socket.on("whatever", function() {
  socket.disconnect();
  console.log("this gets called and disconnects the client");
});
我不能从客户端手动调用断开连接事件吗?因为以下工作:

客户端

$("#disconnectButton").click(function() {
  socket.emit("disconnect");
});
$("#disconnectButton").click(function() {
  socket.emit("whatever");
});
服务器

socket.on("disconnect", function() {
  console.log("this never gets called");
});
socket.on("whatever", function() {
  socket.disconnect();
  console.log("this gets called and disconnects the client");
});

“disconnect”事件是Socket.io事件,如果使用emit调用“disconnect”,可能会导致其他问题

因此:

替换:

$("#disconnectButton").click(function() {
  socket.disconnect();
});
目前,
socket.emit('disconnect')
socket.disconnect()
不起作用。 断开与套接字服务器连接的正确语法如下

socket.close()

你强迫断开连接的理由是什么?如果您试图引导某人违反限制或安全限制,您应该不那么优雅地终止连接。我有一个聊天应用程序,我想在其中添加一个“断开连接”选项,以便某人可以手动断开与websocket的连接(有点像在用户点击刷新时进行复制)这不会为我调用
断开连接
事件。对我有效。非常感谢。