简体   繁体   English

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

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

I'm attempting to use laravel-echo-server (socket.io) I'm getting a roadblock where the client-side doesn't appear to be getting any broadcasted events.我正在尝试使用 laravel-echo-server (socket.io) 我遇到了一个障碍,客户端似乎没有收到任何广播事件。

Everything seems to be connected fine and socket.io server is reporting that it's connecting and authorising the user however when I broadcast a message nothing seems to happen.一切似乎都连接得很好,socket.io 服务器报告它正在连接并授权用户但是当我广播一条消息时似乎什么也没发生。 The event is appearing on Laravel Horizon but otherwise, nothing happens.该事件出现在 Laravel Horizon 上,但除此之外,什么也没有发生。 Here is my code:这是我的代码:

server.js to run the laravel-echo-server: 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,
        }
    }
});

My channel in channel.php我的频道在 channel.php

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

My event:我的活动:

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

My event listener within app.js我在 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();

Lastly the route sending the message:最后是发送消息的路由:

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

I am not quite sure where i have gone wrong or why nothing is being picked up by the client.我不太确定我哪里出错了,或者为什么客户没有收到任何东西。

While looking in laravel-echo-server the command line is echoing:在 laravel-echo-server 中查看时,命令行正在回显:

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

In you browser you need to listen on Channel as shown by redis and also listen to the event as shown by redis, so changing your listenForBroadcast() method may help.在您的浏览器中,您需要收听 redis 所示的频道,还需要收听 redis 所示的事件,因此更改您的 listenForBroadcast() 方法可能会有所帮助。 In the code, Channel name changed from message.pushed to laravel_database_private-message.pushed and Event name changed from MessagePushed to.App\Events\MessagePushed ( Do not miss dot prefix to App )在代码中,频道名称从 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")
    });
}

I tried this solution based on the solution given in the following link and it worked for me laravel Echo does not listen to channel and events我根据以下链接中给出的解决方案尝试了此解决方案,它对我有用 laravel Echo 不收听频道和事件

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

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