【问题标题】:Is my implementation of Long Polling right?我的长轮询实施是否正确?
【发布时间】: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


【解决方案1】:

您的代码看起来是正确的,但如果您真的想使用这种方法,您仍然可以改进您的代码。

因此,在第一次加载页面时,您将收到该用户的所有新通知。现在将通知的最后一个 id 存储在会话中。例如,如果最后插入的通知 id 为 200,则只需在会话中添加 store 200。

当你对你的 php 函数进行 ajax 调用时,从会话中获取存储的通知 ID,并在你的 when 条件中添加另一个条件

->where("notification_id",">",$YOUR_ID);

因此,根据我的示例,您将获得在 id 200 之后添加的所有通知。现在在发送此结果之前,使用最新的通知 id 更新会话值。

在您的 AJAX 函数中,您可以简单地将新数据附加到现有数据中,而不是替换整个 HTML。因此,与其从数据库中获取所有行并替换 HTML,不如仅获取新添加的数据并追加。

【讨论】:

  • 虽然你真的应该考虑使用socket。 :) @cifer
  • 是的,肯定会,但由于时间限制,现在就这样吧。再次感谢。
  • 好的。您可以根据我的建议更新代码,如果有帮助,请标记答案已批准。再次感谢您,祝你好运。 :) @cifer
猜你喜欢
  • 1970-01-01
  • 2021-09-25
  • 1970-01-01
  • 2012-09-02
  • 2020-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-04
相关资源
最近更新 更多