我想在 javascript 端监听服务器端事件。我为此找到了这个包:
https://github.com/tlaverdure/laravel-echo-server
如果这么多次,我已经阅读了所有内容:
https://laravel.com/docs/5.3/events
https://laravel.com/docs/5.3/broadcasting
https://laravel.com/docs/5.3/notifications
到目前为止,我已经这样做了:
控制器动作:
broadcast(new NewVote());
event(new NewVote()); // Also tried this
“NewVote”事件类:
public function broadcastOn()
{
return new Channel('new-vote');
}
Javascript:
import Echo from "laravel-echo"
window.Echo = new Echo({
broadcaster: 'socket.io',
host: 'http://assessment.local:6001'
});
window.Echo.channel('new-vote')
.listen('NewVote', (e) => {
console.log(e);
});
laravel-echo-server.json
{
"appKey": "0p4o9t942ct15boc4nr8tjb178q29fdmlcat8c1p1s51i2pbq9nmtjud94t4",
"authHost": null,
"authEndpoint": "/broadcasting/auth",
"database": "redis",
"databaseConfig": {
"redis": {},
"sqlite": {
"databasePath": "/database/laravel-echo-server.sqlite"
}
},
"devMode": true,
"host": "assessment.local",
"port": "6001",
"referrers": [],
"socketio": {},
"sslCertPath": "",
"sslKeyPath": ""
}
此命令已在运行:
laravel-echo-server start
php artisan queue:work
php artisan queue:listen
gulp watch
当我触发事件时,我期待控制台按摩。但我没有在控制台上得到任何东西。
让我知道是否需要任何其他信息。
注意:我的 socket.io 服务器运行成功,没有任何错误。
谢谢,
帕思·沃拉