为什么我没有收到广播消息Laravel Pusher?

时间:2018-12-09 21:04:59

标签: laravel laravel-5

我的活动广播如下:

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

    public $username;

    public $message;

    public function broadcastAs()
    {
        return 'my-event';
    }

    public function __construct($username)
    {
        $this->username = $username;
        $this->message = "Order status by {$username} was changed";
    }

    public function broadcastOn()
    {

        return ['my-event'];
    }
}

呼叫事件,例如:

Route::get('/', function () {
    OrderStatusUpdate::dispatch('Nick');
    return "Event has been sent!";
});

客户端是:

 var pusher = new Pusher('018814b79958ee4f2ad1d', {
      cluster: 'eu',
      forceTLS: true
    });

    var channel = pusher.subscribe('my-event');
    channel.bind('my-event', function(data) {
      alert(JSON.stringify(data));
    });

所有.env的设置都是正确的!

如您所见,我使用默认的Laravel设置和频道名称。但是消息在Pusher服务器中带有错误。但是我没有在HTML页面上显示它们,代码JS却显示在上方。

1 个答案:

答案 0 :(得分:0)

让我从客户端解释一下:

var channel = pusher.subscribe('order');
    channel.bind('OrderStatusUpdate', function(data) {
      alert(JSON.stringify(data));
    });

order频道的ID名称 OrderStatusUpdate是事件名称Event

这意味着服务器应该是:

public function broadcastAs()
{
  return 'OrderStatusUpdate'; // Event name
}

public function broadcastOn()
{

  return ['order'];
}

但我不确定此数组的正弦值:return ['order'];,请尝试以下方法:

public function broadcastOn()
{

 return new Channel("order");
}