【问题标题】:Action Cable Detect Multiple Connections/Tabs动作电缆检测多个连接/标签
【发布时间】:2019-08-13 00:32:43
【问题描述】:
我正在使用 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
【问题讨论】:
标签:
ruby-on-rails
actioncable
【解决方案1】:
注意:这可能不是您正在寻找的确切答案,但它可能会启发您或其他人寻求不同的解决方案。
我遇到了类似的问题,但后来关于在多个选项卡上显示相同的通知。
对我来说,解决方案实际上是内置在Notifications API 中,通过使用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']}`
}
}
}
});