侦听回调在Pusher API Laravel 5.4中不起作用 [英] Listen callback is not working in Pusher API Laravel 5.4

查看:139
本文介绍了侦听回调在Pusher API Laravel 5.4中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

我可以确认Pusher API正在接收该消息.我在Pusher网站的调试控制台中看到了.但是侦听回调根本不起作用.

I can confirm that Pusher API is receiving the message. I saw in Debug console of Pusher website. But listen callback is not working at all.

我正在遵循 此教程在Laravel 5.4中实现Pusher

I am following this tutorial to implement Pusher in Laravel 5.4

下面是逐步完成的事情.

Below were the step by step things done.

  1. composer require pusher/pusher-php-server
  2. npm install --save laravel-echo pusher-js
  3. 实例化了resources/assets/js/bootstrap.js
  4. 中的Echo实例
  5. 初始化envbootstrap.js文件中的按键.
  1. composer require pusher/pusher-php-server
  2. npm install --save laravel-echo pusher-js
  3. instantiated the Echo instance in your resources/assets/js/bootstrap.js
  4. Initialized the pusher key in env and in bootstrap.js file.

最后,我在Blade中编写了以下代码.

Finally, I wrote below code in blade.

<script>
    window.Echo.channel('SendMessageChannel.1')
        .listen('App.Events.SendMessageEvent', (e) => {
            console.log(e);
        });
</script>

控制器代码

broadcast(new SendMessageEvent("Hi"))->toOthers();

事件代码

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

    public $Message;

    public function __construct($message)
    {
        $this->Message = $message;
    }

    public function broadcastOn()
    {
        return new PrivateChannel('SendMessageChannel.2');
    }

}

我想念什么吗?

推荐答案

  1. 您必须听:SendMessageEvent,不带名称空间.
  2. 收听私人频道时,您需要收听private-SendmessageChannel或使用Echo.private('SendmessageChannel')
  1. You have to listen to: SendMessageEvent without the namespace.
  2. when you listen to a private channel, you need to to listen to private-SendmessageChannel or you use Echo.private('SendmessageChannel')

由于我们是通过小组演讲解决了该问题,因此很难在此答案中进行详细解释.

Because we fixxed the issue via teamspeak at some parts it's difficult to explain it in this answer in full detail.

另一个问题是该事件在客户端开始收听之前就被触发了.最好的方法是使用推送器控制台进行调试并触发自定义事件.

One problem was also that the event was fired before the client started to listen to it. The best way is to debug with the pusher console and to fire custom events.

这篇关于侦听回调在Pusher API Laravel 5.4中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆