致力于php / ajax聊天系统

时间:2011-10-23 16:22:45

标签: php ajax events asynchronous

我一直在研究ajax / php聊天系统,用户可以在这里聊天。我担心服务器负载,它最初编程的方式是每x秒自动刷新div(聊天框)它只是这是用户活动,因为我超时他们的不活动。如果它们在10分钟左右保持不活动状态,它们将显示为空闲状态,系统将停止刷新。然后我查看了使用HTML5的服务器发送事件,但是效果不错,但并非所有浏览器都能使用它。

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

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

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