Laravel Echo 未收到来自事件的信息

问题描述

基本上,当我重新加载主页时,数据会在 console.log 中出现一段时间,然后很快消失。

//Bootstrap.js

window._ = require('lodash');

/**
 * We'll load the axios HTTP library which allows us to easily issue requests
 * to our Laravel back-end. This library automatically handles sending the
 * CSRF token as a header based on the value of the "XSRF" token cookie.
 */

window.axios = require('axios');

window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';

/**
 * Echo exposes an expressive API for subscribing to channels and listening
 * for events that are broadcast by Laravel. Echo and event broadcasting
 * allows your team to easily build robust real-time web applications.
 */

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,wsHost: window.location.hostname,wsPort: 6001,forceTLS: false,disableStats: true,encrypted: false
});

window
    .Echo
    .channel('DemoChannel')
    .listen('WebSocketDemoEvent',e => {
        console.log(e)
    })

//WebSocketDemoEvent.PHP

<?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\Foundation\Events\dispatchable;
use Illuminate\Queue\SerializesModels;

class WebSocketDemoEvent implements Shouldbroadcast
{
    use dispatchable,InteractsWithSockets,SerializesModels;

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

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new Channel('DemoChannel');
    }
}

我不知道为什么在加载 dom 时它一直出现。然后在满载后迅速消失。

是否与 pusher 有任何关系,因为我使用的是 Beyondcode 的 laravel-websockets 包,它声明它不必配置 pusher 凭据。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)