首页 > 解决方案 > 动作电缆检测多个连接/标签

问题描述

我正在使用 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-railsactioncable

解决方案


注意:这可能不是您正在寻找的确切答案,但它可能会激发您或其他人寻求不同的解决方案。

我遇到了一个类似的问题,但是关于在多个选项卡上显示相同的通知。

对我来说,解决方案实际上是内置到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']}`
            }
        }
    }
});

推荐阅读