Skip to content

Commit

Permalink
feat: 优化错误返回信息
Browse files Browse the repository at this point in the history
  • Loading branch information
chaz6chez committed Jul 24, 2024
1 parent 9f8e4e6 commit 04a68f0
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 32 deletions.
10 changes: 5 additions & 5 deletions src/Events/ClientEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,26 @@ public function response(TcpConnection $connection, array $request): void
{
// 事件必须以client-为前缀
if (!str_starts_with($event = $request['event'] ?? '', 'client-')) {
PushServer::error($connection, null, 'Client event rejected - client events must be prefixed by \'client-\'');
PushServer::error($connection, '403', 'Client rejected - client events must be prefixed by \'client-\'');
return;
}
if (!$channel = $request['channel'] ?? null){
PushServer::error($connection, null, 'Bad channel');
PushServer::error($connection, '404', 'Client error - Bad channel');
return;
}
if (!$data = $request['data'] ?? []){
PushServer::error($connection, null, 'Bad data');
PushServer::error($connection, '400', 'Client error - Empty data');
return;
}
// 当前链接没有订阅这个channel
if (!isset(PushServer::getConnectionProperty($connection, 'channels')[$channel])) {
PushServer::error($connection, null, 'Client event rejected - you didn\'t subscribe this channel');
PushServer::error($connection, '403', 'Client rejected - you didn\'t subscribe this channel');
return;
}
// 客户端触发事件必须是private 或者 presence的channel
$channelType = PushServer::getChannelType($channel);
if ($channelType !== CHANNEL_TYPE_PRIVATE and $channelType !== CHANNEL_TYPE_PRESENCE) {
PushServer::error($connection, null, 'Client event rejected - only supported on private and presence channels');
PushServer::error($connection, '403', 'Client rejected - only supported on private and presence channels');
return;
}
// 广播 客户端消息
Expand Down
12 changes: 6 additions & 6 deletions src/Events/Subscribe.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function response(TcpConnection $connection, array $request): void
switch ($channelType = PushServer::getChannelType($channel)){
case CHANNEL_TYPE_PRESENCE:
if (!$channelData) {
PushServer::error($connection, null, 'Empty channel_data');
PushServer::error($connection, '400', 'Client error - Empty channel_data');
return;
}
if ($appsCallback) {
Expand All @@ -76,15 +76,15 @@ public function response(TcpConnection $connection, array $request): void
]);
}
if ($clientAuth !== $auth) {
PushServer::error($connection, null, 'Received invalid Auth ' . $clientAuth);
PushServer::error($connection, '403', 'Client rejected - Received invalid Auth ' . $clientAuth);
return;
}
if (!isset($channelData['user_id']) or !is_string($channelData['user_id'])) {
PushServer::error($connection,null, 'Bad channel_data.user_id');
PushServer::error($connection,'400', 'Client error - Bad channel_data.user_id');
return;
}
if (!isset($channelData['user_info']) or !is_string($channelData['user_info'])) {
PushServer::error($connection,null, 'Bad channel_data.user_info');
PushServer::error($connection,'400', 'Client error - Bad channel_data.user_info');
return;
}
self::subscribeChannel($connection, $channel, $channelType, $channelData['user_id'], $channelData['user_info']);
Expand All @@ -107,7 +107,7 @@ public function response(TcpConnection $connection, array $request): void
]);
}
if ($clientAuth !== $auth) {
PushServer::error($connection,null, 'Received invalid Auth ' . $clientAuth);
PushServer::error($connection,'403', 'Client rejected - Received invalid Auth ' . $clientAuth);
return;
}
self::subscribeChannel($connection, $channel, $channelType);
Expand All @@ -116,7 +116,7 @@ public function response(TcpConnection $connection, array $request): void
self::subscribeChannel($connection, $channel, $channelType);
break;
default:
PushServer::error($connection, null, 'Bad channel_type');
PushServer::error($connection, '403', 'Client rejected - Bad channel_type');
break;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Events/Unsubscribe.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ public function response(TcpConnection $connection, array $request): void
case CHANNEL_TYPE_PRESENCE:
$userData = json_decode($request['data']['channel_data'] ?? '{}', true);
if (!$userData or !isset($userData['user_id'])) {
PushServer::error($connection, null, 'Bad channel_data');
PushServer::error($connection, '400', 'Client error - Bad channel_data');
return;
}
self::unsubscribeChannel($connection, $channel, $userData['user_id']);
break;
default:
PushServer::error($connection, null, 'Bad channel_type');
PushServer::error($connection, '403', 'Client rejected - Bad channel_type');
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/PushServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,15 @@ public function onConnect(TcpConnection $connection): void
function (TcpConnection $connection, string $header) use ($socketId) {
$request = new Request($header);
if (!preg_match('/\/app\/([^\/^\?^]+)/', $request->path() ?? '', $match)) {
static::error($connection, null, 'Invalid app', true);
static::error($connection, '403', 'Client rejected - Invalid app', true);
return;
}
// 默认在空字符串域
$appKey = '';
// 获取app验证回调,如果没有验证回调则忽略验证
if ($appVerifyCallback = static::getConfig('app_verify', getBase: true)) {
if (!call_user_func($appVerifyCallback, $appKey = $match[1])) {
static::error($connection, null, "Invalid app_key", true);
static::error($connection, '403', 'Client rejected - Invalid app_key', true);
return;
}
}
Expand Down Expand Up @@ -215,7 +215,7 @@ function (TcpConnection $connection, $data) {
return;
}
}
static::error($connection,null, 'Client event rejected - Unknown event');
static::error($connection,'403', 'Client rejected - Unknown event');
}
}, $connection, $data);
}
Expand Down
12 changes: 6 additions & 6 deletions tests/PushServerBaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ public function testPushServerOnConnectInvalidAppError()
$data = @json_decode($connection->getSendBuffer(), true) ?: [];
$this->assertEquals(EVENT_ERROR, $data['event'] ?? null);
$this->assertEquals([
'code' => null,
'message' => 'Invalid app'
'code' => '403',
'message' => 'Client rejected - Invalid app'
], $data['data'] ?? []);
}

Expand Down Expand Up @@ -141,8 +141,8 @@ public function testPushServerOnConnectInvalidAppKeyError()
$data = @json_decode($connection->getSendBuffer(), true) ?: [];
$this->assertEquals(EVENT_ERROR, $data['event'] ?? null);
$this->assertEquals([
'code' => null,
'message' => 'Invalid app_key'
'code' => '403',
'message' => 'Client rejected - Invalid app_key'
], $data['data'] ?? []);
}

Expand Down Expand Up @@ -218,8 +218,8 @@ public function testPushServerOnMessageIllegalData(){
$data = @json_decode($connection->getSendBuffer(), true) ?: [];
$this->assertEquals(EVENT_ERROR, $data['event'] ?? null);
$this->assertEquals([
'code' => null,
'message' => 'Client event rejected - Unknown event'
'code' => '403',
'message' => 'Client rejected - Unknown event'
], $data['data'] ?? []);
// 初始化回执buffer
$connection->setSendBuffer(null);
Expand Down
20 changes: 10 additions & 10 deletions tests/PushServerEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,8 @@ public function testPushServerEventClientEventError()
$data = @json_decode($connection->getSendBuffer(), true) ?: [];
$this->assertEquals(EVENT_ERROR, $data['event'] ?? null);
$this->assertEquals([
'code' => null,
'message' => 'Client event rejected - client events must be prefixed by \'client-\''
'code' => '403',
'message' => 'Client rejected - client events must be prefixed by \'client-\''
], $data['data'] ?? null);
// 设置回执buffer null
$connection->setSendBuffer(null);
Expand All @@ -356,8 +356,8 @@ public function testPushServerEventClientEventError()
$data = @json_decode($connection->getSendBuffer(), true) ?: [];
$this->assertEquals(EVENT_ERROR, $data['event'] ?? null);
$this->assertEquals([
'code' => null,
'message' => 'Bad channel'
'code' => '404',
'message' => 'Client error - Bad channel'
], $data['data'] ?? null);
// 设置回执buffer null
$connection->setSendBuffer(null);
Expand All @@ -381,8 +381,8 @@ public function testPushServerEventClientEventError()
$data = @json_decode($connection->getSendBuffer(), true) ?: [];
$this->assertEquals(EVENT_ERROR, $data['event'] ?? null);
$this->assertEquals([
'code' => null,
'message' => 'Bad data'
'code' => '400',
'message' => 'Client error - Empty data'
], $data['data'] ?? null);
// 设置回执buffer null
$connection->setSendBuffer(null);
Expand All @@ -409,8 +409,8 @@ public function testPushServerEventClientEventError()
$data = @json_decode($connection->getSendBuffer(), true) ?: [];
$this->assertEquals(EVENT_ERROR, $data['event'] ?? null);
$this->assertEquals([
'code' => null,
'message' => 'Client event rejected - you didn\'t subscribe this channel'
'code' => '403',
'message' => 'Client rejected - you didn\'t subscribe this channel'
], $data['data'] ?? null);
// 设置回执buffer null
$connection->setSendBuffer(null);
Expand Down Expand Up @@ -487,8 +487,8 @@ public function testPushServerEventClientEventFailure()
$data = @json_decode($connection->getSendBuffer(), true) ?: [];
$this->assertEquals(EVENT_ERROR, $data['event'] ?? null);
$this->assertEquals([
'code' => null,
'message' => 'Client event rejected - only supported on private and presence channels'
'code' => '403',
'message' => 'Client rejected - only supported on private and presence channels'
], $data['data'] ?? null);
}

Expand Down

0 comments on commit 04a68f0

Please sign in to comment.