我正在开发一个应用程序,它的前端是Vuetify (Vue.js),它与laravel后端服务器进行通信。
我正在尝试使用laravel-echo-server与socket.io一起开发一个通知系统。在客户中也使用了拉拉回音。
用于测试连接是否工作的客户端组件中的代码是:
// Needed by laravel-echo
window.io = require('socket.io-client')
let token = this.$store.getters.token
let echo = new Echo({
broadcaster: 'socket.io',
host: 'http://localhost:6001',
auth: {
headers: {
authorization: 'Bearer ' + token,
'X-CSRF-TOKEN': 'too_long_csrf_token_hardcoded'
}
}
})
echo.private('Prova').listen('Prova', () => {
console.log('IT WORKS!')
})这是laravel-echo-server.json的代码
{
"authHost": "http://gac-backend.test",
"authEndpoint": "/broadcasting/auth",
"clients": [],
"database": "redis",
"databaseConfig": {
"redis": {},
"sqlite": {
"databasePath": "/database/laravel-echo-server.sqlite"
}
},
"devMode": true,
"host": null,
"port": "6001",
"protocol": "http",
"socketio": {},
"sslCertPath": "",
"sslKeyPath": "",
"sslCertChainPath": "",
"sslPassphrase": "",
"apiOriginAllow": {
"allowCors": false,
"allowOrigin": "",
"allowMethods": "",
"allowHeaders": ""
}
}我试图修改apiOriginsAllow,但没有成功。事件被分派后,我可以看到它在laravel-echo-server日志中:
Channel: Prova
Event: App\Events\Prova但是之后,当我访问客户端的一个组件时,它包含了我可以在laravel-echo服务器日志中看到的连接代码-长时间的错误跟踪和下一个错误:
The client cannot be authenticated, got HTTP status 419如您所见,我将csrf令牌和授权令牌指定到laravel客户端的标头中。但不起作用。
这是routes/channels.php的代码
Broadcast::channel('Prova', function ($user) {
return true;
});我只想听一个事件,如果它是私有的还是公开的,这并不重要,因为当它起作用时,我想把它放在服务工作者身上。那么,我想如果它是公开的,那就更好了。
发布于 2018-09-10 11:11:47
为了启动客户端侦听器,我使用了Vuex。然后,当我的应用程序启动时,我会分派动作INIT_CHANNEL_LISTENERS来启动侦听器。
通道模块index.js of vuex
import actions from './actions'
import Echo from 'laravel-echo'
import getters from './getters'
import mutations from './mutations'
window.io = require('socket.io-client')
export default {
state: {
channel_listening: false,
echo: new Echo({
broadcaster: 'socket.io',
// host: 'http://localhost:6001'
host: process.env.CHANNEL_URL
}),
notifiable_public_channels: [
{
channel: 'Notificacio',
event: 'Notificacio'
},
{
channel: 'EstatRepetidor',
event: 'BroadcastEstatRepetidor'
}
]
},
actions,
getters,
mutations
}通道模块actions.js of vuex
import * as actions from '../action-types'
import { notifyMe } from '../../helpers'
// import { notifyMe } from '../../helpers'
export default {
/*
* S'entent com a notifiable un event que té "títol" i "message" (Per introduir-los a la notificació)
* */
/**
* Inicialitza tots els listeners per als canals. Creat de forma que es pugui ampliar.
* En cas de voler afegir més canals "Notifiables" s'ha de afegir un registre al state del index.js d'aquest modul.
* @param context
*/
[ actions.INIT_CHANNEL_LISTENERS ] (context) {
console.log('Initializing channel listeners...')
context.commit('SET_CHANNEL_LISTENING', true)
context.getters.notifiable_public_channels.forEach(listener => {
context.dispatch(actions.INIT_PUBLIC_NOTIFIABLE_CHANNEL_LISTENER, listener)
})
// }
},
/**
* Inicialitza un event notificable a través d'un canal.
* Per anar bé hauria de tenir un titol i un missatge.
* @param context
* @param listener
*/
[ actions.INIT_PUBLIC_NOTIFIABLE_CHANNEL_LISTENER ] (context, listener) {
context.getters.echo.channel(listener.channel).listen(listener.event, payload => {
notifyMe(payload.message, payload.title)
})
}
}帮助程序中的notifyMe函数,该函数在浏览器上分发通知
export function notifyMe (message, titol = 'TITLE', icon = icon) {
if (!('Notification' in window)) {
console.error('This browser does not support desktop notification')
} else if (Notification.permission === 'granted') {
let notification = new Notification(titol, {
icon: icon,
body: message,
vibrate: [100, 50, 100],
data: {
dateOfArrival: Date.now(),
primaryKey: 1
}
})
}然后后端使用像问题一样的laravel服务器。使用redis对事件进行排队,并在服务器启动时使用主管启动laravel-echo服务器。
发布于 2018-04-25 09:26:27
你好,我给您介绍了如何使用Laravel和Echo功能配置VUE的整个步骤。
Step1安装laravel
composer create-project laravel/laravel your-project-name 5.4.*步骤2集变量更改Broadcastserviceprovider
我们首先需要注册App\Providers\BroadcastServiceProvider。打开config/app.php并取消对提供程序数组中的以下行的注释。
// App\Providers\BroadcastServiceProvider我们需要告诉Laravel,我们正在使用.env文件中的Pusher驱动程序:
BROADCAST_DRIVER=pusher在config/app.php中添加pusher类
'Pusher' => Pusher\Pusher::class,步骤3在您的laravel项目中添加一个推动器
composer require pusher/pusher-php-server步骤4将以下内容添加到config/
'options' => [
'cluster' => env('PUSHER_CLUSTER'),
'encrypted' => true,
],步骤5集推杆变量
PUSHER_APP_ID=xxxxxx
PUSHER_APP_KEY=xxxxxxxxxxxxxxxxxxxx
PUSHER_APP_SECRET=xxxxxxxxxxxxxxxxxxxx
PUSHER_CLUSTER=xx步骤6安装节点
npm install步骤7 Installl Pusher js
npm install --save laravel-echo pusher-js 步骤8 uncommnet以下
// resources/assets/js/bootstrap.js
import Echo from "laravel-echo"
window.Echo = new Echo({
broadcaster: 'pusher',
key: 'xxxxxxxxxxxxxxxxxxxx',
cluster: 'eu',
encrypted: true
});创建迁移之前的步骤9
// app/Providers/AppServiceProvider.php
// remember to use
Illuminate\Support\Facades\Schema;
public function boot()
{
Schema::defaultStringLength(191);
}发布于 2018-04-25 11:08:18
为套接字
npm install socket.io --save
如果您获得了无效的fram报头,请按以下方式设置bootstrap.js中的auth端点
window.Echo = new Echo({ authEndpoint : 'http://project/broadcasting/auth', broadcaster: 'pusher', key: '63882dbaf334b78ff949', cluster: 'ap2', encrypted: true });
https://stackoverflow.com/questions/50017159
复制相似问题