Merge pull request #135 from 1099511627776/bclounge

Close connection after stream read
This commit is contained in:
Peter Lai 2019-01-17 01:08:34 +08:00 committed by GitHub
commit 5e4e5e35fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 22 deletions

2
.gitignore vendored
View File

@ -1,6 +1,8 @@
composer.phar composer.phar
/vendor/ /vendor/
.phpintel/ .phpintel/
/.idea/
# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file # Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file # You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file

View File

@ -2,9 +2,9 @@
/** /**
* This file is part of web3.php package. * This file is part of web3.php package.
* *
* (c) Kuan-Cheng,Lai <alk03073135@gmail.com> * (c) Kuan-Cheng,Lai <alk03073135@gmail.com>
* *
* @author Peter Lai <alk03073135@gmail.com> * @author Peter Lai <alk03073135@gmail.com>
* @license MIT * @license MIT
*/ */
@ -12,6 +12,7 @@
namespace Web3\RequestManagers; namespace Web3\RequestManagers;
use InvalidArgumentException; use InvalidArgumentException;
use Psr\Http\Message\StreamInterface;
use RuntimeException as RPCException; use RuntimeException as RPCException;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Exception\RequestException;
@ -23,14 +24,14 @@ class HttpRequestManager extends RequestManager implements IRequestManager
{ {
/** /**
* client * client
* *
* @var \GuzzleHttp * @var \GuzzleHttp
*/ */
protected $client; protected $client;
/** /**
* construct * construct
* *
* @param string $host * @param string $host
* @param int $timeout * @param int $timeout
* @return void * @return void
@ -43,7 +44,7 @@ class HttpRequestManager extends RequestManager implements IRequestManager
/** /**
* sendPayload * sendPayload
* *
* @param string $payload * @param string $payload
* @param callable $callback * @param callable $callback
* @return void * @return void
@ -53,22 +54,7 @@ class HttpRequestManager extends RequestManager implements IRequestManager
if (!is_string($payload)) { if (!is_string($payload)) {
throw new \InvalidArgumentException('Payload must be string.'); throw new \InvalidArgumentException('Payload must be string.');
} }
// $promise = $this->client->postAsync($this->host, [
// 'headers' => [
// 'content-type' => 'application/json'
// ],
// 'body' => $payload
// ]);
// $promise->then(
// function (ResponseInterface $res) use ($callback) {
// var_dump($res->body());
// call_user_func($callback, null, $res);
// },
// function (RequestException $err) use ($callback) {
// var_dump($err->getMessage());
// call_user_func($callback, $err, null);
// }
// );
try { try {
$res = $this->client->post($this->host, [ $res = $this->client->post($this->host, [
'headers' => [ 'headers' => [
@ -78,7 +64,12 @@ class HttpRequestManager extends RequestManager implements IRequestManager
'timeout' => $this->timeout, 'timeout' => $this->timeout,
'connect_timeout' => $this->timeout 'connect_timeout' => $this->timeout
]); ]);
$json = json_decode($res->getBody()); /**
* @var StreamInterface $stream ;
*/
$stream = $res->getBody();
$json = json_decode($stream);
$stream->close();
if (JSON_ERROR_NONE !== json_last_error()) { if (JSON_ERROR_NONE !== json_last_error()) {
call_user_func($callback, new InvalidArgumentException('json_decode error: ' . json_last_error_msg()), null); call_user_func($callback, new InvalidArgumentException('json_decode error: ' . json_last_error_msg()), null);