php – Laravel广播认证路由只返回“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“broadcast / 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;
    });
}

什么可能导致广播/验证路由返回简单而不是预期的身份验证?

如果您检查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)
>如果用户未经过身份验证(“用户会话” – 未定义Auth :: user()/ null)并且正在尝试访问私有(或状态)通道类型,则Pusherbroadcaster可以抛出异常AccessDeniedHttpException.
>如果用户正在尝试访问私人/在线频道并且用户已通过身份验证(Auth :: check()),则Laravel将检查是否已验证.用户可以访问该频道. (检查:verifyUserCanAccessChannel方法).
>之后,将调用validAuthenticationResponse方法.此方法将使用用户凭据请求推送并返回一个数组.该数组包含Pusher响应(socket auth: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 ….).

相关文章

laravel的dd函数不生效怎么办
看不懂laravel文档咋办
安装laravel框架出现command怎么办
Laravel开发API怎么使用事务
laravel怎么构建复杂查询条件
laravel如何实现防止被下载