1

目前,每当用户切换页面时,此代码块都会立即运行:

// when the user disconnects.. perform this
socket.on('disconnect', function(){
    delete SiteRooms[socket.username];
    io.sockets.emit('updateusers', usernames);
    io.sockets.emit('updateroomusers', SiteRooms);
});

如果用户只是在我的网站上切换页面,我不想运行它,有什么方法可以做到这一点吗?

我正在考虑一个计时器,也许将其设置为 50 秒 - 如果当时不存在,也许将其设置为 30 秒:然后断开连接。但我不知道这是否是一个好的解决方案,也不知道如何为其编写实际代码..

有任何想法吗?

4

2 回答 2

1

The event firing is correct. When a user switches pages, the whole execution context of that page ends, and any scripts are stopped. This includes the client code for Socket.IO.

You have a couple choices. One choice is to load your page content with AJAX. This isn't always ideal... it depends on what your site does and how it is built.

Another option is to build in that delay yourself. Wherever you keep your user data, just add a property to that object that holds a timeout for however many seconds you'd like. If the user reconnects, clear the timeout before it fires.

于 2013-01-16T03:38:44.843 回答
0

您可以通过更改页面内容的脚本来更改链接。我没有经验,但我认为 JADE 有助于做这样的事情。您在 github 上有一些示例:

https://github.com/visionmedia/jade

我认为更换积木可能是您的解决方案。

于 2013-01-16T02:33:03.620 回答