广播频道的 Laravel 授权不起作用

gha*_*han 6 laravel laravel-5 laravel-5.4

我正在尝试使用 Laravel 5.4(后端)和 Angular 2(前端)将聊天集成到 web 应用程序中。我有一个独特的广播频道,可以向用户发送消息。每个用户都有自己的渠道是chat-{uuid}uuid是其中还存储了一个聊天表的唯一IDuserID谁可以访问该频道的用户。问题是授权回调永远不会被调用,因此任何用户如果知道uuid. 我不确定我做错了什么

频道.php

// This authorization is never called 
Broadcast::channel('chat-{uuid}', function ($user, $uuid) {
    return false; 
});
Run Code Online (Sandbox Code Playgroud)

NewMessage.php 事件

protected     $message;

public function __construct($message)
{
    $this->message = $message;
}

protected function prepareData()
{
    return [
        'chatID'                => $this->message->chatID,
        'userID'                => $this->message->builderID,
        'message'               => $this->message->message,
        ];

}

public function broadcastWith()
{
    return [
        'message' => $this->prepareData(),
    ];
}

public function broadcastAs()
{
    return 'new.message';
}

public function broadcastOn()
{
    return new PrivateChannel('chat-'.$this->message->chatID);
}
Run Code Online (Sandbox Code Playgroud)

广播服务提供者.php

public function boot()
{
    Broadcast::routes( [ 'middleware' => [ 'api', 'auth.jwt' ] ] );
    require base_path('routes/channels.php');
}
Run Code Online (Sandbox Code Playgroud)

连接到前端的 Broadchannel(正在接收消息)

组件.ts

  window['Echo'] = new Echo({
    broadcaster: 'socket.io',
    host: 'http://app.test:6001',
    auth:
      {
        headers:
          {
            'Authorization': 'Bearer ' + this.auth.jwt
          }
      }
  });

window['Echo'].private(`chat-${this.chatUUID}`)
  .listen(".new.message", (data) => {
    this.messages.data.push(data.message);
  });
Run Code Online (Sandbox Code Playgroud)

小智 1

显然问题不在于编程方面,而与您的 Web 服务器配置(或 Web 套接字服务器)有关。我认为你总是通过私人通道进行身份验证,但不是通过/broadcusting/auth简单的方式,因为响应给你 200 代码。正如您在laravel-echo-server 源通道中看到的那样,残留的身份验证。因此,您routes/channels可能不会参与该过程。BroadcastServiceProvider此外,如果您从配置中排除,也许您无论如何都会获得经过身份验证的专用通道。

如果一切都像这样,我建议您查看一下 Web 服务器配置,特别是如果您使用 SSL。当然还有替代的 SSL 实现laravel-echo-server.json