如果由于网络差而断开连接,无法检测服务器端的客户端激活状态 - 动作电缆

发布于 2025-01-17 16:11:40 字数 731 浏览 4 评论 0原文

我们面临着可采取行动的问题。问题是,客户端由于互联网问题而断开连接,但该状态未传达到服务器(断开事件未调用)

由于客户端不活动,以下方法失败,

ActionCable.server.broadcast "project_123_channel", {
      "request" => "show_title"
    }

Ruby 版本:2.5.3p105 Rails 版本:5.2.3 肌动蛋白电缆版本:5.2.3

断开回调:

App.live_data = App.cable.subscriptions.create({ channel: "ProjectDataChannel", project_id: project_id }, {
    collection: function() {
    },
    connected: function() {
     console.log("Live data connected");
    },
    disconnected: function() {
      console.log("disconnected");
      return this.perform('unfollow');
    }

但是此断开回调不起作用。

有没有办法从服务器端检测客户端的活动状态?或者如果客户端由于互联网原因断开连接,则有任何回调来跟踪断开连接状态

We facing issues with actioncable. The issue is , Client is getting disconnected due to internet issue but that status not communicated to the server(disconnect event not calling)

The below method is failing due to inactive client,

ActionCable.server.broadcast "project_123_channel", {
      "request" => "show_title"
    }

Ruby version:2.5.3p105
Rails version:5.2.3
actin cable version: 5.2.3

disconnect callback:

App.live_data = App.cable.subscriptions.create({ channel: "ProjectDataChannel", project_id: project_id }, {
    collection: function() {
    },
    connected: function() {
     console.log("Live data connected");
    },
    disconnected: function() {
      console.log("disconnected");
      return this.perform('unfollow');
    }

But this disconnect callback not working.

Is there any way to detect the client active status from server side? or there any callback to track the disconnect status if the client is disconnect due to internet kind of things

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苍景流年 2025-01-24 16:11:40

这是一个已知的问题。

这是Vlad Dyachenko的博客文章,描述了问题和解决方案...

https://wowinter13.medium.com/actioncable andling-client-connection-ernection-errors-erors-oner-server-side-93e74178d03

这是帖子中的代码解决方案...

module ApplicationCable
  class Channel < ActionCable::Channel::Base
    after_subscribe :connection_monitor
    CONNECTION_TIMEOUT = 10.seconds
    CONNECTION_PING_INTERVAL = 5.seconds
    periodically every: CONNECTION_PING_INTERVAL do
      @driver&.ping
      if Time.now - @_last_request_at > @_timeout
        connection.disconnect
      end
    end
    def connection_monitor
      @_last_request_at ||= Time.now
      @_timeout = CONNECTION_TIMEOUT
      @driver = connection.instance_variable_get('@websocket').possible?&.instance_variable_get('@driver')
      @driver.on(:pong) { @_last_request_at = Time.now }
    end
  end
end

@dbugger的建议很好

This is a known issue.

Here is a blog post from Vlad Dyachenko that describes the problem and a solution...

https://wowinter13.medium.com/actioncable-handling-client-connection-errors-on-server-side-93ea74178d03

This is the code solution from the post...

module ApplicationCable
  class Channel < ActionCable::Channel::Base
    after_subscribe :connection_monitor
    CONNECTION_TIMEOUT = 10.seconds
    CONNECTION_PING_INTERVAL = 5.seconds
    periodically every: CONNECTION_PING_INTERVAL do
      @driver&.ping
      if Time.now - @_last_request_at > @_timeout
        connection.disconnect
      end
    end
    def connection_monitor
      @_last_request_at ||= Time.now
      @_timeout = CONNECTION_TIMEOUT
      @driver = connection.instance_variable_get('@websocket').possible?&.instance_variable_get('@driver')
      @driver.on(:pong) { @_last_request_at = Time.now }
    end
  end
end

It's worked fine as suggestion given by @dbugger

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文