致力于php/ajax聊天系统

发布于 2024-12-11 21:03:08 字数 238 浏览 0 评论 0原文

我一直在开发一种 ajax/php 聊天系统,用户可以在其中明显地互相聊天。我担心服务器负载,它最初的编程方式是每 x 秒自动刷新 div(聊天框),它只会这样做,因为用户处于活动状态,因为我超时了他们的不活动状态。如果它们保持不活动状态 10 分钟左右,它们将显示为空闲,然后系统将停止刷新。然后我研究了 HTML5 的服务器发送事件,它运行良好,但并非所有浏览器都可以使用它。

有谁有更好的解决方案或者div刷新现在可以吗?希望有人帮忙谢谢!

I have been working on an ajax/php chat system where users can obviously chat with one another. Im concerned about server load, the way it was originally programmed was auto refresh div (chat box) every x seconds it only did this is the user was active as i timed out their inactivity. If they remained inactive for 10 minutes or so they would be shown as idle the system would then stop refreshing. Then i looked into Server-Sent Events with HTML5 which worked well however not all browsers work with this.

Does anyone have a better solution or is the div refresh ok for now? Hope someone can help thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清引 2024-12-18 21:03:08

考虑使用 COMET,或者看一下 Ajax Push Engine:链接

使用 COMET 的聊天系统的示例: 链接

Consider using COMET, or take a look at Ajax Push Engine: Link

An example of a chat system using COMET: Link

葬花如无物 2024-12-18 21:03:08
// 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>

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文