diff --git a/src/Events/ClientEvent.php b/src/Events/ClientEvent.php index 7e3863b..82af0a3 100644 --- a/src/Events/ClientEvent.php +++ b/src/Events/ClientEvent.php @@ -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; } // 广播 客户端消息 diff --git a/src/Events/Subscribe.php b/src/Events/Subscribe.php index fb1c842..6c6cc65 100644 --- a/src/Events/Subscribe.php +++ b/src/Events/Subscribe.php @@ -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) { @@ -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']); @@ -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); @@ -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; } } diff --git a/src/Events/Unsubscribe.php b/src/Events/Unsubscribe.php index 835daa3..c4c8dfa 100644 --- a/src/Events/Unsubscribe.php +++ b/src/Events/Unsubscribe.php @@ -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'); } } diff --git a/src/PushServer.php b/src/PushServer.php index 0a1ec19..9a30ce5 100644 --- a/src/PushServer.php +++ b/src/PushServer.php @@ -147,7 +147,7 @@ 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; } // 默认在空字符串域 @@ -155,7 +155,7 @@ function (TcpConnection $connection, string $header) use ($socketId) { // 获取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; } } @@ -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); } diff --git a/tests/PushServerBaseTest.php b/tests/PushServerBaseTest.php index d5a08b3..5e291bd 100644 --- a/tests/PushServerBaseTest.php +++ b/tests/PushServerBaseTest.php @@ -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'] ?? []); } @@ -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'] ?? []); } @@ -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); diff --git a/tests/PushServerEventTest.php b/tests/PushServerEventTest.php index b36d2a0..ec39406 100644 --- a/tests/PushServerEventTest.php +++ b/tests/PushServerEventTest.php @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); }