简体   繁体   中英

AmPHP buffer keeps going forever

I've just picked up AmPHP and I'm trying to get the post body from my AmPHP http server, however, it keeps going forever (just never sends back a response to my client).
This is the code I'm currently using:

$resp = \Amp\Promise\wait($request->getBody()->buffer());

I've tested another piece of code that doesn't keep going forever but when I use that piece, I can't get my body outside the function in the onResolve :

$resp = $request->getBody()->buffer()->onResolve(function($error, $value) {
  return $value;
});
return $resp; // returns null

I've also tried this last bit but that also just returns null :

return yield $request->getBody()->buffer();

Edit: Done some more fiddling around, this is my current (still non-functional) code (although a lot has been stripped out for simplicity sake):

// 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];
  }
}

Wrap execute method implementation to Amp\call() to return Promise instead of Generator. Then on the main loop yield the result to get the array instead of 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
} );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM