尝试在Pusher的帮助下触发事件,但在运行以下代码后失败。不要收到警报消息。 控制台信息: Pusher:事件已发送:{“ event”:“ pusher:ping”,“ data”:{}} Pusher:事件记录:{“ event”:“ pusher:pong”,“ data”:{}}
服务器端代码:
require __DIR__ . '/vendor/autoload.php';
$options = array(
'cluster' => 'ap2',
'encrypted' => true
);
$pusher = new Pusher\Pusher(
'******',
'******',
'****',
$options
);
$data['message'] = 'hello world';
$pusher->trigger('my-channel', 'my-event', $data);
客户端代码:
<!DOCTYPE html>
<head>
<title>Pusher Test</title>
<script src="https://js.pusher.com/4.2/pusher.min.js"></script>
<script
Pusher.logToConsole = true;
var pusher = new Pusher('*******', {
cluster: 'ap2',
encrypted: true
});
var channel = pusher.subscribe('my-channel');
channel.bind('my-event', function(data) {
alert(JSON.stringify(data));
});
</script>
</head>
<body>
<h1>Pusher Test</h1>
<p>
Try publishing an event to channel <code>my-channel</code>
with event name <code>my-event</code>.
</p>
</body>