websocket Laravel Echo在React中没有接收到Pusher事件

mm5n2pyu  于 11个月前  发布在  React
关注(0)|答案(2)|浏览(97)

编辑:忘记添加bootstrap.js
我试图使用Pusher和Laravel-Echo设置websockets,但我遇到了一个奇怪的bug。我可以看到我的事件在Pusher调试器控制台上被触发,但我没有在我的前端接收事件。下面是我的代码来设置Pusher:

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,
});

字符串

广播.php

'pusher' => [
            'driver' => 'pusher',
            'key' => env('PUSHER_APP_KEY'),
            'secret' => env('PUSHER_APP_SECRET'),
            'app_id' => env('PUSHER_APP_ID'),
            'options' => [
                'cluster' => env('PUSHER_APP_CLUSTER'),
                'encrypted' => true
            ],
        ],

app.php

/*
         * Application Service Providers...
         */
        App\Providers\AppServiceProvider::class,
        App\Providers\AuthServiceProvider::class,
        App\Providers\BroadcastServiceProvider::class,
        App\Providers\EventServiceProvider::class,
        App\Providers\RouteServiceProvider::class,
        Tymon\JWTAuth\Providers\LaravelServiceProvider::class,

OptionChange.php(这是我正在监听的事件)

<?php

namespace App\Events;

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

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

    public $instance;

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

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

    // return this info in Pusher
    public function broadcastWith() {
        return [
            'id' => $this->instance->id,
            'answer' => $this->instance->answer,
            'question_number' => $this->instance->question_number
        ];
    }
}


React Code(我正在监听我的事件)

// set up channel listener for live updates
    componentDidMount() {
        const { started, studentId } = this.state

        if (!started) {
            window.Echo.channel('studentStartedTest' + studentId)
                .listen('StartTest', (didStart) => {
                    console.log('startTest')
                    if (didStart) { 
                        this.setState({ started: true })
                    }
                })
        } else {
            // window.Echo.leaveChannel('studentStartedTest' + studentId)
            window.Echo.channel('itemInstance' + this.props.id)
            .listen('OptionChange', (instance) => {
                console.log('change')
                this.setState({ answer: instance.answer })
            })

        }
    }


正如你所看到的,每当我的事件触发时,我都会尝试console.log。不幸的是,console.log永远不会运行。
我也试过将implements ShouldBroadcast更改为implements ShouldBroadcastNow,但没有任何改变

pw9qyyiw

pw9qyyiw1#

您的laravel事件将使用完整的类名和命名空间进行广播。要获得一个更干净的事件名称,以匹配您实际侦听的内容,请将broadcastAs方法添加到OptionChange Event类

public function broadcastAs()
    {
        return 'StartTest';
    }

字符串

wlzqhblo

wlzqhblo2#

在我的例子中,我没有在Laravel文档中注意到这一点
如果使用broadcastAs方法自定义广播名称,则应确保使用前导.字符注册侦听器
所以如果你的broadcastAs()函数看起来像这样

public function broadcastAs()
    {
        return 'StartTest';
    }

字符串
您的JS listen将需要

window.Echo.channel('studentStartedTest' + studentId)
                .listen('.StartTest', (didStart) => {
                    console.log('startTest')
                    if (didStart) { 
                        this.setState({ started: true })
                    }
                })

相关问题