Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] HTTP Response 如何向客户端分段发送数据 #2403

Closed
233cy opened this issue Sep 1, 2020 · 9 comments · Fixed by #4290
Closed

[FEATURE] HTTP Response 如何向客户端分段发送数据 #2403

233cy opened this issue Sep 1, 2020 · 9 comments · Fixed by #4290
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@233cy
Copy link
Contributor

233cy commented Sep 1, 2020

我需要
image
可我在hyperf中不知道该如何使用write

@233cy 233cy added the enhancement New feature or request label Sep 1, 2020
@233cy
Copy link
Contributor Author

233cy commented Sep 1, 2020

有人帮忙说哈子吗!

@limingxinleo
Copy link
Member

现在的代码是不支持这个功能的,如果你需要使用,得自己重写一下 onRequest 方法。

$response 存下来,然后在你的代码里获取到这个对象,再使用

@233cy
Copy link
Contributor Author

233cy commented Sep 1, 2020

现在的代码是不支持这个功能的,如果你需要使用,得自己重写一下 onRequest 方法。

$response 存下来,然后在你的代码里获取到这个对象,再使用

好的知道了。

image

我先自行这样用了。希望能尽快加入呢。!!!!

@limingxinleo
Copy link
Member

@huangzhhui 这个功能,打算支持么?感觉不算是正常的需求

@233cy
Copy link
Contributor Author

233cy commented Sep 2, 2020

@huangzhhui 这个功能,打算支持么?感觉不算是正常的需求

其他的功能目前还没想到怎么用,不过目前我是要做一个简单的限流下载。所以需要用到。

@huangzhhui
Copy link
Member

@huangzhhui 这个功能,打算支持么?感觉不算是正常的需求

不应该这样暴露 Swoole Response,但应该为 Response 提供 chunk 的功能支持

@233cy
Copy link
Contributor Author

233cy commented Sep 2, 2020

@huangzhhui 这个功能,打算支持么?感觉不算是正常的需求

不应该这样暴露 Swoole Response,但应该为 Response 提供 chunk 的功能支持

好的谢谢提醒,我先暂时用着,有时间我看看源码,如果能力允许,我看能写出符合规格的方式不。。

@huangzhhui huangzhhui changed the title 我改如何获取SwooleStream HTTP Response 如何向客户端分段发送数据 Sep 3, 2020
@huangzhhui
Copy link
Member

/switch-to feature

@huangzhhui huangzhhui changed the title HTTP Response 如何向客户端分段发送数据 [FEATURE] HTTP Response 如何向客户端分段发送数据 Sep 3, 2020
@huangzhhui huangzhhui added good first issue Good for newcomers help wanted Extra attention is needed labels Sep 3, 2020
@233cy
Copy link
Contributor Author

233cy commented Sep 4, 2020

将方式改了以下,可能不符合标准,不过可以自行扩展。大致代码如下。使用了。dependencies关系重写。这样更好防止组件升级每次需要重写改写。并且后续官方如果升级了可自行删减或修改代码即可。
首先编写一个继承类如下:

<?php

declare(strict_types=1);
/**
 * 未经授权,不允许用于商业。
 * Author   233cy
 * Mail     wfuren@qq.com
 * DateTime 2020-09-04 02:56:20
 */

namespace App\Depend;

use Hyperf\HttpMessage\Server\Request as Psr7Request;
use Hyperf\HttpMessage\Server\Response as Psr7Response;
use Hyperf\Utils\Context;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Swoole\Http\Request as SwooleRequest;
use Swoole\Http\Response as SwooleResponse;

/**
 * 继承类改动\Hyperf\HttpServer\Server方法。
 *
 * Mail     wfuren@qq.com
 * DateTime 2020-09-03 10:09:01
 */
class ServerDepend extends \Hyperf\HttpServer\Server
{
    protected function initRequestAndResponse(SwooleRequest $request, SwooleResponse $response): array
    {
        /* 此处为自主加入swoole的request和response,使用方法请在控制器中直接通过上下文获取对应键值即可。 */
        Context::set('SwooleResponse', $response);
        Context::set('SwooleRequest', $request);
        /* 以下为\Hyperf\HttpServer\Server官方代码 */
        // Initialize PSR-7 Request and Response objects.
        Context::set(ResponseInterface::class, $psr7Response = new Psr7Response());
        Context::set(ServerRequestInterface::class, $psr7Request = Psr7Request::loadFromSwooleRequest($request));
        return [$psr7Request, $psr7Response];
    }
}

修改config/autoload/dependencies.php关系文件。

<?php

declare(strict_types=1);
/**
 * 未经授权,不允许用于商业。
 * Author   233cy
 * Mail     wfuren@qq.com
 * DateTime 2020-09-04 02:07:59
 */

 /* 这是一个关系重写,配置文件。 */
return [
    Hyperf\HttpServer\Server::class => App\Depend\ServerDepend::class,
];

具体使用方法:
image
通过swoole的响应和请求做更多的个性处理。因能力有限只能通过这种方式来操作。如果你需要某个特殊功能,可自行通过类似工厂模式来防止后期的官方框架升级等。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants