推动器接收数据,但在回声回调功能(Laravel)中未接收数据

发布于 2025-01-25 21:14:07 字数 2190 浏览 0 评论 0原文

我正在使用Laravel和Pusher进行应用内通知。我所做的是在推动器中创建帐户,然后进行这样的混音。

Bootstrap.js

import Echo from 'laravel-echo';

window.Pusher = require('pusher-js');
window.Pusher.logToConsole = true;
window.Echo = new Echo({
    broadcaster: 'pusher',
    key: process.env.MIX_PUSHER_APP_KEY,
    cluster: process.env.MIX_PUSHER_APP_CLUSTER,
    forceTLS: true
});

Events/comment.php

<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class Comment implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;
    public $username;

    public $message;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($username)
    {
        //
        $this->username = $username;
        $this->message  = "{$username} liked your status";
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        // return new PrivateChannel('channel-name');
        return new Channel('comments-channal');
    }
}

script.js

$(function() {
            
            const Http = window.axios;
            const Echo = window.Echo;
           
            let channal = Echo.channel('comments-channel');
            channal.listen('Comment', function(data) {
                alert(data);
                console.log(data)
            })
        })

在控制器中

Comment::dispatch('fatima');

,然后我在Pusher调试控制台中收到了API消息,但是在我的控制台中,我得到了此

Pusher : Event recd : {"event":"pusher_internal:subscription_succeeded","data":{},"channel":"comments-channel"}
Pusher : No callbacks on comments-channel for pusher:subscription_succeeded

fyi,我将.env .env .env for groadcast_driver double sheck = Pusher

如果有任何帮助,我将非常感谢。

I am using laravel and pusher for in-app notification . What I did is create account in the pusher and do the congifration like this.

bootstrap.js

import Echo from 'laravel-echo';

window.Pusher = require('pusher-js');
window.Pusher.logToConsole = true;
window.Echo = new Echo({
    broadcaster: 'pusher',
    key: process.env.MIX_PUSHER_APP_KEY,
    cluster: process.env.MIX_PUSHER_APP_CLUSTER,
    forceTLS: true
});

Events/Comment.php

<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class Comment implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;
    public $username;

    public $message;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($username)
    {
        //
        $this->username = $username;
        $this->message  = "{$username} liked your status";
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        // return new PrivateChannel('channel-name');
        return new Channel('comments-channal');
    }
}

script.js

$(function() {
            
            const Http = window.axios;
            const Echo = window.Echo;
           
            let channal = Echo.channel('comments-channel');
            channal.listen('Comment', function(data) {
                alert(data);
                console.log(data)
            })
        })

in the controller

Comment::dispatch('fatima');

Then I got the API message in Pusher debug console but in my console I got this

Pusher : Event recd : {"event":"pusher_internal:subscription_succeeded","data":{},"channel":"comments-channel"}
Pusher : No callbacks on comments-channel for pusher:subscription_succeeded

FYI I double sheck the .env for BROADCAST_DRIVER=pusher

If there is any help I will be very thankful .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

堇年纸鸢 2025-02-01 21:14:07

在您的评论广播课上,您可以使用此功能格式化数据:

 public function broadcastWith()
    {
        return ['username' => $this->username,'message'=>$this->message];
    }

To your Comment broadcast Class You could format your data using this function:

 public function broadcastWith()
    {
        return ['username' => $this->username,'message'=>$this->message];
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文