【问题标题】:ActionCable not executing subscribed in javascriptActionCable 未在 javascript 中执行订阅
【发布时间】:2017-05-09 20:16:06
【问题描述】:

我正在使用 ActionCable,这是非常基本的设置。

我在客户端有这个:

App.chat = App.cable.subscriptions.create "ChatChannel",
  subscribed: ->
    console.log 'subscribed'
  test: ->
    @perform("test")

在服务器上

class ChatChannel < AC..::Channel
  def subscribed
    Rails.logger.info "This is never printed in the logs"
  end
  def test
    Rails.logger.info "This is never printed in the logs"
  end
end

在 JS 控制台上我可以看到:

[ActionCable] Opening WebSocket, current state is null, subprotocols: actioncable-v1-json,actioncable-unsupported 1494360634092
[ActionCable] ConnectionMonitor started. pollInterval = 3000 ms 1494360634096
[ActionCable] WebSocket onopen event, using 'actioncable-v1-json' subprotocol 1494360634733
[ActionCable] ConnectionMonitor recorded connect 1494360634734

在我看到的服务器日志上:

Started GET "/cable" for 127.0.0.1 at 2017-05-09 15:10:34 -0500
Started GET "/cable/" [WebSocket] for 127.0.0.1 at 09/05/2017 03:10 PM
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
Registered connection (Z2lkOi8vcHJvcGhvL1VzZXIvMg)

如果我从服务器发送广播消息,客户端永远不会收到它。如果我尝试从客户端执行操作,它会返回 false 并且控制台上不会出现任何内容。

网络标签显示:

Request URL:ws://0.0.0.0:3000/cable
Request Method:GET
Status Code:101 Switching Protocols (with a green status icon)

我没有使用redis,用rails server启动服务器

gem 'rails', '~> 5.0.2'
gem 'puma', '~> 3.0'

【问题讨论】:

  • 刚刚发现我包含pace.js 一个javascript 加载动画脚本。似乎这是导致问题的原因
  • 如果您使用的是 Pace.js,则可以将trackWebSockets 选项设置为false,更多信息请点击此处:github.com/HubSpot/pace/issues/411

标签: javascript ruby-on-rails actioncable


【解决方案1】:

试试看。

App.chat = App.cable.subscriptions.create "ChatChannel",
  # subscribed: ->
  #   console.log 'subscribed'
  # test: ->
  #   @perform("test")

  connected: ->
    @perform("test")

【讨论】:

    猜你喜欢
    • 2017-03-28
    • 1970-01-01
    • 2019-03-19
    • 2020-12-12
    • 1970-01-01
    • 2017-11-13
    • 1970-01-01
    • 2018-06-14
    • 2018-11-09
    相关资源
    最近更新 更多