最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do you remove an actioncable channel subscription in Rails 5 with App.cable.subscriptions.remove? - Stack Overf

programmeradmin6浏览0评论

To create subscriptions I run:

  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
      });
    }
  });

But because I want the client to never be subscribed to more than one channel, what can I run each time before this to remove all subscriptions the client has?

I tried to do something super hacky like:

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

But I'm sure it didn't work because there are many other ponents that go into a subscription/unsubscription.

App.cable.subscriptions.remove requires a subscription argument, but what do I pass in?

Thanks!

To create subscriptions I run:

  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
      });
    }
  });

But because I want the client to never be subscribed to more than one channel, what can I run each time before this to remove all subscriptions the client has?

I tried to do something super hacky like:

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

But I'm sure it didn't work because there are many other ponents that go into a subscription/unsubscription.

App.cable.subscriptions.remove requires a subscription argument, but what do I pass in?

Thanks!

Share Improve this question asked May 1, 2016 at 7:21 LaserLaser 5,4394 gold badges37 silver badges50 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 9

A bit of a late answer, but considering you've already declared

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

to remove, something like

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

should suffice.

Running this before each subscription creation will ensure there is only ever a maximum of one subscription per client.

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

It looks like you would need to create a reference to your subscription on create

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

and then remove it like so

App.cable.subscriptions.remove(mySubscription)

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论