在我的laravel.log
中,我可以看到该事件正在频道中广播,即
[2018-08-30 13:41:27] local.INFO: Broadcasting [App\Events\NewRating] on channels [rating] with payload:
{
"music": {
"id": 42,
"name": "1535368873_admin.mp3",
"path": "public\/music\/1535368873_admin.mp3",
"rating": 53,
"user_id": 4,
"created_at": "2018-08-27 11:21:13",
"updated_at": "2018-08-27 11:21:13",
"is_last_played": 1,
"is_now_playing": 0,
"location_id": 1
},
"socket": null
}
据我所知,我已经按照要求完成了所有工作。我的redis
服务器正在运行。laravel-echo-server
正在运行,并且正在加入我的频道,即
[18:41:24] - VX9bkHqs-QHRv3MvAAAC joined channel: rating
这是我订阅频道并收听事件的地方
window.app = new Vue({
el: '#app',
beforeMount: function () {
alert("asdas");
this.joinPublicChatChannel();
},
methods: {
joinPublicChatChannel: function () {
Echo.channel('rating').listen('App\\Events\\NewRating', (event) => {
console.log("Event fired!"); //This doesnt work
});
}
}
});
这是我的活动课
class NewRating implements ShouldBroadcastNow
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $music;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(Music $music)
{
$this->music=$music;
}
/**
* Get the channels the event should broadcast on.
*
* @return Channel|array
*/
public function broadcastOn()
{
return new Channel('rating');
}
}
这是我的环境文件
APP_NAME=Laravel
APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=laravel_restaurant
DB_USERNAME=root
DB_PASSWORD=
BROADCAST_DRIVER=redis
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=
不知道背后的原因:/
更新
只需清除所有缓存,现在我可以在laravel-echo-server
控制台中获取它
Channel: rating
Event: App\Events\NewRating
Channel: rating
Event: App\Events\NewRating
Channel: rating
Event: App\Events\NewRating
答案 0 :(得分:1)
您可以在事件文件中使用它:
public function broadcastAs()
{
return 'NewRating';
}
答案 1 :(得分:0)
没关系,我正在运行:)。刚刚清除了缓存并进入
Echo.channel('rating').listen('App\\Events\\NewRating', (event) => {
console.log("Event fired!"); //This doesnt work
});
我用NewRating
代替了App\\Events\\NewRating