我在 Laravel 5.3 中设置并初始化了我的按键。当我在我的本地环境中测试它时,它有效。当我尝试在我们的生产环境中运行完全相同的代码时,出现此错误:
Pusher : Error : {"type":"WebSocketError","error":{"type":"PusherError","data":{"code":null,"message":"Auth info required to subscribe to private-App.User.16"}}}
我已确认 Pusher key 在我的本地和生产环境中是相同的。
WS 在两个环境中初始化相同:
wss://ws.pusherapp.com/app/264P9d412196d622od64d?protocol=7&client=js&version=4.1.0&flash=false
我能看到的唯一区别是,当我们的生产服务器联系 Laravel 的“broadcasting/auth”路由时,它只是在响应正文中接收到 true
。
当我的本地联系人“广播/授权”时,它会在响应中得到这个:
{auth: "22459d41299d6228d64d:df5d393fe37df0k3832fa5556098307f145d7e483c07974d8e7b2609200483f8"}
在我的 BroadcastServiceProvider.php
中:
public function boot()
{
Broadcast::routes();
// Authenticate the user's personal channel.
Broadcast::channel('App.User.*', function (User $user, $user_id) {
return (int)$user->id === (int)$user_id;
});
}
什么会导致 broadcast/auth
路由返回简单的 true
而不是预期的 auth?
最佳答案
如果您检查 PusherBroadcaster.php
文件,您会看到响应可以是“混合的”。
我认为文档只是在谈论默认广播。
The channel method accepts two arguments: the name of the channel and a callback which returns true or false indicating whether the user is authorized to listen on the channel.
这是 PusherBroadcast
中的 validAuthenticationResponse
方法。
/**
* Return the valid authentication response.
*
* @param \Illuminate\Http\Request $request
* @param mixed $result
* @return mixed
*/
public function validAuthenticationResponse($request, $result)
{
if (Str::startsWith($request->channel_name, 'private')) {
return $this->decodePusherResponse(
$this->pusher->socket_auth($request->channel_name, $request->socket_id)
);
}
return $this->decodePusherResponse(
$this->pusher->presence_auth(
$request->channel_name, $request->socket_id, $request->user()->getAuthIdentifier(), $result)
);
}
再举一个例子,这是在 RedisBroadcast
中。
if (is_bool($result)) {
return json_encode($result);
}
关于这个“授权请求”的简短解释:
BroadcastManager
实例化所有“可用驱动程序”(Pusher、Redis、Log 等),并创建“auth”路由(使用 BroadcastController + 身份验证方法)。
当你调用“auth”时,会发生这样的事情:
- 调用“broadc.../auth”路由。
- BroadcastManager 将实例化适当的驱动程序(在您的情况下为 Pusher)
PusherBroadcaster
可以抛出异常AccessDeniedHttpException
如果用户未通过身份验证(“用户 session ” - Auth::user() 未定义/为空)并且是尝试访问私有(private)(或存在) channel 类型。- 如果用户试图访问一个
private/presence
channel 并且用户已经过身份验证(Auth::check()),Laravel 将检查是否授权。用户可以访问该 channel 。 (检查:verifyUserCanAccessChannel
方法)。 - 之后,将调用
validAuthenticationResponse
方法。此方法将使用用户凭据向推送器发出请求并返回一个数组。此数组包含 Pusher 响应(套接字身份验证:https://github.com/pusher/pusher-http-php/blob/03d3417748fc70a889c97271e25e282ff1ff0ae3/src/Pusher.php#L586/Presence Auth:https://github.com/pusher/pusher-http-php/blob/03d3417748fc70a889c97271e25e282ff1ff0ae3/src/Pusher.php#L615),它是一个字符串。
简答:
Soo.. Pusher 需要这个授权响应。否则您将无法连接/识别用户(wss://ws.pusherapp.com....)。
关于php - Laravel 广播 auth 路由简单地返回 "true",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45361839/