要创建订阅我运行:
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
});
}
});
但是因为我希望客户端永远不会订阅多个频道,所以我每次都可以运行什么来删除客户端的所有订阅?
我试着做一些超级黑客的事情:
App.cable.subscriptions['subscriptions'] = [App.cable.subscriptions['subscriptions'][1, 0]]
但我确定它不起作用,因为还有许多其他组件进入订阅/取消订阅。
App.cable.subscriptions.remove需要订阅参数,但我要传递什么?
谢谢!
答案 0 :(得分:6)
有点迟到的回答,但考虑到你已经宣布了
App.room = App.cable.subscriptions.create(...)
删除,类似
if (App.room) App.cable.subscriptions.remove(App.room);
应该足够了。
答案 1 :(得分:3)
在每次订阅创建之前运行此命令将确保每个客户端最多只能有一个订阅。
if (App.cable.subscriptions['subscriptions'].length > 1) {
App.cable.subscriptions.remove(App.cable.subscriptions['subscriptions'][1])
};
答案 2 :(得分:0)
您似乎需要在创建
上创建对订阅的引用let mySubscription = App.cable.subscriptions.create({
channel: "MyChannel"
},
{
connected: () => { console.log('connected') },
disconnected: () => {},
received: (data) => {}
});
然后将其删除
App.cable.subscriptions.remove(mySubscription)