我一直在开发一个ajax/php聊天系统,在这个系统中,用户可以明显地互相聊天。我关心的是服务器负载,它最初的编程方式是每x秒自动刷新div (聊天框),它只这样做是因为用户是活动的,因为我超时了他们的不活动。如果它们保持非活动状态10分钟左右,它们将显示为空闲,然后系统将停止刷新。然后,我用HTML5研究了服务器发送的事件,它工作得很好,但并不是所有的浏览器都能使用它。
有没有人有更好的解决方案,或者div刷新现在还可以吗?希望有人能帮上忙,谢谢!
发布于 2011-10-26 22:24:19
考虑使用COMET,或者看看Ajax推送引擎:Link
使用COMET的聊天系统示例:Link
发布于 2019-06-28 19:13:51
// jQuery Document
$(document).ready(function(){
});
//jQuery Document
$(document).ready(function(){
//If user wants to end session
$("#exit").click(function(){
var exit = confirm("Are you sure you want to end the session?");
if(exit==true){window.location = 'index.php?logout=true';}
});
});
//If user submits the form
$("#submitmsg").click(function(){
var clientmsg = $("#usermsg").val();
$.post("post.php", {text: clientmsg});
$("#usermsg").attr("value", "");
loadLog;
return false;
});
function loadLog(){
var oldscrollHeight = $("#chatbox").attr("scrollHeight") - 20; //Scroll height before the request
$.ajax({
url: "log.html",
cache: false,
success: function(html){
$("#chatbox").html(html); //Insert chat log into the #chatbox div
//Auto-scroll
var newscrollHeight = $("#chatbox").attr("scrollHeight") - 20; //Scroll height after the request
if(newscrollHeight > oldscrollHeight){
$("#chatbox").animate({ scrollTop: newscrollHeight }, 'normal'); //Autoscroll to bottom of div
}
},
});
}
setInterval (loadLog, 1000);
</script>
https://stackoverflow.com/questions/7867411
复制相似问题