【发布时间】:2019-11-05 17:14:21
【问题描述】:
我正在尝试使用 Laravel Echo 在私人频道上收听,但它不仅仅在私人频道上收听,公共频道工作正常。
我也在使用 beyondcode/Laravel-Websockets 包,它在 websockets 仪表板面板中向我展示了所有的连接和事件。它显示了包本身建立的所有私有连接、我的公共连接和所有触发的事件(私有和公共),但它没有显示我的私有连接
如果触发事件并将数据保存在数据库中,一切正常,除了在视图中,我没有收到来自触发的私有事件的任何数据
当预定义为 "^1.5.4"
时,我尝试将 Laravel Echo 版本降级到版本 "^1.3.2"这是我在私人频道上播出的 laravel 活动
<?php
namespace App\Events;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
class NuevoGasto implements ShouldBroadcastNow
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $gasto;
public function __construct($gasto)
{
$this->gasto = $gasto;
}
public function broadcastOn()
{
return new PrivateChannel('nuevo-gasto-channel');
}
}
注意我正在使用 ShouldBroadcastNow 特征,所以我不需要使用队列
这是我的 bootstrap.js 文件,里面有我的 Laravel Echo 配置
import Echo from 'laravel-echo'
window.Pusher = require('pusher-js');
window.Echo = new Echo({
broadcaster: 'pusher',
key: process.env.MIX_PUSHER_APP_KEY,
cluster: process.env.MIX_PUSHER_APP_CLUSTER,
wsHost: window.location.hostname,
wsPort: 6001,
disableStats: true,
});
这是我的 routes/channels.php 文件
Broadcast::channel('nuevo-gasto-channel', function ($user) {
return true;
});
请注意,出于演示目的,我返回 true
这是我在 nuevo-gasto-channel 上侦听 NuevoGasto 事件时的视图文件
Echo.private('nuevo-gasto-channel')
.listen('NuevoGasto', (e) => {
console.log(e);
});
最后这是我的控制器,我触发事件的地方
...
broadcast(new NuevoGasto($gasto))->toOthers();
...
我没有收到任何错误,只是没有在私人频道上收听
【问题讨论】: