Php 类似StackOverflow的Ajax帖子评论
我的问题是,在StackOverflow上做评论系统的更好方法是什么, 我的意思是,我从我的浏览器发送请求,每个人都会看到这个评论(或在其他浏览器中),而不会像聊天一样刷新页面 我的解决方案是使用Php 类似StackOverflow的Ajax帖子评论,php,jquery,ajax,Php,Jquery,Ajax,我的问题是,在StackOverflow上做评论系统的更好方法是什么, 我的意思是,我从我的浏览器发送请求,每个人都会看到这个评论(或在其他浏览器中),而不会像聊天一样刷新页面 我的解决方案是使用setInterval,但我认为必须有另一种方法 $(document).ready(function() { get(); $('#send').click(function() { $.post('http://localhost/mvc.com/comment/post', {
setInterval
,但我认为必须有另一种方法
$(document).ready(function() {
get();
$('#send').click(function() {
$.post('http://localhost/mvc.com/comment/post', {
n_id: parseInt(newsId),
user_id: $('#uid').val(),
text: $('#content').val(),
token: $('#token').val()
}, function (ret) {
if (ret.comm.err) {
$('.f').empty().prepend('<li id=e><h3 style="color: red">ERROR</h3></li>');
return false;
}
get();
});
setInterval(get,3000);
});
$('#content').keypress(function(e){
var key = e.which;
var cnt=$(this).val().length;
var max=100;
var tot=parseInt(max-cnt);
if(key >= 33 || key == 13 || key == 32) {
if (parseInt(tot) <= 0) {
e.preventDefault();
}
}
});
function get() {
$.post('http://localhost/mvc.com/comment', {get: parseInt(newsId)}, function (ret) {
$('.f').empty();
for (var key in ret.comm) {
$('.f').append('<li class=c id=' + ret.comm[key].id +
'><span>' + ret.comm[key].name + '</span><hr><br>' + ret.comm[key].text + '</li>');
}
});
}
$(文档).ready(函数(){
get();
$(“#发送”)。单击(函数(){
$.post($)http://localhost/mvc.com/comment/post', {
n_id:parseInt(newsId),
用户id:$('#uid').val(),
文本:$('#content').val(),
令牌:$(“#令牌”).val()
},功能(ret){
如果(返回通信错误){
$('.f').empty().prepend('错误 ');
返回false;
}
get();
});
设置间隔(get,3000);
});
$(“#内容”)。按键(功能(e){
var-key=e.which;
var cnt=$(this).val().length;
var max=100;
var tot=parseInt(最大cnt);
如果(键>=33 | |键==13 | |键==32){
if(parseInt(tot)虽然我看到您前面提到的方法被用于实时更新,但这不是正确的方法
您将需要使用web套接字,这是实时web应用程序的事实
Web套接字本身就是一个主题,我可以继续,但这里有一个链接可以让您开始使用它们:您不需要设置间隔。您可以做的就是所谓的长轮询:
Javascript:定义在完成时调用自身的ajax函数:
function poll(){
$.ajax({
type: "POST",
url: url,
data: data,
success: function(msg){
update_poll(msg);//here you update your span, div, whatever what contains this comment
},
dataType: "text",
complete: function(){
poll();//here you call it again
}
});
}
$(document).ready(function(){
poll();//call it just once
});
PHP:启动一分钟循环,每3秒检查一次数据库中的新条目:
if(isset($_POST['n_id'])){
$n_id = (int) $_POST['n_id'];
$time = time();
while((time() - $time) < 60) {
$last entry = get_last_entry($n_id);
if($last entry){
echo $last_entry;//if found new entry, echo it out and break the loop
break;
}
sleep(3);//wait 3 seconds
}
}
if(isset($\u POST['n\u id'])){
$n_id=(int)$\u POST['n_id'];
$time=time();
而((time()-$time)<60){
$last entry=get_last_entry($n_id);
如果($最后一项){
echo$last_entry;//如果找到新条目,则将其回显并中断循环
打破
}
睡眠(3);//等待3秒钟
}
}
是一个比setInterval更好的解决方案。添加setInterval
而不清除每个#send
上的前一个,听起来像是一个危险的想法。var-tot=parseInt(max-cnt);
而不是简单的var-tot=max-cnt;
看起来很奇怪。parseInt(tot)如果您熟悉.Net技术,请尝试在Signal中使用它,它为您提供了很好的解决方案。请求会挂起几分钟?您知道,这不是一个好主意。例如,您可能有30秒的请求超时时间。此外,由于而,它会消耗更多服务器资源。您也无法更改与请求相关的内容(额外的强制更新或更改请求参数或间隔)在客户端使用此方法进行请求。@Regent没有什么是完美的,但我已经使用多年,它工作正常,这是最简单的方法。但“它工作”并不意味着它是好的(我不谈论完美)顺便说一句。假设你想检查邮箱中是否有新邮件。你不是一天检查一次,而是整天呆在邮箱附近,邮箱每小时都会重新检查新邮件。听起来“OK”吗?你想一整天都呆在邮箱附近吗?@Regent坦白地说,我只用它来检查我的应用程序中用户操作的注册表项,每隔几秒钟就会出现一次,所以它会不断闪烁。我不知道OP的应用程序是什么。顺便说一句,我也可以在这个循环中检查几个不同的更改,即使是新的e邮件。