Laravel Echo未订阅私人频道

时间:2020-08-02 16:27:49

标签: laravel websocket pusher laravel-echo

  • 回声版本:1.8.0
  • Laravel版本:7.0
  • PHP版本:7.2。
  • NPM版本:6.12.1
  • 节点版本:12.13.1

说明:我正在尝试向用户发送通知,

我的推送程序连接良好,因为它在公共频道上发送 而且,我在控制台上收到此消息 [2020-07-24 21:08:09][355] Processed: Illuminate\Notifications\Events\BroadcastNotificationCreated 在推杆上,我还看到消息已发送。 我的浏览器控制台也没有出现任何错误,因为所有的websocket连接都运行良好。 但是我仍然没有收到任何消息。 我尝试过

Echo.private('App.User.' +userId)
   .notification((notification) => {
   console.log(notification);
  });

我也尝试过

Echo.private('App.User.' + userId)
  .listen('.Illuminate\\Notifications\\Events\\BroadcastNotificationCreated', (e) => {
  console.log('Event Notification received ', e)
  });

现在我打开了Pusher错误日志记录,我得到了 app.js:42600 Pusher : : ["JSON returned from auth endpoint was invalid, yet status code was 200. Data was: <!DOCTYPE html>\r\n<html lang=\"en\">\r\n

我还关注了许多教程和文档 全部无济于事。

复制步骤:

创建通知并声明toArray方法和toBroadcast方法,但是echo无法捕获来自pusher的消息

1 个答案:

答案 0 :(得分:0)

我终于自己解决了这个问题

步骤

在我的bootstrap.js文件中

window.Echo = new Echo({
      broadcaster: "pusher",
      key: "my key",
      cluster: "eu",
      encrypted: true,
      authEndpoint: "api/broadcasting/auth",
      auth: {
        headers: {
          Authorization: "Bearer " + localStorage.getItem("token"),
        },
      },
    });

然后在我的route / api.php文件中

Route::middleware('auth:api')->post('broadcasting/auth', function (Request $request) {
    $pusher = new Pusher\Pusher(
    $app_key,
    $app_secret,
    $app_id
);
return $pusher->socket_auth($request->request->get('private-my-channel'),($request->request->get('socket_id'));
});

documentation中对此有更多信息, 我希望有一天能对某人有所帮助