socket.io强制断开客户端
我可能在这里做错了事,但是我无法理解这一点。
以下不起作用:
客户
$("#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(); });