Heroku:Rails 5 WebSocket 连接失败

Posted

技术标签:

【中文标题】Heroku:Rails 5 WebSocket 连接失败【英文标题】:Heroku: Rails 5 WebSocket connection failed 【发布时间】:2016-01-19 17:22:55 【问题描述】:

我正在尝试在 heroku 上的 rails 5 应用程序上部署 reactjs,这是我的 Procfile

web: bundle exec puma -C config/puma.rb

这是我的 puma.rb 文件

workers Integer(ENV['WEB_CONCURRENCY'] || 2)
threads_count = Integer(ENV['MAX_THREADS'] || 5)
threads threads_count, threads_count

preload_app!

rackup      DefaultRackup
port        ENV['PORT']     || 5000
environment ENV['RACK_ENV'] || 'development'

on_worker_boot do
  ActiveRecord::Base.establish_connection
end

这是我的 cable.yml 文件

production:
:url: redis://redistogo:f9e8821e7e25167ae7023516bd3fce13@tarpon.redistogo.com:11879/
:host: tarpon.redistogo.com
:port: 11879
:password: xxxx
:inline: true
:timeout: 1

development:
  url: redis://localhost:6379/2

test:
  url: redis://localhost:6379/3

我尝试使用

从 reactjs 连接到套接字
App.cable = ActionCable.createConsumer("wss://adham-chatty.heroku.com");

但我在 chrome 控制台上出现此错误,并且套接字不工作:\

WebSocket connection to 'wss://adham-chatty.heroku.com/' failed: WebSocket opening handshake was canceled

这是我的 Heroku 日志

016-01-19T17:17:22.405051+00:00 heroku[web.1]: State changed from up to starting
    2016-01-19T17:17:25.031404+00:00 heroku[web.1]: Starting process with command `bundle exec puma -C config/puma.rb`
    2016-01-19T17:17:26.521202+00:00 heroku[web.1]: Stopping all processes with SIGTERM
    2016-01-19T17:17:27.266810+00:00 app[web.1]: [3] Puma starting in cluster mode...
    2016-01-19T17:17:27.266821+00:00 app[web.1]: [3] * Version 2.15.3 (ruby 2.2.2-p95), codename: Autumn Arbor Airbrush
    2016-01-19T17:17:27.266822+00:00 app[web.1]: [3] * Min threads: 5, max threads: 5
    2016-01-19T17:17:27.266822+00:00 app[web.1]: [3] * Environment: production
    2016-01-19T17:17:27.266823+00:00 app[web.1]: [3] * Process workers: 2
    2016-01-19T17:17:27.266823+00:00 app[web.1]: [3] * Preloading application
    2016-01-19T17:17:27.721211+00:00 app[web.1]: [3] - Gracefully shutting down workers...
    2016-01-19T17:17:28.101087+00:00 app[web.1]: [3] === puma shutdown: 2016-01-19 17:17:28 +0000 ===
    2016-01-19T17:17:28.101095+00:00 app[web.1]: [3] - Goodbye!
    2016-01-19T17:17:28.974002+00:00 app[web.1]: [3] * Listening on tcp://0.0.0.0:18058
    2016-01-19T17:17:28.974157+00:00 app[web.1]: [3] Use Ctrl-C to stop
    2016-01-19T17:17:28.978252+00:00 app[web.1]: [3] - Worker 0 (pid: 6) booted, phase: 0
    2016-01-19T17:17:28.994863+00:00 heroku[web.1]: Process exited with status 0
    2016-01-19T17:17:28.979382+00:00 app[web.1]: [3] - Worker 1 (pid: 10) booted, phase: 0
    2016-01-19T17:17:29.346266+00:00 heroku[web.1]: State changed from starting to up

我该怎么办?

【问题讨论】:

您应该在 cable.yml 中的密码字段中有这个 f9e8821e7e25167ae7023516bd3fce13 【参考方案1】:

你需要设置 config.action_cable.url & config.action_cable.allowed_request_origins & mount ActionCable.server 在你的路线中

【讨论】:

以上是关于Heroku:Rails 5 WebSocket 连接失败的主要内容,如果未能解决你的问题,请参考以下文章

Heroku Websocket到自定义域

带有 master.key 的 Rails 5.2 - Heroku 部署

在 Heroku 上部署 ActionCable (Rails 5 beta4)

Rails 5.2 和 webpacker 3.4.3:部署到 Heroku 时资产未编译

Rails 5 + Webpacker 应用程序作为空白页面部署到 Heroku,没有失败

Rails 5 - ActionCable - 无法升级到 WebSocket