Laravel Echo + Pusherjs,回调函数不起作用?

huangapple go评论50阅读模式
英文:

Laravel Echo + Pusherjs, callback function not working?

问题

在我的Laravel 10 Breeze应用中,我正在设置一个私有的Pusher连接。我正在使用logToConsole来调试连接,我可以发送和接收数据,任何请求都能正常工作。然而,在处理回调函数内部的数据时,出现了问题。我做错了什么?

进一步解释一下,使用pusher.logToConsole是有效的。但是放置任何类型的代码,比如console.logalert、设置状态等都不起作用。

通道是正确的,因为更改它不会有太多作用。我的猜测是事件出了问题,因为将其更改为SomeRandomName既不会产生错误,也不会产生不同的结果。

其中一个日志似乎是一个可能导致问题的错误:
["No callbacks on private-admin_users for pusher:subscription_succeeded"]
我不知道为什么会出现这个错误。

这是我的事件类:

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

    public $message;

    /**
     * 创建一个新的事件实例。
     */
    public function __construct($message)
    {
        $this->message = $message;
    }

    /**
     * 获取事件应该广播到的频道。
     *
     * @return array|Channel|Channel[]
     */
    public function broadcastOn()
    {
        return new PrivateChannel('admin_users');
    }
}

这是我的bootstrap.js

window.Pusher = Pusher;

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: import.meta.env.VITE_PUSHER_APP_KEY,
    cluster: import.meta.env.VITE_PUSHER_APP_CLUSTER ?? 'mt1',
    forceTLS: true,
});

这是我在Laravel控制器中发送请求的地方:

public function activateWeek($id, Request $request) {
    $week = Week::find($id);
    $week->active = true;
    $week->save();

    broadcast(new DeliveryListUpdate([
        'event_type' => 'remove_user',
        'user_id' => $request->user_id,
    ]))->toOthers();
}

我尝试过使用监听器和事件服务提供程序,但我不确定它是否是解决我的问题的方法。无论如何,似乎对我没有起作用。

提前感谢您的帮助!

英文:

in my Laravel 10 Breeze app i'm setting up a private Pusher connection. I am using logtoconsole to debug the connection, and any request i put in works, and i can send and receive data. However, when it comes to processing the data inside a callback function, it's no use. What am i doing wrong?

To further explain, using pusher.logToConsole works. However putting any kind of code like, console.log, alert, setting state, etc. Does not work.

The channel is correct as changing it won't do much. My guess is there is something wrong with the event, as changing it to SomeRandomName doesn't yield an error nor diffirent results.

Pusher.logToConsole = true;
window.Echo.private('admin_users').listen('DeliveryListUpdated', (e) => {

        console.log(1111)
        console.log(e)
    });

One of the logs does seem to be a error that could be a cause:
["No callbacks on private-admin_users for pusher:subscription_succeeded"]
I don't know why i am getting this.

This is my event class:


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

    public $message;

    /**
     * Create a new event instance.
     */
    public function __construct(
        $message
        //public User $user
    )
    {
        $this->message = $message;
    }

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

This is my bootstrap.js:


window.Pusher = Pusher;

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: import.meta.env.VITE_PUSHER_APP_KEY,
    cluster: import.meta.env.VITE_PUSHER_APP_CLUSTER ?? 'mt1',
    // wsHost: import.meta.env.VITE_PUSHER_HOST ? import.meta.env.VITE_PUSHER_HOST : `ws-${import.meta.env.VITE_PUSHER_APP_CLUSTER}.pusher.com`,
    // wsPort: import.meta.env.VITE_PUSHER_PORT ?? 80,
    // wssPort: import.meta.env.VITE_PUSHER_PORT ?? 443,
    forceTLS: true,
    // forceTLS: (import.meta.env.VITE_PUSHER_SCHEME ?? 'https') === 'https',
    // enabledTransports: ['ws', 'wss'],
});

.. and this is where i send a request (in my Laravel controller):


    public function activateWeek($id, Request $request) {
        $week = Week::find($id);
        $week->active = true;
        $week->save();

        broadcast(new DeliveryListUpdate(
            ['event_type' => 'remove_user',
                'user_id' => $request->user_id,
            ]))->toOthers();
    }

I tried listeners and event service providers, but i'm not sure if it's the solution for my problem. Did not seem to work for me anyhow.

Thanks in advance!

答案1

得分: 1

在你的事件内添加 `broadcastAs()` 方法:

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

并且在监听广播时,在其前面加上一个点 (.):

window.Echo.private('admin_users').listen('.message', (e) => {
console.log(1111)
console.log(e)
});

这有助于我解决了一个类似的问题。
英文:

Add the broadcastAs() method inside your event:

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

And when listening to your event on the broadcast, put a dot (.) in before of it:

window.Echo.private('admin_users').listen('.message', (e) => {
    console.log(1111)
    console.log(e)
});

It helped me fix a similar problem I was having.

huangapple
  • 本文由 发表于 2023年6月19日 20:21:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76506585.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定