【问题标题】:How do you remove an actioncable channel subscription in Rails 5 with App.cable.subscriptions.remove?如何使用 App.cable.subscriptions.remove 在 Rails 5 中删除可操作的频道订阅?
【发布时间】:2016-05-01 07:21:51
【问题描述】:

创建订阅我运行:

  App.room = App.cable.subscriptions.create({
    channel: "RoomChannel",
    roomId: roomId
  }, {
    connected: function() {},
    disconnected: function() {},
    received: function(data) {
      return $('#messages').append(data['message']);
    },
    speak: function(message, roomId) {
      return this.perform('speak', {
        message: message,
        roomId: roomId
      });
    }
  });

但是因为我希望客户端永远不会订阅多个频道,所以在此之前每次我可以运行什么来删除客户端的所有订阅?

我试图做一些超级 hacky 之类的事情:

App.cable.subscriptions['subscriptions'] = [App.cable.subscriptions['subscriptions'][1, 0]]

但我确信它不起作用,因为订阅/取消订阅还有许多其他组件。

App.cable.subscriptions.remove 需要订阅参数,但我应该传入什么?

谢谢!

【问题讨论】:

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


    【解决方案1】:

    答案有点晚,但考虑到您已经声明了

    App.room = App.cable.subscriptions.create(...)

    删除,类似

    if (App.room) App.cable.subscriptions.remove(App.room);

    应该够了。

    【讨论】:

      【解决方案2】:

      在每个订阅创建之前运行此操作将确保每个客户端最多只能有一个订阅。

      if (App.cable.subscriptions['subscriptions'].length > 1) {
          App.cable.subscriptions.remove(App.cable.subscriptions['subscriptions'][1])
      };
      

      【讨论】:

        【解决方案3】:

        您似乎需要在创建时创建对您的订阅的引用

        let mySubscription = App.cable.subscriptions.create({
          channel: "MyChannel"
        },
        {
          connected: () => { console.log('connected') },
          disconnected: () => {},
          received: (data) => {}
        });
        

        然后像这样删除它

        App.cable.subscriptions.remove(mySubscription)
        

        【讨论】:

          猜你喜欢
          • 2017-02-10
          • 2014-11-29
          • 1970-01-01
          • 2021-11-15
          • 1970-01-01
          • 2013-04-22
          • 1970-01-01
          • 1970-01-01
          • 2022-01-03
          相关资源
          最近更新 更多