Laravel事件广播未被接收laravel-echo-server

时间:2019-10-22 18:37:50

标签: javascript laravel redis socket.io laravel-echo

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

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

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

1 个答案:

答案 0 :(得分:0)

在浏览器中,您需要像Redis一样监听Channel,也要像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 does not listen to channel and events