mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-12-01 19:27:39 +08:00
Added websocket client test.
This commit is contained in:
parent
9be7cc316b
commit
c52216ef17
@ -2,7 +2,7 @@
|
||||
|
||||
## Added
|
||||
|
||||
- Nothing.
|
||||
- Added WebSocket Client.
|
||||
|
||||
## Changed
|
||||
|
||||
|
@ -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"
|
||||
]
|
||||
}
|
||||
},
|
||||
|
@ -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
|
||||
|
32
src/websocket-client/tests/ClientTest.php
Normal file
32
src/websocket-client/tests/ClientTest.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
namespace HyperfTest\WebSocketClient;
|
||||
|
||||
use Hyperf\HttpMessage\Uri\Uri;
|
||||
use Hyperf\WebSocketClient\Client;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @coversNothing
|
||||
*/
|
||||
class ClientTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @expectedException \Hyperf\WebSocketClient\Exception\ConnectException
|
||||
*/
|
||||
public function testClientConnectFailed()
|
||||
{
|
||||
new Client(new Uri('ws://172.168.1.1:9522'));
|
||||
}
|
||||
}
|
32
src/websocket-client/tests/FrameTest.php
Normal file
32
src/websocket-client/tests/FrameTest.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
namespace HyperfTest\WebSocketClient;
|
||||
|
||||
use Hyperf\WebSocketClient\Frame;
|
||||
use Mockery;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Swoole\WebSocket\Frame as SwFrame;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @coversNothing
|
||||
*/
|
||||
class FrameTest extends TestCase
|
||||
{
|
||||
public function testFrame()
|
||||
{
|
||||
$frame = new Frame(Mockery::mock(SwFrame::class));
|
||||
|
||||
$this->assertSame($frame->data, (string) $frame);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user