Skip to content

Commit

Permalink
fixed bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
chaz6chez committed Aug 12, 2024
1 parent 5f2414c commit 34035ea
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 25 deletions.
4 changes: 3 additions & 1 deletion src/Events/ClientEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Workbunny\WebmanPushServer\PushServer;
use Workbunny\WebmanPushServer\Traits\ChannelMethods;
use Workerman\Connection\TcpConnection;
use function Workbunny\WebmanPushServer\ms_timestamp;
use const Workbunny\WebmanPushServer\CHANNEL_TYPE_PRESENCE;
use const Workbunny\WebmanPushServer\CHANNEL_TYPE_PRIVATE;

Expand Down Expand Up @@ -61,10 +62,11 @@ public function response(TcpConnection $connection, array $request): void
// 广播 客户端消息
PushServer::publishUseRetry(AbstractPublishType::PUBLISH_TYPE_CLIENT, [
'appKey' => PushServer::getConnectionProperty($connection,'appKey'),
'socketId' => PushServer::getConnectionProperty($connection,'socketId'),
'timestamp' => ms_timestamp(),
'channel' => $channel,
'event' => $event,
'data' => $data,
'socketId' => PushServer::getConnectionProperty($connection,'socketId')
]);
}
}
14 changes: 9 additions & 5 deletions src/Events/Subscribe.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Workbunny\WebmanPushServer\PublishTypes\AbstractPublishType;
use Workbunny\WebmanPushServer\PushServer;
use Workerman\Connection\TcpConnection;
use function Workbunny\WebmanPushServer\ms_timestamp;
use function Workbunny\WebmanPushServer\uuid;
use const Workbunny\WebmanPushServer\CHANNEL_TYPE_PRESENCE;
use const Workbunny\WebmanPushServer\CHANNEL_TYPE_PRIVATE;
Expand Down Expand Up @@ -167,6 +168,8 @@ public static function subscribeChannel(TcpConnection $connection, string $chann
// 内部事件广播 通道被创建事件
PushServer::publishUseRetry(AbstractPublishType::PUBLISH_TYPE_SERVER, [
'appKey' => $appKey,
'socketId' => $socketId,
'timestamp' => ms_timestamp(),
'channel' => $channel,
'event' => EVENT_CHANNEL_OCCUPIED,
'data' => [
Expand Down Expand Up @@ -202,15 +205,16 @@ public static function subscribeChannel(TcpConnection $connection, string $chann
* {"event":"pusher_internal:member_added","data":{"user_id":1488465780,"user_info":"{\"name\":\"123\",\"sex:\"1\"}","channel ":"presence-channel"}}
*/
PushServer::publishUseRetry(AbstractPublishType::PUBLISH_TYPE_CLIENT, [
'appKey' => $appKey,
'channel' => $channel,
'event' => EVENT_MEMBER_ADDED,
'data' => [
'appKey' => $appKey,
'socketId' => $socketId,
'timestamp' => ms_timestamp(),
'channel' => $channel,
'event' => EVENT_MEMBER_ADDED,
'data' => [
'id' => uuid(),
'user_id' => $userId,
'user_info' => $userInfo
],
'socketId' => $socketId
]);
}
}
Expand Down
14 changes: 9 additions & 5 deletions src/Events/Unsubscribe.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Workbunny\WebmanPushServer\PublishTypes\AbstractPublishType;
use Workbunny\WebmanPushServer\PushServer;
use Workerman\Connection\TcpConnection;
use function Workbunny\WebmanPushServer\ms_timestamp;
use function Workbunny\WebmanPushServer\uuid;
use const Workbunny\WebmanPushServer\CHANNEL_TYPE_PRESENCE;
use const Workbunny\WebmanPushServer\CHANNEL_TYPE_PRIVATE;
Expand Down Expand Up @@ -83,14 +84,15 @@ public static function unsubscribeChannel(TcpConnection $connection, string $cha
* {"event":"pusher_internal:member_removed","data":"{"user_id":"14884657801"}","channel":"presence-channel"}
*/
PushServer::publishUseRetry(AbstractPublishType::PUBLISH_TYPE_CLIENT, [
'appKey' => $appKey,
'channel' => $channel,
'event' => EVENT_MEMBER_REMOVED,
'data' => [
'appKey' => $appKey,
'socketId' => $socketId,
'timestamp' => ms_timestamp(),
'channel' => $channel,
'event' => EVENT_MEMBER_REMOVED,
'data' => [
'id' => uuid(),
'user_id' => $uid
],
'socketId' => $socketId
]);
}
}
Expand All @@ -101,6 +103,8 @@ public static function unsubscribeChannel(TcpConnection $connection, string $cha
// 内部事件广播 通道被移除事件
PushServer::publishUseRetry(AbstractPublishType::PUBLISH_TYPE_SERVER, [
'appKey' => $appKey,
'socketId' => $socketId,
'timestamp' => ms_timestamp(),
'channel' => $channel,
'event' => EVENT_CHANNEL_VACATED,
'data' => [
Expand Down
2 changes: 1 addition & 1 deletion src/PublishTypes/ServerType.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class ServerType extends AbstractPublishType
public static function response(array $data): void
{
static::verify($data, [
['appKey', 'is_string', true],
['event', 'is_string', true],
['appKey', 'is_string', false],
['socketId', 'is_string', false],
]);
// 断开连接事件
Expand Down
4 changes: 3 additions & 1 deletion src/PushServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,9 @@ public static function error(TcpConnection $connection, ?string $code, ?string $
public static function send(TcpConnection $connection, ?string $channel, ?string $event, mixed $data): void
{
$response = static::filter([
'timestamp' => intval(microtime(true) * 1000),
'appKey' => static::getConnectionProperty($connection, 'appKey'),
'socketId' => PushServer::getConnectionProperty($connection,'socketId'),
'timestamp' => ms_timestamp(),
'channel' => $channel,
'event' => $event,
'data' => $data
Expand Down
8 changes: 6 additions & 2 deletions src/config/plugin/workbunny/webman-push-server/route.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use support\Response;
use Workbunny\WebmanPushServer\ApiRoute;
use Workbunny\WebmanPushServer\PushServer;
use function Workbunny\WebmanPushServer\ms_timestamp;
use const Workbunny\WebmanPushServer\CHANNEL_TYPE_PRESENCE;
use const Workbunny\WebmanPushServer\CHANNEL_TYPE_PRIVATE;
use function Workbunny\WebmanPushServer\response;
Expand Down Expand Up @@ -188,10 +189,11 @@
foreach ($channels as $channel) {
PushServer::publishUseRetry(AbstractPublishType::PUBLISH_TYPE_CLIENT, PushServer::filter([
'appKey' => $appKey,
'socketId' => $socketId,
'timestamp' => ms_timestamp(),
'channel' => $channel,
'event' => $event,
'data' => $data,
'socketId' => $socketId,
]));
}
return response(200, json_encode([
Expand All @@ -218,10 +220,11 @@
$socketId = $package['socket_id'] ?? null;
PushServer::publishUseRetry(AbstractPublishType::PUBLISH_TYPE_CLIENT, PushServer::filter([
'appKey' => $appKey,
'socketId' => $socketId,
'timestamp' => ms_timestamp(),
'channel' => $channel,
'event' => $event,
'data' => $data,
'socketId' => $socketId,
]));
}
return response(200,json_encode([
Expand All @@ -247,6 +250,7 @@
PushServer::publishUseRetry(AbstractPublishType::PUBLISH_TYPE_SERVER, [
'appKey' => $appKey,
'socket_id' => $socketId,
'timestamp' => ms_timestamp(),
'event' => EVENT_TERMINATE_CONNECTION,
'data' => [
'type' => 'API',
Expand Down
10 changes: 10 additions & 0 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,14 @@ function str2byte(string|int|float $str): int
{
return strlen((string)$str);
}
}

/**
* 毫秒时间戳
*/
if (!function_exists('ms_timestamp')) {
function ms_timestamp(): int
{
return intval(microtime(true) * 1000);
}
}
24 changes: 14 additions & 10 deletions tests/PushServerBaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Workbunny\WebmanPushServer\PublishTypes\AbstractPublishType;
use Workbunny\WebmanPushServer\PushServer;
use Workerman\Worker;
use function Workbunny\WebmanPushServer\ms_timestamp;
use const Workbunny\WebmanPushServer\EVENT_CONNECTION_ESTABLISHED;
use const Workbunny\WebmanPushServer\EVENT_ERROR;
use const Workbunny\WebmanPushServer\EVENT_PONG;
Expand Down Expand Up @@ -442,9 +443,10 @@ public function testPushServerSubscribeResponse()

// 模拟服务广播响应 非忽略的channel广播
PushServer::_subscribeResponse(AbstractPublishType::PUBLISH_TYPE_CLIENT, [
'appKey' => PushServer::getConnectionProperty($channelConnection, 'appKey'),
'event' => EVENT_PONG,
'channel' => 'public-test'
'appKey' => PushServer::getConnectionProperty($channelConnection, 'appKey'),
'timestamp' => ms_timestamp(),
'event' => EVENT_PONG,
'channel' => 'public-test'
]);
// 断言检测回执buffer 仅合法channel连接接收到广播回执
$this->assertNull($tcpConnection->getSendBuffer());
Expand All @@ -457,10 +459,11 @@ public function testPushServerSubscribeResponse()

// 模拟服务广播响应 指定忽略socketId的channel广播
PushServer::_subscribeResponse(AbstractPublishType::PUBLISH_TYPE_CLIENT, [
'appKey' => PushServer::getConnectionProperty($channelConnection, 'appKey'),
'event' => EVENT_PONG,
'channel' => 'public-test',
'socketId' => PushServer::getConnectionProperty($channelConnection, 'socketId'),
'appKey' => PushServer::getConnectionProperty($channelConnection, 'appKey'),
'socketId' => PushServer::getConnectionProperty($channelConnection, 'socketId'),
'timestamp' => ms_timestamp(),
'event' => EVENT_PONG,
'channel' => 'public-test',
]);
// 断言检测回执buffer 所有连接不应接收到回执
$this->assertNull($tcpConnection->getSendBuffer());
Expand All @@ -473,9 +476,10 @@ public function testPushServerSubscribeResponse()

// 模拟服务广播响应 向未知连接广播
PushServer::_subscribeResponse(AbstractPublishType::PUBLISH_TYPE_CLIENT, [
'appKey' => PushServer::$unknownTag,
'event' => EVENT_PONG,
'channel' => 'public-test',
'appKey' => PushServer::$unknownTag,
'timestamp' => ms_timestamp(),
'event' => EVENT_PONG,
'channel' => 'public-test',
]);
// 断言检测回执buffer 所有连接不应接收到回执
$this->assertNull($tcpConnection->getSendBuffer());
Expand Down

0 comments on commit 34035ea

Please sign in to comment.