【问题标题】:AJAX long-polling : How do I make this code long-polling?AJAX 长轮询:如何使这段代码长轮询?
【发布时间】:2014-08-10 18:05:34
【问题描述】:

最初在我理解长轮询之前,我有这个代码:

     var updateMessages = function() {
        var conv_id = [];
        var lastMessages = [];

        $('.user').each(function(){
            conv_id.push($(this).find('p').attr('id'));
        });
        if($('.rightP').find('.msg .msg_block').length!=0){
            $('.rightP').find('.msg .msg_block').each(function(){
                if(($('.rightP').find('.msg .msg_block p').length)==0){
                }else {
                    lastMessages.push($(this).find('p:last-child')[0].dataset.created_at);
                }
            });
        }
        $.ajax({
            type: "POST",
            url: 'create/populate',
            data: {
                'from': lastMessages,
                'conv_id':conv_id
            },
            success: function(messages) {
                console.log(messages);
                $.each(messages, function() {
                    appendMessage(this);
                });
            },
            error: function(xhr,textStatus,errorThrown) {
                console.log(xhr.responseText);
            },
            complete: function() {
                window.setTimeout(updateMessages, 2000);
            },
            dataType: 'json'
        });
    };
updateMessages();

但是,有人评论说这段代码不是长轮询的。所以我研究并调整了上面的一些代码:

 ...
     complete: function() {
                updateMessages(); //also tried updateMessages;
            },
     timeout:30000,
     dataType: 'json'
...

但它遇到了根本没有轮询并且消息不会更新的问题。如何调整我的原始代码以进行长轮询?代码的改进是一个奖励。谢谢!

温馨提示:我不使用网络套接字,因为存在旧版浏览器兼容性问题。我也不使用 nodejs,因为我的共享服务器不允许长时间运行的进程。


PHP 代码(在我的控制器中)

public function index()
    {
        $timestamps = Input::get('from'); //timestamp of latest message
        $conv_id = Input::get('conv_id');
        $allMessages = Messages::whereIn('conv_id',$conv_id);
        if(is_null($timestamps)){
           $messages = $allMessages->orderBy('created_at','desc')->take(10);
        }else{
           asort($timestamps);
           $messages = $allMessages->where('created_at','>',end($timestamps));
        }
        return $messages->get()->reverse();
    }

【问题讨论】:

  • setInterval 但你应该看看Web Sockets
  • @Darren 你的意思是我应该把 setTimeout 改成 setInterval?顺便说一句,我不使用网络套接字,因为旧版浏览器兼容性问题。我也不使用 nodejs,因为我的共享服务器不允许长时间运行的进程。
  • 这个快速的JSFiddle 演示说明了 setInterval(检查控制台)。或者你应该看看This SO Answer
  • 您还需要让您的服务器代码继续等待数据 25 秒。仅在 javascript 上设置超时不会这样做。您的服务器可能仍会在一秒钟内做出响应,而不是三十秒。
  • @Mark 是的!你的 javascript 对我来说看起来很合理。但是我仍然看不到 php 中的循环和睡眠部分。请注意,我也不知道使用并发连接的 PHP 执行此操作是否存在性能问题。

标签: php jquery ajax laravel long-polling


【解决方案1】:

请注意,下面的代码仅试图演示 jQuery + PHP 长轮询如何工作。并且可能存在并发性能等问题。我正在尝试根据问题演示一个简单的长轮询脚本。

为了能够适当地扩展和支持长轮询,我建议使用 Darren 提到的 Web SocketsSocket.io 和可能的 ReactPHP 等技术来替代 node.js。

客户端 javascipt:

var updateMessages = function() {
    var conv_id = [];
    var lastMessages = [];

    // your code to get conv_id and lastMessages here

    $.ajax({
        type: "POST",
        url: 'create/populate',
        timeout: 30000,
        dataType: 'json',
        data: {
            'from': lastMessages,
            'conv_id': conv_id
        },
        success: function(messages) {
            // your success code here
        },
        error: function(xhr,textStatus,errorThrown) {
            console.log(xhr.responseText);
        },
        complete: function() {
            window.setTimeout(updateMessages, 2000);
        },
    });
};

updateMessages();

您的 PHP 控制器:

public function populate()
{
    $timeout_in_seconds = 20;
    $interval_in_seconds = 5;

    $start_time = time();
    $timeout = false;

    $timestamps = Input::get('from'); //timestamp of latest message
    $conv_id = Input::get('conv_id');

    // While we don't have any new messages, and haven't reached timeout yet.
    while (empty($messages) && !$timeout) {

        $allMessages = Messages::whereIn('conv_id', $conv_id)
            ->orderBy('created_at','desc');

        if (empty($timestamps)) {
           $messages = $allMessages->take(10)->get()->reverse();
        } else {
           asort($timestamps);
           $messages = $allMessages->where('created_at', '>', end($timestamps))->get()->reverse();
        }

        $timeout = (time() - $start_time) > $timeout_in_seconds;

        // If there is message or timeout, break out of the while loop and return the messages immediately.
        if (!empty($messages) || $timeout) {
            break;
        }

        sleep($interval_in_seconds);
    }

    return $messages;
}

【讨论】:

  • 谢谢!我想我可能找到了solution。你怎么看?这可以通过共享服务器完成吗?
  • 说实话,我真的不认为你会从共享主机中受益。如果您可以接受 1 分钟或更长时间的延迟,那么从 javascript 中每 1 分钟或更长时间执行一次间隔调用实际上比我上面提出的 30 秒假长轮询更有效。
  • 是的,我是这么认为的。共享主机真的很痛苦。我想我会坚持我的正常投票。还是谢谢!
  • 附带说明,如今云服务器相当便宜,有些起价为每月 5 美元,也许考虑在未来放弃共享主机?
猜你喜欢
  • 2011-04-20
  • 2012-02-24
  • 2012-08-16
  • 2013-08-08
  • 2012-05-17
  • 2011-09-19
  • 1970-01-01
  • 1970-01-01
  • 2019-05-28
相关资源
最近更新 更多