【问题标题】:Pusher Doesn't Broadcast on Private Channels -PHP/LaravelPusher 不在私人频道上广播-PHP/Laravel
【发布时间】:2018-02-23 19:45:51
【问题描述】:

我已经为我的应用设置了 Pusher 和 Laravel Echo,以便在某些事件触发时通知用户。

我已经通过在“公共频道”上广播来测试设置是否正常工作,并成功地看到了。

这是事件本身:

class ItemUpdated implements ShouldBroadcast
{
  use Dispatchable, InteractsWithSockets, SerializesModels;

  public $item;

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

  public function broadcastOn()
  {
      return new Channel('items');
  }
}

公共频道:

Broadcast::channel('items', function ($user, $item) {
  return true;
});

app/resources/assets/js/bootstrap.js:

import  Echo from 'laravel-echo';
window.Pusher = require('pusher-js');

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: 'my_key',
    cluster: 'eu',
    encrypted: true
});

还有 Laravel Echo 注册:(它位于我的主布局的“头部”部分,事件触发视图从该部分扩展。)

<head>
<meta charset="utf-8">


<meta name="csrf-token" content="{{ csrf_token() }}">

<script src="{{ asset('js/app.js') }}"></script>

<script>
    window.Laravel = {'csrfToken': '{{csrf_token()}}'}

    window.Echo.channel('items')
    .listen('ItemUpdated', function(e) {
        console.log("It is working:)");
    });

</script>
</head>

现在,此设置适用于公共频道广播,但是当我尝试在私人频道上广播时,我得到了

  //updated "ItemUpdated" event broadcastOn() method
  public function broadcastOn()
  {
      return new PrivateChannel('items');
  }

//updated laravel echo registering
<script>
window.Laravel = {'csrfToken': '{{csrf_token()}}'}

window.Echo.private('items')
.listen('ItemUpdated', function(e) {
    console.log("It is working:)");
});

 //console logged error
Pusher couldn't get the auth user from :myapp 500

我检查了来自开发者控制台的传出网络请求,发现推送器正在尝试“POST”到

http://localhost:8000/broadcast/auth

但它得到一个 500 错误代码。

认为这可能有助于找出问题。

我该怎么办?

【问题讨论】:

  • 这是一个500 错误。检查您的日志。
  • 是的,但是由于我的服务器在公共广播中正常工作,我不知道问题出在哪里
  • 如上所述。检查您的日志。他们说什么?
  • Log infoPusher:无法从您的 webapp 获取身份验证信息:500
  • 这是你服务器的错误日志,还是推送者的错误日志?

标签: php laravel pusher


【解决方案1】:

所以我想出了我应该如何实现私人频道监听并让它工作。

我的错误在于通配符的选择。

我告诉它使用经过身份验证的用户 ID 作为通配符,而不是应用更改的项目 ID。

因此,这个下面的频道注册总是返回 false 并导致 Pusher 抛出 500 auth 找不到。

  Broadcast::channel('items.{item}', function ($user, \App\Item $item) {
    return $user->id === $item->user_id;
});

所以,这里是工作版本:

正在广播的事件:

//ItemUpdated event

class ItemUpdated implements ShouldBroadcast
{
   use Dispatchable, InteractsWithSockets, SerializesModels;

   public $item;

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

   public function broadcastOn()
   {
       return new PrivateChannel('items.'.$this->item->id);
   }
}

频道注册:

app\routes\channels.php    

Broadcast::channel('items.{item}', function ($user, \App\Item $item) {
    return $user->id === $item->user_id;
});

Laravel Echo 注册:

<head>

<meta name="csrf-token" content="{{ csrf_token() }}">


<script>
    window.Laravel = {'csrfToken': '{{csrf_token()}}'}

    window.Echo.private(`items.{{{$item->id}}}`)
    .listen('ItemUpdated', function(e) {
        console.log("It is working!");
    });

</script>

谢谢大家的指点。

【讨论】:

    猜你喜欢
    • 2020-03-24
    • 2021-11-15
    • 2021-07-15
    • 2019-06-04
    • 2021-06-17
    • 2021-06-01
    • 2018-03-03
    • 2023-03-17
    • 2017-05-12
    相关资源
    最近更新 更多