diff --git a/CHANGELOG.md b/CHANGELOG.md index 09c98c41d..9a4123a8b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## Added -- Nothing. +- Added WebSocket Client. ## Changed diff --git a/composer.json b/composer.json index a95b9fb6f..1f2139edc 100644 --- a/composer.json +++ b/composer.json @@ -83,7 +83,9 @@ "hyperf/redis": "self.version", "hyperf/server": "self.version", "hyperf/tracer": "self.version", - "hyperf/utils": "self.version" + "hyperf/utils": "self.version", + "hyperf/websocket-client": "self.version", + "hyperf/websocket-server": "self.version" }, "suggest": { }, @@ -128,20 +130,24 @@ "Hyperf\\Redis\\": "src/redis/src/", "Hyperf\\Server\\": "src/server/src/", "Hyperf\\Tracer\\": "src/tracer/src/", - "Hyperf\\Utils\\": "src/utils/src/" + "Hyperf\\Utils\\": "src/utils/src/", + "Hyperf\\WebSocketClient\\": "src/websocket-client/src/", + "Hyperf\\WebSocketServer\\": "src/websocket-server/src/" } }, "autoload-dev": { "psr-4": { - "HyperfTest\\ConfigApollo\\": "./src/config-apollo/tests", - "HyperfTest\\Consul\\": "./src/consul/tests", - "HyperfTest\\Di\\": "./src/di/tests", - "HyperfTest\\Dispatcher\\": "./src/dispatcher/test", - "HyperfTest\\Database\\": "./src/database/tests", - "HyperfTest\\DbConnection\\": "./src/db-connection/tests", - "HyperfTest\\Event\\": "./src/event/tests", - "HyperfTest\\Redis\\": "./src/redis/tests", - "HyperfTest\\Utils\\": "./src/utils/tests" + "HyperfTest\\ConfigApollo\\": "./src/config-apollo/tests/", + "HyperfTest\\Consul\\": "./src/consul/tests/", + "HyperfTest\\Di\\": "./src/di/tests/", + "HyperfTest\\Dispatcher\\": "./src/dispatcher/test/", + "HyperfTest\\Database\\": "./src/database/tests/", + "HyperfTest\\DbConnection\\": "./src/db-connection/tests/", + "HyperfTest\\Event\\": "./src/event/tests/", + "HyperfTest\\Redis\\": "./src/redis/tests/", + "HyperfTest\\Utils\\": "./src/utils/tests/", + "HyperfTest\\WebSocketClient\\": "src/websocket-client/tests/", + "HyperfTest\\WebSocketServer\\": "src/websocket-server/tests/" } }, "config": { @@ -178,7 +184,9 @@ "Hyperf\\Redis\\ConfigProvider", "Hyperf\\Server\\ConfigProvider", "Hyperf\\Tracer\\ConfigProvider", - "Hyperf\\Utils\\ConfigProvider" + "Hyperf\\Utils\\ConfigProvider", + "Hyperf\\WebSocketClient\\ConfigProvider", + "Hyperf\\WebSocketServer\\ConfigProvider" ] } }, diff --git a/src/websocket-client/src/Client.php b/src/websocket-client/src/Client.php index 933edc281..2c43e7458 100644 --- a/src/websocket-client/src/Client.php +++ b/src/websocket-client/src/Client.php @@ -15,6 +15,7 @@ namespace Hyperf\WebSocketClient; use Hyperf\WebSocketClient\Exception\ConnectException; use Psr\Http\Message\UriInterface; use Swoole\Coroutine; +use Swoole\WebSocket\Frame as SwFrame; class Client { @@ -45,7 +46,7 @@ class Client $path = empty($query) ? $path : $path . '?' . $query; $ret = $this->client->upgrade($path); - if (!$ret) { + if (! $ret) { $errCode = $this->client->errCode; throw new ConnectException(sprintf('Websocket upgrade failed by [%s] [%s].', $errCode, swoole_strerror($errCode))); } @@ -59,8 +60,11 @@ class Client public function recv(float $timeout = -1) { $ret = $this->client->recv($timeout); - var_dump($ret instanceof \Swoole\WebSocket\Frame); - return $ret ? new Frame($ret) : $ret; + if ($ret instanceof SwFrame) { + return new Frame($ret); + } + + return $ret; } public function push(string $data, int $opcode = WEBSOCKET_OPCODE_TEXT, bool $finish = true): bool diff --git a/src/websocket-client/tests/ClientTest.php b/src/websocket-client/tests/ClientTest.php new file mode 100644 index 000000000..057d583f9 --- /dev/null +++ b/src/websocket-client/tests/ClientTest.php @@ -0,0 +1,32 @@ +assertSame($frame->data, (string) $frame); + } +}