Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
chaz6chez committed Jul 2, 2024
1 parent e771f86 commit c53fa84
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 10 deletions.
10 changes: 7 additions & 3 deletions src/PushServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,13 @@ public function onConnect(TcpConnection $connection): void
static::setConnectionProperty($connection, 'onWebSocketConnect',
// ws 连接会调用该回调
function (TcpConnection $connection, string $header) use ($appKey, $socketId) {
$unknown = $appKey;
$request = new Request($header);
if (!preg_match('/\/app\/([^\/^\?^]+)/', $request->path() ?? '', $match)) {
static::error($connection, null, 'Invalid app', true);
return;
}
// 获取app验证回调,如果没有验证回调则忽略验证
$appKey = '';
if ($appVerifyCallback = static::getConfig('app_verify', getBase: true)) {
if (!call_user_func($appVerifyCallback, $appKey = $match[1])) {
static::error($connection, null, "Invalid app_key", true);
Expand All @@ -155,6 +155,10 @@ function (TcpConnection $connection, string $header) use ($appKey, $socketId) {
static::setConnectionProperty($connection, 'appKey', $appKey);
static::setConnectionProperty($connection, 'queryString', $request->queryString() ?? '');
static::setConnectionProperty($connection, 'channels', []);
// 移除unknown连接
static::unsetConnection($unknown, $socketId);
// 设置连接
static::setConnection($appKey, $socketId, $connection);
/**
* 向客户端发送链接成功的消息
* {"event":"pusher:connection_established","data":"{"socket_id":"208836.27464492","activity_timeout":120}"}
Expand Down Expand Up @@ -394,7 +398,7 @@ public static function getConnectionProperty(TcpConnection $connection, string $
*/
public static function getConnections(): array
{
return self::$_connections;
return static::$_connections;
}

/**
Expand All @@ -403,7 +407,7 @@ public static function getConnections(): array
*/
public static function setConnections(array $connections): void
{
self::$_connections = $connections;
static::$_connections = $connections;
}

/**
Expand Down
52 changes: 45 additions & 7 deletions tests/PushServerBaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace Tests;

use Exception;
use Tests\MockClass\MockTcpConnection;
use Workbunny\WebmanPushServer\Events\Ping;
use Workbunny\WebmanPushServer\PushServer;
Expand All @@ -36,17 +35,21 @@ public function testPushServerOnConnect()
$this->assertFalse(property_exists($connection, 'queryString'));
$this->assertFalse(property_exists($connection, 'socketId'));
$this->assertFalse(property_exists($connection, 'channels'));
$this->assertEquals([], PushServer::getConnections()[PushServer::$unknownTag] ?? []);
// 模拟onConnect
$this->getPushServer()->onConnect($connection);
// 断言判定
$this->assertEquals(
PushServer::$unknownTag, PushServer::getConnectionProperty($connection, 'appKey', 'has-not')
PushServer::$unknownTag, $appKey = PushServer::getConnectionProperty($connection, 'appKey', 'has-not')
);
$this->assertEquals(
0, PushServer::getConnectionProperty($connection, 'clientNotSendPingCount', 'has-not')
);
$this->assertNotEquals(
'has-not', PushServer::getConnectionProperty($connection, 'socketId', 'has-not')
'has-not', $socketId = PushServer::getConnectionProperty($connection, 'socketId', 'has-not')
);
$this->assertEquals(
$connection, PushServer::getConnection($appKey, $socketId)
);
$this->assertNotEquals(
'', PushServer::getConnectionProperty($connection, 'queryString', 'has-not')
Expand All @@ -61,8 +64,9 @@ public function testPushServerOnConnect()
// 模拟调用$connection->onWebSocketConnect
call_user_func(PushServer::getConnectionProperty($connection, 'onWebSocketConnect'), $connection, $this->getWebsocketHeader());
// 断言判定
$this->assertEquals([],PushServer::getConnections()[PushServer::$unknownTag] ?? []);
$this->assertEquals(
'workbunny', PushServer::getConnectionProperty($connection, 'appKey', 'has-not')
'workbunny', $appKey = PushServer::getConnectionProperty($connection, 'appKey', 'has-not')
);
$this->assertEquals(
0, PushServer::getConnectionProperty($connection, 'clientNotSendPingCount', 'has-not')
Expand All @@ -71,7 +75,10 @@ public function testPushServerOnConnect()
'protocol=7&client=js&version=3.2.4&flash=false', PushServer::getConnectionProperty($connection, 'queryString', 'has-not')
);
$this->assertNotEquals(
'has-not', PushServer::getConnectionProperty($connection, 'socketId', 'has-not')
'has-not', $socketId = PushServer::getConnectionProperty($connection, 'socketId', 'has-not')
);
$this->assertEquals(
$connection, PushServer::getConnection($appKey, $socketId)
);
$this->assertEquals(
[], PushServer::getConnectionProperty($connection, 'channels', 'has-not')
Expand All @@ -89,6 +96,8 @@ public function testPushServerOnMessage()
$connection = new MockTcpConnection();
// 模拟onConnect
$this->getPushServer()->onConnect($connection);
// 模拟调用$connection->onWebSocketConnect
call_user_func(PushServer::getConnectionProperty($connection, 'onWebSocketConnect'), $connection, $this->getWebsocketHeader());
// 断言检测心跳计数为0
$this->assertEquals(
0, PushServer::getConnectionProperty($connection, 'clientNotSendPingCount', 'has-not')
Expand All @@ -101,8 +110,8 @@ public function testPushServerOnMessage()
);
// 断言检测事件初始为null
$this->assertNull($this->getPushServer()->getLastEvent());
// 断言检测初始buffer为空
$this->assertTrue(!$connection->getSendBuffer());
// EVENT_CONNECTION_ESTABLISHED事件回复
$this->assertEquals(EVENT_CONNECTION_ESTABLISHED, @json_decode($connection->getSendBuffer(), true)['event'] ?? null);
// 模拟心跳
$this->getPushServer()->onMessage($connection, '{"event":"pusher:ping"}');
// 断言检测心跳累计为0
Expand All @@ -115,4 +124,33 @@ public function testPushServerOnMessage()
$this->assertEquals(EVENT_PONG, @json_decode($connection->getSendBuffer(), true)['event'] ?? null);
}

/**
* @return void
*/
public function testPushServerOnCloseHasNotChannel()
{
// 初始化一个mock tcp连接
$connection = new MockTcpConnection();
// 模拟onConnect
$this->getPushServer()->onConnect($connection);
// 模拟调用$connection->onWebSocketConnect
call_user_func(PushServer::getConnectionProperty($connection, 'onWebSocketConnect'), $connection, $this->getWebsocketHeader());
// 断言判定
$this->assertEquals(
'workbunny', $appKey = PushServer::getConnectionProperty($connection, 'appKey', 'has-not')
);
$this->assertNotEquals(
'has-not', $socketId = PushServer::getConnectionProperty($connection, 'socketId', 'has-not')
);
$this->assertEquals(
$connection, PushServer::getConnection($appKey, $socketId)
);
// 模拟onClose
$this->getPushServer()->onClose($connection);
// 断言判定
$this->assertEquals(
null, PushServer::getConnection($appKey, $socketId)
);
}

}

0 comments on commit c53fa84

Please sign in to comment.