【问题标题】:Detect unsubscribe from Action Cable on page change在页面更改时检测从 Action Cable 退订
【发布时间】:2016-06-11 16:21:38
【问题描述】:

我正在 Rails 中构建一个聊天室类型系统,我需要检测用户何时离开服务器上的频道(即关闭页面),以便更新活动用户列表。我在 GitHub 上搜索了文档和示例,但似乎找不到答案。

我过去使用过 SignalR,它会触发断开连接事件,但我在 Rails 5 中找不到等效的(如果确实存在的话)。

我可以在频道类中看到带有取消订阅方法的示例,但无论我导航到新页面还是完全关闭浏览器,这都不会触发。

编辑:不确定这是否有帮助,但我没有使用 turbolinks。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-5 actioncable


    【解决方案1】:

    这是 Rails 5 的 beta 2 的一个问题。我已经在最新版本 - beta 4 中尝试过它,现在它按预期触发了。

    这里是github问题供参考:https://github.com/rails/rails/pull/23715

    【讨论】:

      【解决方案2】:

      试试这个

      App.chatroom.disconnected()
      => yourChannel stopped streaming from chatroom #rails terminal
      
      App.chatroom = App.cable.subscriptions.create({channel: 
           'yourChannel'}, {
      
           disconnected: function () {
             App.cable.subscriptions.remove(this)
             this.perform('unsubscribed') #in the channel have a method 'unsubscibed' to update users
           },
       )}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-02-24
        • 2018-02-08
        • 1970-01-01
        • 2013-12-20
        • 1970-01-01
        • 2020-05-19
        • 1970-01-01
        • 2011-11-16
        相关资源
        最近更新 更多