Pusher:在testChannel上没有App\Events\Event的回调



我建立了一个Laravel广播,计划将其作为实时聊天应用程序来实现。检查客户端页面时,控制台日志显示:

Pusher:事件记录:{"event":"App\Events\Event","data":{"message":"Greetings from PrinceLuo!"},"channel":"testChannel"}

Pusher:App\Events\Event 的testChannel上没有回调

它只是忽略了确实存在的回调函数。。。。。。

顺便说一句,我还没有安装npm,所以我使用了Pusher仪表板建议的简单Javascript代码,而不是Laravel建议的Vue代码。

在控制台日志和Pusher面板上,我都可以看到服务器发送的广播消息。

这是我的客户端代码:

<!DOCTYPE html>
<html>
<!DOCTYPE html>
<head>
<title>Pusher Test</title>
<script src="{{ asset('js/pusher.min.js') }}"></script>
<script>
// Enable pusher logging - don't include this in production
Pusher.logToConsole = true;
var pusher = new Pusher('****************', {
cluster: 'ap1',
encrypted: true
});
var channel = pusher.subscribe('testChannel');
channel.bind('AppEventsEvent', function(data) {
console.log(data);
});

</script>
</head>
<body>
<h1>Pusher Test</h1>
<p>
Try publishing an event to channel <code>testChannel</code>
with event name <code>Event</code>.
</p>
</body>
</html>

只需隐藏按键即可~~

我在谷歌上搜索了一些类似的案例。但没人能给我答案。有人见过这个案子,或者对这个案子有什么想法吗?


更新:

我还将我的服务器端代码发布在这里,供任何需要的人使用:

<?php
namespace AppEvents;
use IlluminateBroadcastingChannel;
use IlluminateQueueSerializesModels;
use IlluminateBroadcastingPrivateChannel;
use IlluminateBroadcastingPresenceChannel;
use IlluminateFoundationEventsDispatchable;
use IlluminateBroadcastingInteractsWithSockets;
use IlluminateContractsBroadcastingShouldBroadcast;
class Event implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $message;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct($message)
{
//
$this->message = $message;
}
/**
* Get the channels the event should broadcast on.
*
* @return IlluminateBroadcastingChannel|array
*/
public function broadcastOn()
{
//        return new PrivateChannel('channel-name');
//        return new PrivateChannel('testChannel');
return new IlluminateBroadcastingChannel('testChannel');
}
}

这是我的路线:

Route::get('test_event',function(){
event(new Event('Greetings from PrinceLuo!'));
});
Route::get('test_listen',function(){
return view('listenBroadcast');
});

对于那些对这个案例感兴趣的人,我发布了我为解决这个问题所做的事情:

请注意,Push Logger显示[App\Events\Event]以逃避反斜杠的功能。因此,在JavaScript中,我们必须对其进行相同的更改:

channel.bind('App\Events\Event', function(data){});

简单但重要。

这就是我所做的

channel.bind('pusher:subscription_succeeded', function(members) {
// alert('successfully subscribed!');
});
channel.bind("App\Events\NewComment", function(data) {
console.log(data);
});

最新更新