From 7bae5e56ffe9832633228c949405755e07cc410f Mon Sep 17 00:00:00 2001 From: lkddi Date: Sun, 1 Mar 2026 01:41:04 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=EF=BC=9A=E7=A7=81=E6=9C=89?= =?UTF-8?q?=E9=A2=91=E9=81=93=E6=94=B9=E7=94=A8=E6=95=B0=E5=AD=97=20ID?= =?UTF-8?q?=EF=BC=8C=E8=A7=A3=E5=86=B3=E4=B8=AD=E6=96=87=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E5=90=8D=E5=AF=BC=E8=87=B4=20Pusher=20=E9=A2=91=E9=81=93?= =?UTF-8?q?=E5=90=8D=E9=9D=9E=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 错误原因:Pusher 频道名只允许 [a-zA-Z0-9_\-=@,.], 中文用户名(如「超级舞魅」)用于 private-user.{username} 导致 PusherException: Invalid channel name。 修复方案(改用数字 ID): - FriendAdded/FriendRemoved 构造加 toUserId 参数 - broadcastOn() 改为 PrivateChannel('user.' . $toUserId) - FriendController 传入 $target->id / $targetUser->id - channels.php 鉴权改为 'user.{id}',核对 $user->id 数字相等 - frame.blade.php chatContext 加 userId - scripts.blade.php Echo.private 改用 userId 订阅 --- app/Events/FriendAdded.php | 17 +++++++---------- app/Events/FriendRemoved.php | 17 +++++++---------- app/Http/Controllers/FriendController.php | 11 +++++++---- resources/views/chat/frame.blade.php | 1 + resources/views/chat/partials/scripts.blade.php | 8 +++++--- routes/channels.php | 8 ++++---- 6 files changed, 31 insertions(+), 31 deletions(-) diff --git a/app/Events/FriendAdded.php b/app/Events/FriendAdded.php index 0ce31e9..b8bfe3f 100644 --- a/app/Events/FriendAdded.php +++ b/app/Events/FriendAdded.php @@ -3,10 +3,8 @@ /** * 文件功能:好友添加广播事件 * - * 当用户 A 添加用户 B 为好友时,向 B 的私有频道广播此事件, - * B 的客户端收到后展示弹窗通知。 - * 携带 has_added_back 字段:若 B 已将 A 加为好友则为 true(双向好友), - * 否则为 false,前端提示 B 可以点击回加。 + * 当用户 A 添加用户 B 为好友时,向 B 的私有频道广播此事件。 + * 频道名使用数字 ID(user.{id}),避免中文用户名导致 Pusher 频道名验证失败。 * * @author ChatRoom Laravel * @@ -30,28 +28,27 @@ class FriendAdded implements ShouldBroadcastNow * 构造好友添加事件。 * * @param string $fromUsername 发起添加的用户名(A) - * @param string $toUsername 被添加的用户名(B,接收通知方) + * @param string $toUsername 被添加的用户名(B,用于消息显示) + * @param int $toUserId 被添加用户的数字 ID(用于私有频道,避免中文名非法) * @param bool $hasAddedBack B 是否已将 A 加为好友(互相添加=true) */ public function __construct( public readonly string $fromUsername, public readonly string $toUsername, + public readonly int $toUserId, public readonly bool $hasAddedBack = false, ) {} /** - * 广播到被添加用户的私有频道,仅本人可见。 + * 广播到被添加用户的私有频道(用数字 ID 命名,避免中文频道名不合法)。 */ public function broadcastOn(): Channel { - return new PrivateChannel('user.'.$this->toUsername); + return new PrivateChannel('user.'.$this->toUserId); } /** * 指定广播事件名称(短名),供前端 listen('.FriendAdded') 匹配。 - * - * 默认广播名为全类名 App\Events\FriendAdded, - * 指定短名后前端只需 .listen('.FriendAdded')。 */ public function broadcastAs(): string { diff --git a/app/Events/FriendRemoved.php b/app/Events/FriendRemoved.php index 91ad271..164627f 100644 --- a/app/Events/FriendRemoved.php +++ b/app/Events/FriendRemoved.php @@ -3,10 +3,8 @@ /** * 文件功能:好友删除广播事件 * - * 当用户 A 删除用户 B 为好友时,向 B 的私有频道广播此事件, - * B 的客户端收到后展示弹窗通知。 - * 携带 hadAddedBack 字段:若 B 之前也把 A 加为好友(互相好友)则为 true, - * 前端可提示 B "是否同步移除对方"。 + * 当用户 A 删除用户 B 为好友时,向 B 的私有频道广播此事件。 + * 频道名使用数字 ID(user.{id}),避免中文用户名导致 Pusher 频道名验证失败。 * * @author ChatRoom Laravel * @@ -30,28 +28,27 @@ class FriendRemoved implements ShouldBroadcastNow * 构造好友删除事件。 * * @param string $fromUsername 发起删除的用户名(A) - * @param string $toUsername 被删除的用户名(B,接收通知方) + * @param string $toUsername 被删除的用户名(B,用于消息显示) + * @param int $toUserId 被删除用户的数字 ID(用于私有频道,避免中文名非法) * @param bool $hadAddedBack B 之前是否也将 A 加为好友(互相好友=true) */ public function __construct( public readonly string $fromUsername, public readonly string $toUsername, + public readonly int $toUserId, public readonly bool $hadAddedBack = false, ) {} /** - * 广播到被删除用户的私有频道,仅本人可见。 + * 广播到被删除用户的私有频道(用数字 ID 命名,避免中文频道名不合法)。 */ public function broadcastOn(): Channel { - return new PrivateChannel('user.'.$this->toUsername); + return new PrivateChannel('user.'.$this->toUserId); } /** * 指定广播事件名称(短名),供前端 listen('.FriendRemoved') 匹配。 - * - * 默认广播名为全类名 App\Events\FriendRemoved, - * 指定短名后前端只需 .listen('.FriendRemoved')。 */ public function broadcastAs(): string { diff --git a/app/Http/Controllers/FriendController.php b/app/Http/Controllers/FriendController.php index ca8e47f..74dbd6a 100644 --- a/app/Http/Controllers/FriendController.php +++ b/app/Http/Controllers/FriendController.php @@ -112,8 +112,8 @@ class FriendController extends Controller ->where('towho', $me->username) ->exists(); - // 广播给对方(仅对方可见),携带是否已回加的状态 - broadcast(new FriendAdded($me->username, $username, $hasAddedBack)); + // 广播给对方(仅对方可见),携带是否已回加的状态;用数字 ID 作为频道名,避免中文名 + broadcast(new FriendAdded($me->username, $username, $target->id, $hasAddedBack)); // 若对方在线,推送聊天区悄悄话(文案根据互相状态区分) $this->notifyOnlineUser($username, $me->username, 'added', $request->input('room_id'), $hasAddedBack); @@ -152,8 +152,11 @@ class FriendController extends Controller ->where('towho', $me->username) ->exists(); - // 广播给对方,携带之前的互相好友状态 - broadcast(new FriendRemoved($me->username, $username, $hadAddedBack)); + // 查询目标用户 ID(用于私有频道,避免中文名非法) + $targetUser = User::where('username', $username)->first(); + + // 广播给对方,携带之前的互相好友状态;用数字 ID 避免中文频道名 + broadcast(new FriendRemoved($me->username, $username, $targetUser?->id ?? 0, $hadAddedBack)); // 若对方在线,推送聊天区悄悄话(文案根据互相状态区分) $this->notifyOnlineUser($username, $me->username, 'removed', $request->input('room_id'), $hadAddedBack); diff --git a/resources/views/chat/frame.blade.php b/resources/views/chat/frame.blade.php index 0642ee3..52ed5c4 100644 --- a/resources/views/chat/frame.blade.php +++ b/resources/views/chat/frame.blade.php @@ -29,6 +29,7 @@