brownpaperbag/flush 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

brownpaperbag/flush

最新稳定版本:1.0.0

Composer 安装命令:

composer require brownpaperbag/flush

包简介

Library to flush data friendly. To use in long-polling and zombie requests.

README 文档

README

Library to flush data friendly. To use in long-polling and zombie requests. Distributed as a composer package.

Instalation

composer require brownpaperbag/flush

Usage

This library was built to work in three scenarios basically:

  • Long-polling requests;
  • Long polling requests with socket style;
  • Zombie requests;

Usage in long-polling requests

<?php

use BrownPaperBag\Flush;

$flush = new Flush('application/json');

$flush->prepare();

do{

    if(!$flush->signal()){
    
        break;
    
    }
    else{
    
        // Your commands here or conditions to break in some point...
    
    }

}
while(sleep(1) || true);

$flush->json(array(

    'command' => 'location.reload();'

));

Usage in long-polling requests with socket style

<?php

use BrownPaperBag\Flush;

$flush = new Flush('application/json');

$flush->prepare();

do{

    $message = rand(1, 10);

    if(!$flush->signal()){

        break;

    }
    else if($message % 2){ // Any condition you want...

        $flush->json(array(

            'success' => true,

            'message' => $message

        ));

    }

}
while(sleep(1) || true);

That's an example how to receive the response on client side:

var request = new XMLHttpRequest();

request.onreadystatechange = function(){

    if(this.status == 200){

        switch(this.readyState){

            case this.LOADING:

                var data = this.responseText,
                    length = data.length;

                data = data.slice(this.lastLength - length);
                data = data.trim(data);
                
                this.lastLength = length;

                if(data.length){

                    data = $.parseJSON(data);
                    
                    // Whatever you want to do with your new object.
                    // You can use it to implement push notifications for example...
                    
                    console.log(data);

                }

                break;

        }

    }

};

request.open('GET', '/api/notifications', true);
request.send();

Usage in zombie requests

<?php

use BrownPaperBag\Flush;

// You could use new Flush('application/json', true) instead of commands bellow.
// Just showing another way...

$flush = new Flush();

$flush->setContentType('application/json'); // Optional
$flush->setLimboEnabled(true); // Required for zombie requests

// Doing whatever task that user must to wait...

$flush->json(array( // If limbo is enabled Flush will prepare() automatically.

    'message' => 'An awesome return message here...'

)); // To prevent prepare(), for any reason, just pass an explicit false as second argument in json/data().

// From this moment you can execute any task in background like: render a PDF, send emails or fire some slow script.
// Just avoid to create an infinite loop :)

License

MIT

统计信息

  • 总下载量: 84
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 9
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-10-28

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固