7bae5e56ff
错误原因: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 订阅
35 lines
1.4 KiB
PHP
35 lines
1.4 KiB
PHP
<?php
|
||
|
||
use Illuminate\Support\Facades\Broadcast;
|
||
|
||
Broadcast::channel('App.Models.User.{id}', function ($user, $id) {
|
||
return (int) $user->id === (int) $id;
|
||
});
|
||
|
||
// 聊天室房间 Presence Channel 鉴权与成员信息抓取
|
||
Broadcast::channel('room.{roomId}', function ($user, $roomId) {
|
||
// 这里未来可以增加判断:比如该房间是否被锁定,或者该用户是否在此房间的黑名单中
|
||
// 凡是通过了这个判断的人(返回一个数组),他就会成功建立 WebSocket,
|
||
// 且他的这个数组信息会被 Reverb 推送给这个房间内的所有其他人 (joining / here 事件)。
|
||
|
||
$superLevel = (int) \App\Models\Sysparam::getValue('superlevel', '100');
|
||
|
||
return [
|
||
'id' => $user->id,
|
||
'username' => $user->username,
|
||
'user_level' => $user->user_level,
|
||
'sex' => $user->sex,
|
||
'headface' => $user->headface, // 通过 accessor 读取 usersf,默认 1.gif
|
||
'vip_icon' => $user->vipIcon(),
|
||
'vip_name' => $user->vipName(),
|
||
'vip_color' => $user->isVip() ? ($user->vipLevel?->color ?? '') : '',
|
||
'is_admin' => $user->user_level >= $superLevel,
|
||
];
|
||
});
|
||
|
||
// 用户私有频道鉴权(好友通知:FriendAdded / FriendRemoved / BannerNotification)
|
||
// 使用数字 ID 命名频道,避免中文用户名导致 Pusher 频道名验证失败。
|
||
Broadcast::channel('user.{id}', function ($user, int $id) {
|
||
return (int) $user->id === $id;
|
||
});
|