我的网站上的用户可以有朋友,朋友可以互相聊天。您还可以将状态设置为在线、离开和忙碌。
如果用户不再在网站上,则应将其状态设置为脱机。我想最好的方法是定期向服务器发送AJAX请求,然后在服务器停止接收这些消息时自动将状态设置为offline。
但是,发送这些消息的合适间隔是多少?5秒?1分钟?
如果这真的取决于way服务器的能力,有没有办法用PHP检查一下?一些常用的for服务器功能的示例间隔是多少?
发布于 2013-03-13 04:22:30
我会考虑使用这个>> http://www.bedroomlan.org/coding/detecting-%E2%80%98idle%E2%80%99-and-%E2%80%98away%E2%80%99-timeouts-javascript
然后修改以下内容:
setIdleTimeout(2000); // 2 seconds
setAwayTimeout(4000); // 4 seconds
document.onIdle = function() {$('#div_idle').css('opacity', '1');}
document.onAway = function() {$('#div_away').css('opacity', '1');}
document.onBack = function(isIdle, isAway) {
if (isIdle) $('#div_idle').css('opacity', '0.2');
if (isAway) $('#div_away').css('opacity', '0.2');
}类似这样的东西:
setIdleTimeout(120000); // Make these two
setAwayTimeout(360000); // a lot longer
document.onIdle = function() {
// your ajax calls for when they go
}
document.onAway = function() {
// code here to maybe log them out or what ever you wish to do via ajax.
}
document.onBack = function(isIdle, isAway) {
// code here for when they return
}这样你就减少了请求。
快速编辑:
关闭浏览器时注销可以通过jQuery .unload()实现:
$(window).unload(function() {
// ajax call when they just go, like they don't even care!
});发布于 2013-03-13 04:24:19
这实际上更多的是可用性/期望问题,而不是编码问题。如果服务器上的负载不是很高,这些甚至不会被注意到。我怀疑你是否需要每5秒检查一次,但这可能是可行的,特别是如果它只是一个用户数量有限的个人网站。在高负载条件下,您可能会使其大大提高。
https://stackoverflow.com/questions/15371185
复制相似问题