如何使用App.cable.subscriptions.remove删除Rails5中的actioncable频道订阅
How do you remove an actioncable channel subscription in Rails 5 with App.cable.subscriptions.remove?
要创建订阅,我运行:
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需要一个订阅参数,但我要传递什么?
谢谢!
答案有点晚,但考虑到您已经声明了
App.room = App.cable.subscriptions.create(...)
删除,类似的东西
if (App.room) App.cable.subscriptions.remove(App.room);
应该足够了。
在每次创建订阅之前运行此操作将确保每个客户端最多只能有一个订阅。
if (App.cable.subscriptions['subscriptions'].length > 1) {
App.cable.subscriptions.remove(App.cable.subscriptions['subscriptions'][1])
};
看起来您需要在创建时创建对订阅的引用
let mySubscription = App.cable.subscriptions.create({
channel: "MyChannel"
},
{
connected: () => { console.log('connected') },
disconnected: () => {},
received: (data) => {}
});
然后像一样将其移除
App.cable.subscriptions.remove(mySubscription)
相关文章:
- 无法从 jQuery RSS Feed 中的 localStorage 动态替换类
- 可以't让我的if语句处理js中的html表单输入
- 引用对象中的通用值
- 使用JS如何动态更改显示的html文件中的文本背景颜色
- 用程序搜索JQuery数据表中的文本
- 无法导出函数expressjs/requestjs中的变量
- 函数参数中的数据与指定变量之间的任何性能差异
- 不能从angular2中的子组件指定父组件中的数组
- Dojo不解析自定义小部件的模板html中的小部件声明性
- 如何在angularJS中编辑时,如果DB中的值为true,则设置复选框,如果值为false,则取消选中复选框
- 正在验证8个真/假复选框或复选框中的2个
- Javascript-如何读取json文件中的列并将其保存在Javascript数组中
- 如何更改bigquery API中的计费层选项
- 将输入字段中的文本提交到我的数据库,同时将其添加到我的列表中
- 将jsp文件下拉列表中的选定项分配给一个java变量(比如String selection)
- 使用Dnamics CRM 2011中的JavaScript读取子网格的所有记录,而不考虑活动页面
- 拨打'父亲'函数形式a'儿童'ReactJS中的组件
- 我如何找到一个句子中的所有空格并替换忽略它们
- zurb基础中的固定宽度立柱3
- 如何使用App.cable.subscriptions.remove删除Rails5中的actioncable频道订阅