Laravel Echo 500错误(broadcasting/auth)?

4

祝您拥有愉快的一天!

我遇到了一个问题: 我设置了Laravel Echo & Pusher,但出现了这个错误,不知道该如何解决 :(

enter image description here

我检查了我的应用密钥和应用集群,但是所有的都是正确的。
有人可以帮帮我吗?

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;
});

你的 laravel.log 文件中是否包含任何内容? - user320487
4个回答

6

如果您在使用 Laravel 版本> 5.3 和 Pusher 时遇到错误403或500 /broadcasting/auth,您需要更改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

这对我很有用,希望能对您有所帮助。


这是正确的答案! - Crysfel

2

由于您没有从事件中传递$id,因此请将其删除

最初的回答翻译成中文为"原始答案"

Broadcast::channel('chatroom', function ($user) {
  return true;
});

0

如果你正在本地工作,请确保你的.env文件已经正确设置。

尝试进行设置。

APP_URL=http://localhost
DB_HOST=localhost

并运行

php artisan config:cache

希望这能对你有所帮助。

0

我认为如果您使用了Laravel Echo,您需要提供一个真实的观点,只需前往Resources/assets/js/bootstrap.js。

只需在窗口中添加以下行:

Echo = new Echo({
authEndpoint : 'http://localhost/projectName/public/broadcasting/auth',
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接