度过愉快的一天!
我有一个问题: 我成立了Laravel Echo& Pusher但是得到了这个错误,不知道解决:(
我检查了我的app-key,app-cluster但都是正确的。
有人可以帮助我吗?
app.js
const app = new Vue({
el: '#app',
data: {
messages: []
},
methods:{
addMessage(message){
this.messages.push(message);
axios.post('/messages', message).then(response => {
console.log(response);
});
}
},
created(){
axios.get('/messages').then(response => {
this.messages = response.data;
});
Echo.channel('chatroom')
.listen('MessageEvent', (e) => {
console.log(e);
});
}
})
bootstrap.js
import Echo from 'laravel-echo'
window.Pusher = require('pusher-js');
window.Echo = new Echo({
broadcaster: 'pusher',
key: '************',
cluster: 'ap1',
encrypted: false
});
MessageEvent事件
use Dispatchable, InteractsWithSockets, SerializesModels;
public $message, $user;
public function __construct(Message $message, User $user)
{
$this->message = $message;
//query
$this->user = $user;
}
public function broadcastOn()
{
return new PresenceChannel('chatroom');
}
channels.php
Broadcast::channel('App.User.{id}', function ($user, $id) {
return (int) $user->id === (int) $id;
});
Broadcast::channel('chatroom', function ($user, $id) {
return $user;
});
答案 0 :(得分:3)
使用Laravel版本的错误403或500 / broadcast / auth> 5.3& Pusher,您需要使用
更改 resources / assets / js / bootstrap.js 中的代码window.Echo = new Echo({
broadcaster: 'pusher',
key: 'your key',
cluster: 'your cluster',
encrypted: true,
auth: {
headers: {
Authorization: 'Bearer ' + YourTokenLogin
},
},
});
在 app / Providers / BroadcastServiceProvider.php 中更改
Broadcast::routes()
与
Broadcast::routes(['middleware' => ['auth:api']]);
或
Broadcast::routes(['middleware' => ['jwt.auth']]); //if you use JWT
它对我有用,希望对你有帮助。
答案 1 :(得分:0)
如果您使用了laravel echo,我认为你需要给出一个真实的观点 Resources / assets / js / bootstrap.js
只需在窗口中添加以下行
即可Echo = new Echo({
authEndpoint : 'http://localhost/projectName/public/broadcasting/auth',
});
答案 2 :(得分:0)
删除未通过事件传递的$ id
Broadcast::channel('chatroom', function ($user) {
return true;
});
答案 3 :(得分:0)
如果您在本地主机上工作,请确保正确设置.env文件
尝试设置
APP_URL=http://localhost
DB_HOST=localhost
然后运行
php artisan config:cache
希望这会对您有所帮助。