【问题标题】:Event on private pusher channel received only after refreshing the page?仅在刷新页面后才收到私人推送频道上的事件?
【发布时间】:2013-09-10 23:43:47
【问题描述】:

我正在创建一个私人频道,长时间运行的操作的结果将在该频道上发送。我的浏览器控制台中有以下内容:

Pusher : State changed : initialized -> connecting
rt_cli...?body=1 (line 5)
Pusher : Connecting : {"transport":"ws","url":"ws://ws-eu.pusher.com:80/app/xxxxxxxxxxxxx?protocol=6&client=js&version=2.1.2&flash=false"}
rt_cli...?body=1 (line 5)
Pusher : State changed : connecting -> connected
rt_cli...?body=1 (line 5)
POST http://localhost:3000/pusher/auth

pusher.min.js (line 111)
Connected to Pusher!
common.js?body=1 (line 56)
Pusher : Event sent : {"event":"pusher:subscribe","data":{"auth":"179bfeac81d16e6ae36f:81fa74052368dac232d70146d2f2d343f3d4ce5befc34bce43673a9e6ceaa38a","channel":"private-user-channel-11"}}
rt_cli...?body=1 (line 5)
Pusher : Event recd : {"event":"pusher_internal:subscription_succeeded","data":{},"channel":"private-user-channel-11"}
rt_cli...?body=1 (line 5)
Pusher : No callbacks on private-user-channel-11 for pusher:subscription_succeeded

客户端连接如下所示:

  $(document).ready(function() {
      Pusher.host = 'ws-eu.pusher.com';
      Pusher.sockjs_host = 'sockjs-eu.pusher.com';

      pusher = new Pusher("#{ ENV["PUSHER_APP_KEY"] }", {cluster: "eu"});
      var channel = pusher.subscribe("private-user-channel-11");

      // Some useful debug msgs
      pusher.connection.bind('connecting', function() {
        clog("connecting to pusher");
      });
      pusher.connection.bind('connected', function() {
        clog('Connected to Pusher!');
      });
      pusher.connection.bind('failed', function() {
        clog('Connection to Pusher failed :(');
      });
      channel.bind('subscription_error', function(status) {
        clog('Pusher subscription_error');
      });

      channel.bind("ninja", function(e) {
        console.log(e);
      });
    });

从我的工作进程中,我触发了这样的事件:

Pusher.trigger s_channel_name, "ninja", {"message" => "whatever"}

问题是,我的页面加载,脚本执行,但在浏览器中我没有收到消息。但是,如果我刷新页面(并在浏览器控制台中保留日志,我会看到收到了消息)。另外,我注意到如果我在两个不同的浏览器中登录,并在这两个浏览器中保持页面打开,未刷新的浏览器将实时接收事件。这使我相信触发的浏览器(即使是间接请求,也被排除在消息的收件人之外。有没有办法向连接到私人频道的所有用户广播?另外,如果我触发从 Rails 控制台手动操作事件,它会立即出现(无需刷新浏览器)。

编辑:看起来我的工作人员在浏览器加载页面之前完成了他的工作,并且消息丢失了。很抱歉认为问题出在其他地方。

【问题讨论】:

  • 可能值得提交您的解决方案作为您自己问题的答案并将其标记为答案。这样看来,这仍然不是一个悬而未决的问题。

标签: ruby-on-rails ruby websocket pusher


【解决方案1】:

问题是工作人员在页面完成加载之前触发了消息,我无法对该事件做出反应。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-09
    • 2019-11-05
    • 2021-10-20
    • 1970-01-01
    • 1970-01-01
    • 2018-02-26
    • 2020-12-14
    • 2014-11-16
    相关资源
    最近更新 更多