2017-07-27 31 views
6

我有我的推送键集,并在Laravel 5.3中初始化。当我在我的本地环境中测试它时,它可以工作。当我尝试运行我们的生产环境完全相同的代码,我得到这个错误:Laravel广播授权路由只是返回“true”

Pusher : Error : {"type":"WebSocketError","error":{"type":"PusherError","data":{"code":null,"message":"Auth info required to subscribe to private-App.User.16"}}} 

我已经证实了推关键是我的地方和生产都相同。

的WS上都初始化环境相同:

wss://ws.pusherapp.com/app/264P9d412196d622od64d?protocol=7&client=js&version=4.1.0&flash=false 

唯一的区别,我可以看到,就是当我们的生产服务器联系Laravel“广播/ AUTH”的路线,它只是接收true在应对机构。

当我的本地联系人 “广播/ 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路线简单地返回,而不是true预期的认证?

+0

不应该在'route/channels.php'上吗? [链接](https://github.com/laravel/laravel/blob/master/app/Providers/BroadcastServiceProvider.php) –

+0

@AntoniosTsimourtos是Laravel 5.3之后的版本。 – eComEvo

+0

这是基本的,但我想它值得问:你使用'composer.lock'来确保你在两个envs中都有相同的代价? –

回答

2

如果检查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.

这里面PusherBroadcastvalidAuthenticationResponse方法。

/** 
* 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实例化所有 “可用的驱动程序”(推,Redis的,日志等),并创建了 “权威性” 路线(使用BroadcastController +认证方法)。

当调用 “身份验证”,会出现这种情况:

  1. 呼叫 “broadc .../AUTH” 路线。
  2. BroadcastManager将实例正确的驱动程序(你的情况推
  3. PusherBroadcaster可以抛出一个异常AccessDeniedHttpException如果用户没有通过验证(以下简称“用户会话” - 验证::用户()没有定义/空)并尝试访问私人(或在线)频道类型。
  4. 如果用户试图访问private/presence频道并且用户已通过身份验证(Auth :: check()),则Laravel将检查身份验证是否正确。用户可以访问该频道。 (检查:verifyUserCanAccessChannel方法)。
  5. 之后,将调用validAuthenticationResponse方法。此方法将请求推送用户凭据并返回数组。此数组包含Pusher响应(套接字身份验证:https://github.com/pusher/pusher-http-php/blob/03d3417748fc70a889c97271e25e282ff1ff0ae3/src/Pusher.php#L586/Presence身份验证:https://github.com/pusher/pusher-http-php/blob/03d3417748fc70a889c97271e25e282ff1ff0ae3/src/Pusher.php#L615),它是一个字符串。

简短的回答

洙..推杆需要此AUTH响应。否则,您将无法连接/识别用户(wss://ws.pusherapp.com ....)。

+0

感谢您的详细解释。这使我指出了正确的方向,但为什么'validAuthenticationResponse'中的'$ result'可能是Pusher在不同环境中的布尔响应? – eComEvo

1

编辑这是来自5.5版本的文档,在这里不适用。

我认为这个问题可能在通道名称中使用'*'通配符。

我使用本地和生产以下产品:

Broadcast::channel("servers.{id}", function (Authenticatable $user, $id) { 
    return (int)$user->getAuthIdentifier() === (int)$id; 
}); 
+0

这很奇怪,因为[documentation](https://laravel.com/docs/5.3/broadcasting#authorizing-channels)另有说明。 – Camilo

+0

我的不好,我忽略了他说的5.3版本。这是5.5。 – btl