我建立了一个Laravel广播系统,计划将其作为一个实时聊天应用程序来实现。当检查客户端页面时,控制台日志显示:
Pusher :事件记录:
{"event":"App\\Events\\Event","data":{"message":"Greetings from PrinceLuo!"},"channel":"testChannel"}Pusher :App\Events\Events不回调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('App\Events\Event', 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 App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
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 \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
// return new PrivateChannel('channel-name');
// return new PrivateChannel('testChannel');
return new \Illuminate\Broadcasting\Channel('testChannel');
}
}这是我的路线:
Route::get('test_event',function(){
event(new Event('Greetings from PrinceLuo!'));
});
Route::get('test_listen',function(){
return view('listenBroadcast');
});发布于 2018-07-13 17:58:35
对于那些感兴趣的人,我张贴了我为解决这个问题所做的工作:
注意,Push Logger显示[App\Events\Event]以转义反斜杠的函数。因此,在JavaScript中,我们必须对其进行相同的修改:
channel.bind('App\\Events\\Event', function(data){});很简单但很重要。
发布于 2020-06-20 07:37:42
这就是我所做的
channel.bind('pusher:subscription_succeeded', function(members) {
// alert('successfully subscribed!');
});
channel.bind("App\\Events\\NewComment", function(data) {
console.log(data);
});https://stackoverflow.com/questions/51303435
复制相似问题