问题描述
问题
我可以确认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.
-
composer require pusher/pusher-php-server
-
npm install --save laravel-echo pusher-js
- 实例化了
resources/assets/js/bootstrap.js
中的Echo实例 - 初始化
env
和bootstrap.js
文件中的按键.
composer require pusher/pusher-php-server
npm install --save laravel-echo pusher-js
- instantiated the Echo instance in your
resources/assets/js/bootstrap.js
- Initialized the pusher key in
env
and inbootstrap.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');
}
}
我想念什么吗?
推荐答案
- 您必须听:
SendMessageEvent
,不带名称空间. - 收听私人频道时,您需要收听
private-SendmessageChannel
或使用Echo.private('SendmessageChannel')
- You have to listen to:
SendMessageEvent
without the namespace. - when you listen to a private channel, you need to to listen to
private-SendmessageChannel
or you useEcho.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中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!