首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AmPHP缓冲区永远在运行

AmPHP缓冲区永远在运行
EN

Stack Overflow用户
提问于 2021-02-11 19:52:12
回答 1查看 118关注 0票数 0

我刚刚获得了AmPHP,我正在尝试从我的AmPHP http服务器获取post正文,但是,它一直在运行(只是从来没有向我的客户端发送回复)。

这是我目前使用的代码:

代码语言:javascript
复制
$resp = \Amp\Promise\wait($request->getBody()->buffer());

我已经测试了另一段代码,它不会一直运行下去,但是当我使用这段代码时,我无法在onResolve中的函数之外获得我的身体。

代码语言:javascript
复制
$resp = $request->getBody()->buffer()->onResolve(function($error, $value) {
  return $value;
});
return $resp; // returns null

我也尝试了最后一段,但这也只是返回null

代码语言:javascript
复制
return yield $request->getBody()->buffer();

编辑:再做一些修改,这是我当前的(仍然没有功能的)代码(尽管为了简单起见,已经删除了很多代码):

代码语言:javascript
复制
// Main loop
Loop::run(function() {
  $webhook = new Webhook();
  $resp = $webhook->execute($request);
  print_r($resp); // null
});

// Webhook
class Webhook {
  public function execute(\Amp\Http\Server\Request $request) {
    $postbody = yield $request->getBody()->buffer();
    return ['success' => true, 'message' => 'Webhook executed successfully', 'data' => $postbody];
  }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-16 07:53:49

将execute方法实现包装为Amp\call(),以返回诺言,而不是生成器。然后在主循环上产生结果,得到数组而不是null。

代码语言:javascript
复制
// Webhook
class Webhook {
    public function execute( \Amp\Http\Server\Request $request ) {
        return Amp\call( function () use ( $request ) {
            $postbody = yield $request->getBody()->buffer();

            return [ 'success' => true, 'message' => 'Webhook executed successfully', 'data' => $postbody ];
        } );
    }
}

// Main loop
Loop::run( function () {
    $webhook = new Webhook();
    $resp    = $webhook->execute( $request );
    $output  = yield $resp;
    print_r( $resp ); // array with post body
} );
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66161838

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档