2014-02-19 185 views
2

我使用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(); 

服务器侧发布的代码(使用PHP推杆包):

$pusher = new Pusher(PUSHER_KEY, PUSHER_SECRET, PUSHER_APP_ID); 
$pusher->trigger('toastr-channel', 'new-toast', $input); 

推杆调试控制台显示仅收到一个消息。

推动器 - 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 

回答

2

两件事应该一定要仔细看看并更新你的问题:

  1. The Pusher Debug Console - is th e事件在那里显示两次?
  2. pusher-js JavaScript logging - 事件被记录为传入两次?

这里的另一个常见问题是,有时事件可能会被绑定两次 - 因此,两个回调。但是,您的代码并不表示发生这种情况。

+0

调试控制台只显示一条消息,但日志记录显示两条消息。我已经相应地更新了我的问题 – Matanya

+1

嗯,那很尴尬。事实证明,我在代码中的不同位置调用了两次句柄 – Matanya