我试图从断开连接中发出两件事,其中一件是重定向。
服务器端:
socket.on('disconnect', () => {
socket.broadcast.emit('user-disconnected', users[socket.id]);
socket.emit('redirect', destination);
console.log('disconnect: '+ users[socket.id]);
delete users[socket.id];
});
客户端:
这被称为:
socket.on('user-disconnected', username => {
appendMessage(username+ ' est parti.');
removeUser(username);
});
这不是:
socket.on('redirect', function(destination) { // never gets called…
window.location.href = destination;
});
你知道为什么我的服务器端被调用,用户断开连接,但我的重定向没有被调用吗?
非常感谢你的帮助!
您正在发送:
socket.emit('redirect', destination);
连接到刚刚断开连接的插座。您实际上是在尝试从disconnect
事件发送消息。该套接字无法发送更多消息。您可以执行socket.broadcast.emit(...)
以发送到所有其他套接字,但不能发送到这个套接字。