我正在使用Pusher(pusher.com)在管理员发送通知时触发所有登录客户端的通知。
出于某种原因,事件是两次拍摄,虽然我只触发一次。
客户端订阅代码:
var handleToastrListener = function() {
var pusher = new Pusher("913284db62a0cc237db4");
var channel = pusher.subscribe('toastr-channel');
channel.bind('new-toast', function(data) {
toastr.options = data.options;
var $toast = toastr[data.scf](data.msg, data.title);
return true;
});
}
handleToastrListener();
服务器端发布代码(使用pusher包的PHP):
$pusher = new Pusher(PUSHER_KEY, PUSHER_SECRET, PUSHER_APP_ID);
$pusher->trigger('toastr-channel', 'new-toast', $input );
Pusher调试控制台显示只收到一条消息。
然而,pusher-js Javascript日志记录显示两条消息:
Pusher : Event recd : {"event":"new-toast","data":{"options":{"positionClass":"toast-top-right","onclick":"","showDuration":"1000","hideDuration":"1000","timeOut":"5000","extendedTimeOut":"1000","showEasing":"swing","hideEasing":"linear","showMethod":"fadeIn","hideMethod":"fadeOut"},"title":"Toastr Notifications","msg":"Gnome & Growl type non-blocking notifications","scf":"success"},"channel":"toastr-channel"} app.js:143
Pusher : Event recd : {"event":"new-toast","data":{"options":{"positionClass":"toast-top-right","onclick":"","showDuration":"1000","hideDuration":"1000","timeOut":"5000","extendedTimeOut":"1000","showEasing":"swing","hideEasing":"linear","showMethod":"fadeIn","hideMethod":"fadeOut"},"title":"Toastr Notifications","msg":"Gnome & Growl type non-blocking notifications","scf":"success"},"channel":"toastr-channel"}
进一步观察我发现订阅发生了两次,虽然我只打了一次:
Pusher : Event sent : {"event":"pusher:subscribe","data":{"channel":"toastr-channel"}} app.js:143
Pusher : Event recd : {"event":"pusher_internal:subscription_succeeded","data":{},"channel":"toastr-channel"} app.js:143
Pusher : No callbacks on toastr-channel for pusher:subscription_succeeded app.js:143
Pusher : State changed : connecting -> connected app.js:143
Pusher : Event sent : {"event":"pusher:subscribe","data":{"channel":"toastr-channel"}} app.js:143
Pusher : Event recd : {"event":"pusher_internal:subscription_succeeded","data":{},"channel":"toastr-channel"} app.js:143
Pusher : No callbacks on toastr-channel for pusher:subscription_succeeded
答案 0 :(得分:2)
你应该好好看看并妥善更新你的问题:
这里的另一个常见问题是有时事件可能被绑定两次 - 因此,两次回调。但是,您的代码并不表示会发生这种情况。