使用Laravel 4更新实时数据(如进度条) [英] Update live data (like progress bar) in view with Laravel 4

查看:290
本文介绍了使用Laravel 4更新实时数据(如进度条)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想发送数据以实时更新视图中的内容,例如进度 显示操作状态的条形图. 在laravel 4中做到这一点的最佳方法是什么?

I would like to send data to update live in a view, such as a progress bar showing the status of an action. What is the best way to do that in laravel 4?


设置

我正在研究基于Laravel 4的项目,每个用户都可以赎回一个序列号.


The Setup

I'm working on a Laravel 4 based project where each user can redeem a serial key.

我已经创建了一个管理后端,可以在其中轻松粘贴密钥列表或上传密钥文件.

I've made an admin backend where I can easily paste in a list of keys, or upload a file of them.

假设$key_string是我上传的换行分隔键的字符串,并且想要解析然后从中上传包含的键字符串-这是添加键的简化代码:

Let's say $key_string is the string of newline-seperated keys that I've uploaded, and want to parse out to then upload the contained key strings from - here is the simplified code that adds the keys:

$key_string = rtrim($key_string);
$key_string = str_replace("\n\r", "\n", $key_string);
$keys = explode( "\n", $key_string);

foreach($keys as $index => $key) {
    Key::create(
        array( "serial" => trim($key) )
    );
}

由于我上传的密钥组数以千计,所以有时可能需要花费30秒钟的时间,在此期间,管理面板自然不会显示任何内容.

Since the sets of keys I upload number in the thousands, this can sometimes take a good 30 seconds, during which time the admin panel naturally doesn't show anything.

现在,我不介意这段时间.我不需要优化上传就可以使用一个查询,等等,但是我想得到一些实际的反馈,所以我知道上传已经进行了多长时间.

Now, I don't mind it taking this time. I don't need to optimize the upload to use one query, etc, but I would like to have some actual feedback so I know how far the upload has gone.

当我上传密钥时,我希望能够每隔几秒钟或百分之几的滴答声(使用当前的$index)更新视图中的进度条或计数器

When I upload keys, I would like to be able to update a progress bar or counter in my view every few seconds or percent ticks (using the current $index)

是否有一种简便的方法可以轻松地处理此问题,最好将其集成在Laravel 4中?我以为这会涉及ajax,但是有人可以指出我正确的方向吗?

Is there an easy way to handle this painlessly, preferably integrated in Laravel 4? I'm assuming this would involve ajax, but can someone point me in the right direction?

推荐答案

对于PHP,实际上有两种选择,而无需进行Web套接字或推挽式设置.这并不是Laravel真正的事情,它更像是一个请求JSON事物"的AJAX循环.

With PHP there are really two options without going to Web Sockets or Push-Pull setups. This isn't really a Laravel thing it's more of an AJAX loop that requests JSON "thing".

快速轮询

Olark将这种方法用于他们的聊天脚本.

Olark uses this methodology for their chat script.

setInterval(function() {
    $.getJSON("/path", function(data) {
        // update the view with your fresh data
    });
}, 5000);

长时间轮询

JavaScript

Javascript

var eventName = function() {
    $.getJSON("/path", function(data) {
        // update the view with your fresh data
        if (data.progress < 100)
            eventName();
    });
};

控制器逻辑

当我有用户上传CSV并等待其完成上传并得到处理时,我会使用它.

I use this when I have users upload a CSV and are waiting for it to finish uploading and be processed.

// in your controller
$check = true;
while ($check) {
    // search database
    // compare values
    if ($newDataWasFound)
        $check = false;

    $progressFromAbove = 90;
}

return Response::json(array(
    'newData' => $array,
    'progress' => $progressFromAbove,
));

我使用Laravel 3进行了截屏,但是Long Polling与PHP相关,与Laravel没有关系. https://www.youtube.com/watch?v=LDgJF77jELo

I made a screencast on this using Laravel 3 but Long Polling is PHP relevant not Laravel. https://www.youtube.com/watch?v=LDgJF77jELo

示例

  • https://gist.github.com/clouddueling/5239153
  • https://gist.github.com/clouddueling/6296036

这篇关于使用Laravel 4更新实时数据(如进度条)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆