[英]AmPHP buffer keeps going forever
我刚刚拿起 AmPHP,我正试图从我的 AmPHP http 服务器获取帖子正文,但是,它一直在运行(只是从不向我的客户发送回复)。
这是我目前正在使用的代码:
$resp = \Amp\Promise\wait($request->getBody()->buffer());
我已经测试了另一段不会永远持续下去的代码,但是当我使用那段代码时,我无法将我的身体放在 onResolve 中的onResolve
:
$resp = $request->getBody()->buffer()->onResolve(function($error, $value) {
return $value;
});
return $resp; // returns null
我也试过最后一点,但也只是返回null
:
return yield $request->getBody()->buffer();
编辑:做了更多的摆弄,这是我当前的(仍然没有功能的)代码(虽然为了简单起见,很多已经被剥离了):
// 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];
}
}
将执行方法实现包装到 Amp\call() 以返回 Promise 而不是 Generator。 然后在主循环中产生结果以获取数组而不是 null。
// 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
} );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.