离开页面时收到ActionCable Server Broadcast

时间:2019-01-19 12:01:55

标签: ruby-on-rails ruby actioncable

我注意到我的ActionCable消息仅在我离开应该在其上看到它们的页面导航时才出现...这是一个测试控制器:

class TestController < ApplicationController

  before_action :run_before_action
  after_action :run_after_action

  def index
    ActionCable.server.broadcast("test_channel", action: 'index')
  end

  private

  def run_before_action
    ActionCable.server.broadcast("test_channel", action: 'before_action')
  end

  def run_after_action
    ActionCable.server.broadcast("test_channel", action: 'after_action')
  end

end

这是我的频道:

class TestChannel < ApplicationCable::Channel
  def subscribed
    stream_from "test_channel"
  end

  def unsubscribed
    # Any cleanup needed when channel is unsubscribed
  end
end

最后是要控制台的JS,注销连接和数据:

App.notifications = App.cable.subscriptions.create("TestChannel", {
  connected: function () {
      // Called when the subscription is ready for use on the server
      console.log('connected');
  },
  disconnected: function () {
      // Called when the subscription has been terminated by the server
      console.log('disconnected');
  },
  received: function (data) {
    // Called when there's incoming data on the websocket for this channel
    console.log('received');
    console.log(data);
    $('body').append(data.action);
  }
});

我将Redis用于开发中的适配器,并删除了Turbolinks。

但是我看到的是,在页面加载时,我仅看到已登录的控制台登录。然后第二次刷新页面,或单击链接导航,看到控制台中收到的内容,并将数据附加到页面上。

为什么在休假中发生这种情况?我什至做到了,使ActionCable广播在动作之前,之中和之后进行,以查看是否有所不同……但事实并非如此。有什么想法吗?

更新:有趣的是,如果我打开一个新标签页并打开该页面,我会在原始标签页中看到它们...然后刷新,它将像以前一样再次显示它们。

0 个答案:

没有答案