【问题标题】:Laravel Echo 500 error (broadcasting/auth)?Laravel Echo 500 错误(广播/身份验证)?
【发布时间】:2018-07-14 21:10:52
【问题描述】:

祝你有美好的一天!

我有一个问题: 我设置了 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
});

消息事件

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 文件是否包含任何内容?

标签: php vuejs2 laravel-5.4 pusher laravel-echo


【解决方案1】:

如果您在 localhost 上工作,请确保您的 .env 文件设置正确

尝试设置

APP_URL=http://localhost
DB_HOST=localhost

然后运行

php artisan config:cache

希望这会对你有所帮助。

【讨论】:

    【解决方案2】:

    删除 $id,因为您没有从事件中传递

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

    【讨论】:

      【解决方案3】:

      错误 403 或 500 /broadcasting/auth with Laravel version > 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
      

      它对我有用,希望对你有所帮助。

      【讨论】:

      • 这是正确答案!
      【解决方案4】:

      如果你使用 laravel echo just goto,我认为你需要给出一个真实的观点 资源/资产/js/bootstrap.js

      只需在窗口内添加以下行

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

      【讨论】:

        猜你喜欢
        • 2023-03-17
        • 2020-08-01
        • 2023-03-10
        • 1970-01-01
        • 2018-01-03
        • 2018-01-21
        • 1970-01-01
        • 2018-02-19
        • 2019-09-13
        相关资源
        最近更新 更多