Laravel 简单地广播 auth 路由 returns "true"
Laravel broadcasting auth route simply returns "true"
我在 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 密钥在我的本地和生产环境中是相同的。
WS 在两个环境中初始化相同:
wss://ws.pusherapp.com/app/264P9d412196d622od64d?protocol=7&client=js&version=4.1.0&flash=false
我能看到的唯一区别是,当我们的生产服务器联系 Laravel "broadcasting/auth" 路由时,它只是在响应正文中接收 true
。
当我的本地联系人 "broadcasting/auth" 它在响应中得到这个:
{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
到 return 的路由只是 true
而不是预期的身份验证?
编辑这是来自5.5版本的文档,这里不适用。
我认为问题可能与在频道名称中使用“*”通配符有关。
我在本地和生产中都使用以下内容:
Broadcast::channel("servers.{id}", function (Authenticatable $user, $id) {
return (int)$user->getAuthIdentifier() === (int)$id;
});
如果您检查 PusherBroadcaster.php
文件,您会看到响应可以是 "mixed"。
我认为该文档只是在谈论默认广播。
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);
}
对此的简短解释 "auth request":
BroadcastManager
实例化所有 "available drivers"(Pusher、Redis、Log 等),并创建 "auth" 路由(使用 BroadcastController + 身份验证方法).
当您调用 "auth" 时,会发生以下情况:
- 呼叫"broadc.../auth"路线。
- BroadcastManager 将实例化正确的驱动程序(在您的情况下是 Pusher)
如果用户未通过身份验证("user session" - Auth::user() 不是 defined/null),PusherBroadcaster
可以抛出异常 AccessDeniedHttpException
并且是尝试访问私人(或状态)频道类型。
- 如果用户尝试访问
private/presence
频道并且用户已通过身份验证(Auth::check()),Laravel 将检查如果授权。用户可以访问该频道。 (检查:verifyUserCanAccessChannel
方法)。
- 之后,将调用
validAuthenticationResponse
方法。此方法将使用用户凭据和 return 数组向推送器发出请求。该数组包含 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 需要此身份验证响应。否则你将无法 connect/identify 用户 (wss://ws.pusherapp.com...).
出现此问题是因为您没有在生产 .env
文件中设置正确的 BROADCAST_DRIVER
(默认情况下为 redis
)。
# before
BROADCAST_DRIVER=redis
# after
BROADCAST_DRIVER=pusher
我在 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 密钥在我的本地和生产环境中是相同的。
WS 在两个环境中初始化相同:
wss://ws.pusherapp.com/app/264P9d412196d622od64d?protocol=7&client=js&version=4.1.0&flash=false
我能看到的唯一区别是,当我们的生产服务器联系 Laravel "broadcasting/auth" 路由时,它只是在响应正文中接收 true
。
当我的本地联系人 "broadcasting/auth" 它在响应中得到这个:
{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
到 return 的路由只是 true
而不是预期的身份验证?
编辑这是来自5.5版本的文档,这里不适用。
我认为问题可能与在频道名称中使用“*”通配符有关。
我在本地和生产中都使用以下内容:
Broadcast::channel("servers.{id}", function (Authenticatable $user, $id) {
return (int)$user->getAuthIdentifier() === (int)$id;
});
如果您检查 PusherBroadcaster.php
文件,您会看到响应可以是 "mixed"。
我认为该文档只是在谈论默认广播。
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);
}
对此的简短解释 "auth request":
BroadcastManager
实例化所有 "available drivers"(Pusher、Redis、Log 等),并创建 "auth" 路由(使用 BroadcastController + 身份验证方法).
当您调用 "auth" 时,会发生以下情况:
- 呼叫"broadc.../auth"路线。
- BroadcastManager 将实例化正确的驱动程序(在您的情况下是 Pusher) 如果用户未通过身份验证("user session" - Auth::user() 不是 defined/null),
PusherBroadcaster
可以抛出异常AccessDeniedHttpException
并且是尝试访问私人(或状态)频道类型。- 如果用户尝试访问
private/presence
频道并且用户已通过身份验证(Auth::check()),Laravel 将检查如果授权。用户可以访问该频道。 (检查:verifyUserCanAccessChannel
方法)。 - 之后,将调用
validAuthenticationResponse
方法。此方法将使用用户凭据和 return 数组向推送器发出请求。该数组包含 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 需要此身份验证响应。否则你将无法 connect/identify 用户 (wss://ws.pusherapp.com...).
出现此问题是因为您没有在生产 .env
文件中设置正确的 BROADCAST_DRIVER
(默认情况下为 redis
)。
# before
BROADCAST_DRIVER=redis
# after
BROADCAST_DRIVER=pusher