【问题标题】:Pusher event is triggered twicePusher 事件被触发两次
【发布时间】:2014-02-19 08:06:09
【问题描述】:

每当管理员发送通知时,我正在使用 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包):

 $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 

【问题讨论】:

    标签: javascript php events publish-subscribe pusher


    【解决方案1】:

    您绝对应该看看两件事并适当地更新您的问题:

    1. The Pusher Debug Console - 事件在那里显示了两次?
    2. pusher-js JavaScript logging - 事件是否被记录为 incoming 两次?

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

    【讨论】:

    • 调试控制台只显示一条消息,但日志显示两条。我已经相应地更新了我的问题
    • 嗯,这很尴尬。事实证明,我在代码的不同位置调用了两次句柄
    猜你喜欢
    • 2023-03-26
    • 2017-04-17
    • 2011-03-05
    • 1970-01-01
    • 1970-01-01
    • 2020-01-31
    • 1970-01-01
    • 1970-01-01
    • 2013-09-11
    相关资源
    最近更新 更多