如何检测ActionCable订阅请求是否失败

问题描述 投票:1回答:2

我最近部署了一个Rails应用程序,并且从一些用户那里听说它在他们的工作环境中不起作用。很明显,Websockets被阻止了。

我已经通过Rails文档搜索了这个但是找不到有关如何检测是否发生这种情况的任何信息。我最好的猜测是为ActionCable广播发送一个AJAX请求,并且在某个超时后没有收到该广播时,得出结论必须阻止Websockets。

这里是否有任何简单的答案,可能已经是Rails API的一部分,以确定Websocket连接?

ruby-on-rails actioncable
2个回答
0
投票

你可以使用一个rejected处理程序。当订阅被服务器拒绝时,这应该触发。

以下coffeescript示例来自official rails docs

App.cable.subscriptions.create "AppearanceChannel",
  # Called when the subscription is ready for use on the server.
  connected: ->
    // Do something

  # Called when the subscription is rejected by the server.
  rejected: ->
    // Do something

0
投票

每当动作电缆连接失败时,它都会写入浏览器控制台failed: WebSocket is closed before the connection is established

您可以利用它来了解是否存在连接错误:

def action_cable_connection_errored?
  page.driver.browser.manage.logs.get(:browser)
        .map(&:message)
        .join
        .match('failed: WebSocket is closed before the connection is established')
end
© www.soinside.com 2019 - 2024. All rights reserved.