Pusher 没有捕获触发的事件,因为 TLS 从 2018 年 9 月 6 日开始发生变化。它在控制台上没有给出一个错误,当我打开包含 pusher 客户端 JavaScript 的页面时,它显示通道已连接到pusher 服务器,但是当我触发它时,它没有响应,也没有给出任何错误。只是它根本不调用绑定事件回调。这是我包含的缩小的 pusher.js。
<script src="https://js.pusher.com/4.3/pusher.min.js"></script>
这是客户端JS绑定的事件代码。
var notificationsWrapper = $('.dropdown-notifications');
var notificationsToggle = notificationsWrapper.find('a[data-toggle]');
var notificationsCountElem = notificationsToggle.find('i[data-count]');
var notificationsCount = parseInt(notificationsCountElem.data('count'));
var notifications = notificationsWrapper.find('ul.dropdown-menu');
if (notificationsCount <= 0) {
notificationsWrapper.hide();
}
// Enable pusher logging - don't include this in production
Pusher.logToConsole = true;
var pusher = new Pusher('AppKey', {
cluster: 'ap2',
forceTLS: true
});
// Subscribe to the channel we specified in our Laravel Event
var channel = pusher.subscribe('subscribed');
channel.bind('pusher:subscription_succeeded', function(members) {
console.log('subscribed successful') ;
});
channel.bind('pusher:subscription_error', function(status) {
console.log('subscribed error: '+ status) ;
});
channel.bind('theEvent', function(data) {
var existingNotifications = notifications.html();
var avatar = Math.floor(Math.random() * (71 - 20 + 1)) + 20;
var newNotificationHtml = `
<li class="notification active">
<div class="media">
<div class="media-left">
<div class="media-object">
<img src="https://api.adorable.io/avatars/71/`+avatar+`.png" class="img-circle" alt="50x50" style="width: 50px; height: 50px;">
</div>
</div>
<div class="media-body">
<strong class="notification-title">`+data.type+`</strong>
<!--p class="notification-desc">Extra description can go here</p-->
<div class="notification-meta">
<small class="timestamp">`+data.date_time+`</small>
</div>
</div>
</div>
</li>
`;
notifications.html(newNotificationHtml + existingNotifications);
notificationsCount += 1;
notificationsCountElem.attr('data-count', notificationsCount);
notificationsWrapper.find('.notif-count').text(notificationsCount);
notificationsWrapper.show();
$.playSound('/notification/notification.mp3') ;
});
这是我在 laravel 控制器中调用触发器的地方。
$pusher = HomeController::getPusher() ;
$pusher->trigger('subscribed', 'theEvent', $callBack);
这两行在一个函数中,如果在控制器响应视图之前调用并执行特定路由,则调用该函数。Pusher 实例是使用 Home Controller 的引用创建的。这些路由功能和推送功能都在 Home Controller 中。
public function getPusher() {
$options = array(
'cluster' => 'ap2',
'useTLS' => true
);
$pusher = new Pusher(
'AppKey',
'AppSecret',
'AppId',
$options
);
return $pusher ;
}