【发布时间】:2018-09-09 12:14:10
【问题描述】:
我正在尝试为我们的基于 Web 的系统实现一个简单的通知系统。我已经阅读了有关 websocket、ratchet、socket.io 等的信息。但这里的问题是,我真的没有足够的时间来实现这些,所以我决定采用长轮询来解决。但是,我不确定我是否真的做对了,或者我只是在上传系统后向我的服务器发送垃圾邮件。我还阅读了几个关于长轮询的主题,但我不太确定这些主题与我目前使用的主题有何不同。所以我担心的是:
1.谁能告诉我我是否在做长轮询?
2.如果不是,我应该对我的代码进行哪些更改?
HTML:
<li class="dropdown notifications-menu" id="notif-div">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
<i class="fa fa-bell-o"></i><?php if (!empty($notif_count)): ?><span class="label label-warning" id="notif-count"><?php echo $notif_count->notif_count?></span>
<?php endif; ?>
</a>
<ul class="dropdown-menu">
<li>
<ul class="menu">
<?php foreach ($notifications as $key => $notification): ?>
<?php if ($notification->type_of_notification == 'Comment'): ?>
<li>
<a href="<?php echo $notification->href ?>">
<i class="ion-chatbubble"></i> <?php echo $notification->title_content ?>
</a>
</li>
<?php elseif ($notification->type_of_notification == 'Reply'):?>
<li>
<a href="<?php echo $notification->href ?>">
<i class="ion-chatbubbles"></i> <?php echo $notification->title_content ?>
</a>
</li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
</li>
</ul>
</li>
JS:
(function() {
var notif = function(){
var user_id = {
user_id: "<?php echo $traveller_details->user_id ?>"
};
$.ajax({
url: "/Home/get_notif",
type: "POST",
data: user_id,
success: function (data){
$('#notif-div').html(data);
}
});
};
setInterval(function(){
notif();
}, 60000);
})();
PHP:
public function get_notif()
{
$this->data['notif__count'] = $this->Homes->get_notif_count($this->input->post('user_id'));
$this->data['notifications'] = $this->Homes->get_notifications($this->input->post('user_id'));
echo '
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
<i class="fa fa-bell-o"></i>';
if (!empty($this->data['notif__count'])) {
echo '<span class="label label-warning" id="notif-count">'.$this->data['notif__count']->notif_count.'</span>';
}
echo '</a>';
echo '<ul class="dropdown-menu">
<li>
<ul class="menu">';
foreach ($this->data['notifications'] as $key => $notification) {
if ($notification->type_of_notification == 'Comment') {
echo '
<li>
<a href="'.$notification->href.'">
<i class="ion-chatbubble"></i> '.$notification->title_content.'
</a>
</li>
';
}elseif ($notification->type_of_notification == 'Reply') {
echo '
<li>
<a href="'.$notification->href.'">
<i class="ion-chatbubbles"></i> '.$notification->title_content.'
</a>
</li>
';
}
}
echo '
</ul>
</li>
</ul>
';
}
我对长轮询不是很熟悉,所以我向你道歉。顺便说一句,我正在使用 Codeigniter 框架。提前致谢。
【问题讨论】:
-
你试过了吗?
-
@FunkFortyNiner 是的,它正在工作。但是,当我们向有经验的人咨询时,他告诉我们我们的流程是有问题的,因为它会向我们的服务器发送垃圾邮件 (?)。
标签: javascript php jquery html long-polling