【问题标题】:Laravel pusher/echo getting broadcast error 500 and 403Laravel pusher/echo 收到广播错误 500 和 403
【发布时间】:2023-03-10 15:09:02
【问题描述】:

您好,我正在尝试为我的应用程序制作聊天系统,但在制作推送器和回显时遇到问题。当我打开两个人之间的聊天时,它不会实时更新,我必须刷新页面才能获取更新的内容,例如消息等。在控制台中我收到这 2 个错误

POST http://localhost/broadcasting/auth 403(禁止)

POST http://localhost/broadcasting/auth 500(内部服务器错误)

bootstrap.js

import Echo from 'laravel-echo';

window.Pusher = require('pusher-js');

window.Echo = new Echo({
     broadcaster: 'pusher',
     key: process.env.MIX_PUSHER_APP_KEY,
     cluster: process.env.MIX_PUSHER_APP_CLUSTER,
     encrypted: true,
});

ChatBroadcast.php

class ChatBroadcast implements ShouldBroadcastNow
{
    use Dispatchable, InteractsWithSockets, SerializesModels;


    public $ticket;

    /**
     * Create a new event instance.
     *
     * @param $ticket
     */
    public function __construct(Ticket $ticket)
    {
        $this->ticket = $ticket;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new PrivateChannel('tickets_channel.' . $this->ticket->id);
    }

    public function broadcastWith(){
        $messages = Message::all()->where('ticket_id','=',$this->ticket->id)->sortBy('created_at');

        return [
            'ticket' => $this->ticket,
            'messages' => $messages
        ];
    }
}

channels.php

use App\Ticket;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Broadcast;

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

Broadcast::channel('tickets_channel.{ticketID}', function ($user, $ticketID) {
    Auth::check();
    $ticket = Ticket::all()->where('id', '=', $ticketID)->first();
    return $ticketID == $ticket->id;
});

Vue组件

mounted() {
            Echo.private('tickets_channel.${ticketID}')
                .listen('ChatBroadcast', (e) => {
                    console.log(e);
                    this.messagesMutable = e.messages;
                    this.state = e.ticket.isOpened;
                });
        },

这是我的 500 错误日志

[2020-09-08 09:25:24] local.ERROR: Invalid channel name private-tickets_channel.${ticketID} {"userId":11,"exception":"[object] (Pusher\\PusherException(code: 0): Invalid channel name private-tickets_channel.${ticketID} at /Users/miroslavjesensky/Documents/Blog/vendor/pusher/pusher-php-server/src/Pusher.php:282)

-编辑-

我已通过将 VueComponent 代码更改为此修复了 500 错误

mounted() {
            let id = this.ticket.id;

            Echo.private('tickets_channel' + id)
                .listen('ChatBroadcast', (e) => {
                    this.messagesMutable = e.messages;
                    this.state = e.ticket.isOpened;
                });
        },

【问题讨论】:

  • 我猜是因为字符串,如果您使用大括号变量,请像这样使用它,例如:“tickets_channel.{$ticketID}”

标签: laravel echo pusher


【解决方案1】:

原来问题出在我与第一个一起使用的另一个 VueComponent 上。修复回声的聆听部分后,一切正常

【讨论】:

    猜你喜欢
    • 2017-04-15
    • 2018-07-14
    • 2019-09-14
    • 2023-03-24
    • 2021-08-05
    • 2020-09-15
    • 1970-01-01
    • 2018-09-22
    • 2021-07-02
    相关资源
    最近更新 更多