正在开发php/ajax聊天系统


Working on php/ajax chat system

我一直致力于一个ajax/php聊天系统,用户可以明显地彼此聊天。我担心服务器负载,它最初的编程方式是每x秒自动刷新div(聊天框),它只做这是用户是活跃的,因为我超时了他们的不活动。如果它们保持非活动状态10分钟左右,它们将显示为空闲,然后系统将停止刷新。然后我研究了HTML5的服务器发送事件,它工作得很好,但不是所有的浏览器都能工作。

有没有人有更好的解决方案,或者是div刷新现在ok ?希望有人能帮忙,谢谢!

考虑使用COMET,或者看看Ajax Push Engine: Link

使用COMET的聊天系统示例:Link

// 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>