IT이야기

Laravel Echo 500 오류(브로드캐스트/인증)

cyworld 2022. 6. 3. 22:38
반응형

Laravel Echo 500 오류(브로드캐스트/인증)

좋은 하루 보내세요!

문제가 있습니다.Laravel Echo & Pusher를 설정했는데 이 오류가 발생하였습니다.해결 방법이 없습니다.

여기에 이미지 설명 입력

앱키와 앱클러스터를 확인했지만 모두 맞았습니다.

누가 나 좀 도와줄래?

app.module

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

부트스트랩.이네이블로그

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

채널을 선택합니다.php

Broadcast::channel('App.User.{id}', function ($user, $id) {
    return (int) $user->id === (int) $id;
});

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

오류 403 또는 500 /broadcasting/auth with Larabel version > 5.3 & Pusher, 리소스/자산/js/bootstrap.js에서 코드를 변경해야 합니다.

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: 'your key',
    cluster: 'your cluster',
    encrypted: true,
    auth: {
        headers: {
            Authorization: 'Bearer ' + YourTokenLogin
        },
    },
});

/프로바이더/Broadcast Service Provider에 있습니다.php 변경 기준

Broadcast::routes()

와 함께

Broadcast::routes(['middleware' => ['auth:api']]);

또는

Broadcast::routes(['middleware' => ['jwt.auth']]); //if you use JWT

효과가 있었고 도움이 됐으면 좋겠네요

이벤트에서 전달되지 않으므로 $id를 제거합니다.

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

laravel echo를 사용한 적이 있는 경우 Resources / assets / js / bootstrap . js point if u u u u if if if if if if if if if if if if if if if if if if if if if if if if if if

창 안쪽에 다음 줄을 추가하세요.

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

localhost에서 작업하는 경우 .env 파일이 올바르게 설정되어 있는지 확인합니다.

설정해 보다

APP_URL=http://localhost
DB_HOST=localhost

실행하다

php artisan config:cache

이게 도움이 되길 바라

언급URL : https://stackoverflow.com/questions/48615342/laravel-echo-500-error-broadcasting-auth

반응형