gpt4 book ai didi

node.js - socket.io 强制断开客户端

转载 作者:IT老高 更新时间:2023-10-28 23:16:50 27 4
gpt4 key购买 nike

我可能在这里做错了什么,但我无法理解这一点。

以下不起作用:

客户

$("#disconnectButton").click(function() {
socket.emit("disconnect");
});

服务器

socket.on("disconnect", function() {
console.log("this never gets called");
});

我不能从客户端手动调用断开事件吗?因为以下工作:

客户

$("#disconnectButton").click(function() {
socket.emit("whatever");
});

服务器

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

最佳答案

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

所以:

$("#disconnectButton").click(function() {
socket.emit("disconnect");
});

替换:

$("#disconnectButton").click(function() {
socket.disconnect();
});

关于node.js - socket.io 强制断开客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20710858/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com