簡體   English   中英

AmPHP 緩沖區永遠存在

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM