AJAX长轮询:如何使此代码进行长轮询?

时间:2014-06-20 03:35:53

标签: php jquery ajax laravel long-polling

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

     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'
...

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

gente note :我不使用遗留浏览器兼容性问题的网络套接字bcoz。我也不使用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();
    }

1 个答案:

答案 0 :(得分:1)

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

为了能够正确地扩展和支持长轮询,我建议使用Darren提到的Web Sockets等技术,Socket.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;
}
相关问题