繁体   English   中英

Laravel 事件广播没有被拾取 laravel-echo-server

[英]Laravel Event Broadcast not being picked up laravel-echo-server

我正在尝试使用 laravel-echo-server (socket.io) 我遇到了一个障碍,客户端似乎没有收到任何广播事件。

一切似乎都连接得很好,socket.io 服务器报告它正在连接并授权用户但是当我广播一条消息时似乎什么也没发生。 该事件出现在 Laravel Horizon 上,但除此之外,什么也没有发生。 这是我的代码:

server.js 运行 laravel-echo-server:

require('dotenv').config();

const env = process.env;

require('laravel-echo-server').run({
    authHost: env.APP_URL,
    devMode: env.APP_DEBUG,
    database: "redis",
    databaseConfig: {
        redis: {
            host: env.REDIS_HOST_PUBLIC,
            port: env.REDIS_PORT,
        }
    }
});

我的频道在 channel.php

Broadcast::channel('message.pushed', function () {
    return true;
});

我的活动:

<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class MessagePushed implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $message;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->message = "My New Message";
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return Channel|array
     */
    public function broadcastOn()
    {
        return new PrivateChannel('message.pushed');
    }
}

我在 app.js 中的事件监听器

import Echo from 'laravel-echo';
window.io = require('socket.io-client');


if (typeof io !== 'undefined') {
    var token = $('meta[name="_token"]').attr('content')
    window.Echo = new Echo({
        auth       : {
            headers: {
                Authorization: `Bearer ${token}`,
            },
        },
        broadcaster: 'socket.io',
        host       : window.location.hostname + ':6001',
    });
}



function listenForBroadcast() {
    Echo.private('message.pushed')
    .listen('MessagePushed', (e) => {
        console.log(e)
        console.log("Listened")
    });
}

listenForBroadcast();

最后是发送消息的路由:

Route::get('/event-test', function () {
    broadcast(new App\Events\MessagePushed());
});

我不太确定我哪里出错了,或者为什么客户没有收到任何东西。

在 laravel-echo-server 中查看时,命令行正在回显:

Channel: laravel_database_private-message.pushed
Event: App\Events\MessagePushed

在您的浏览器中,您需要收听 redis 所示的频道,还需要收听 redis 所示的事件,因此更改您的 listenForBroadcast() 方法可能会有所帮助。 在代码中,频道名称从 message.pushed 更改为 laravel_database_private-message.pushed,事件名称从 MessagePushed 更改为.App\Events\MessagePushed (不要错过 App 的点前缀)

function listenForBroadcast() {        
    Echo.private('laravel_database_private-message.pushed')         
    .listen('.App\\Events\\MessagePushed', (e) => { 
        console.log(e)
        console.log("Listened")
    });
}

我根据以下链接中给出的解决方案尝试了此解决方案,它对我有用 laravel Echo 不收听频道和事件

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM