我想知道如何从服务器端触发事件,而不必每次想要向Pusher触发事件时刷新页面。
这是我的代码:
通知/ notifications.php
<?php
require(dirname(__FILE__).'/vendor/autoload.php');
include(dirname(__FILE__).'../../notifications.php');
$pusher = new Pusher('APP_KEY', 'APP_SECRET', 'APP_ID');
$notificationsrow = $numorows + $numrows;
$data['message'] = $notificationsrow;
$pusher->trigger('notifications', 'notify', $data);
echo $data['message'];
?>
的index.php
<div class="badge" id="noteindex"></div>
<script src="/v2/app/dashboard/js/jquery-3.1.1.min.js"></script>
<script src="//js.pusher.com/3.1/pusher.min.js" type="text/javascript"></script>
<script>
document.addEventListener('DOMContentLoaded', function(){
var pusher = new Pusher('APP_KEY');
var notificationsChannel = pusher.subscribe('notifications');
var eventname = 'notify';
notificationsChannel.bind(eventname, function(notification){
var message = notification.message;
$('#noteindex').text(message);
});
}, false);
</script>
谢谢你提前:)。