且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何知道一个用户离开网站或立即关闭浏览器窗口,在服务器端?

更新时间:2023-12-01 21:06:10

由于有很多事情可以中断用户的访问就像失去联系,或浏览器崩溃;你应该依靠客户端告诉服务器嘿,我还在这里。并假定他们离开时,他们停止这样做,这样,而不是让客户告诉他们离开了服务器。

Since there are many things that can interrupt a user's visit like a lost connection, or a browser crash; you should rely on the the client to tell the server "Hey I am still here." and assume they left when they stop doing so that rather than have the client tell the server they left.

要做到这一点,你可以使用什么被称为心跳:结果
http://ajaxpatterns.org/Heartbeat

To accomplish this, you can use what's known as the "Heartbeat":
http://ajaxpatterns.org/Heartbeat

您可以调整心跳间隔这样效果会更直接。这样做,你也可以调整你认为是用户活动。依托功能燃煤 onunload的将导致谁离开自己的电脑,但没有关闭浏览器窗口,用户似乎是积极的。

You can tweak the heartbeat interval so the effect can be more immediate. Doing that you can also tweak what you consider "user activity." Relying on functions fired onunload will cause users who left their computer but didn't close their browser window appear to be "active."