ruby-on-rails - 动作电缆检测多个连接/标签
问题描述
我正在使用 Action Cable,想知道您将如何检测是否为同一用户打开了多个选项卡或连接,并在必要时将其删除。我有一个下载任务执行多次,我只希望它执行一次。
class ListsChannel < ApplicationCable::Channel
def subscribed
stream_from "channel"
end
# # TODO code to detect multiple streams and delete them if tabs.
# def unsubscribed
# end
end
解决方案
注意:这可能不是您正在寻找的确切答案,但它可能会激发您或其他人寻求不同的解决方案。
我遇到了一个类似的问题,但是关于在多个选项卡上显示相同的通知。
对我来说,解决方案实际上是内置到Notifications API中,通过使用 a tag
,它只会在多个选项卡上显示一次通知:
let notification = new Notification('Hey!', {
body : 'So nice to hear from you',
tag : 'greeting-notify',
icon : 'https://example.org/my_funny_icon.png'
});
也许对您而言,解决方案可能是使用此 API 将您的下载链接推送到:
Notification.requestPermission().then(function (result) {})
import consumer from "./consumer"
consumer.subscriptions.create("ReportNotificationChannel", {
connected() {},
disconnected() {},
received(data) {
if (Notification.permission === 'granted') {
let title = `Your file is ready!`
let options = { body: 'Click here to start downloading.', tag: data['filename']} // Assuming filename is something unique
let notification = new Notification(title, options)
notification.onclick = function(event){
window.location.href = `/downloads/${data['filename']}`
}
}
}
});
推荐阅读
- powershell - 查找第一个可用文件以将日志输出到
- c++ - 用于更改参数和返回值类型的功能对象的包装器
- python - 如何获得函数/lambda 的准确表示?
- javascript - 从 Uint8Array 创建的 Blob 大小给出了无意义的值
- android - Glide和recyclerview,内存太大
- excel - 如果我想在进入工作簿 1 后“复制/粘贴”工作簿 2 中的数据,是否需要使用 VBA?
- c - C 循环中断子句
- bash - sed 让我发疯
- javascript - Javascript - 按条件使用链式函数的不同部分
- vue.js - 如何使用 vuexfire 进行查询