0

我的本地主机上有一个带有 ActionCable 的可用 rails 5 应用程序,我正在尝试将它部署到 heroku。在访问聊天室所在的页面时,我可以在 chrome 的控制台中看到:

WebSocket connection to 'wss://full-beyond-9816.herokuapp.com/cable' failed: Error during WebSocket handshake: Unexpected response code: 404

我确实在 heroku 上设置了 Redis 和 Redis 插件。这是我的 cable.yml 文件的生产部分:

production: &production
  :url: redis://redistogo:4140ce7f3e7493bd1b12@porgy.redistogo.com:9463/
  :host: tarpon.redistogo.com
  :port: 10272
  :password: 12e348ac10ca002879ce7d85daf0bb0
  :inline: true
  :timeout: 1

这是我的 room.coffee 文件:

(function() {
  this.App || (this.App = {});

  App.cable = ActionCable.createConsumer();

}).call(this);

在 heroku 上设置 ActionCable 似乎很棘手,我发现的每一篇关于该主题的帖子要么使用 Phusion Passenger(我正在使用 Puma),要么使用相当旧版本的 ActionCable(我正在使用 Rails 5 的最新测试版) )。

我应该如何设置?感谢您的时间和帮助

4

1 回答 1

5

很难从你的错误中看出。我确实设法使用 Redis 插件和 Puma 在 Heroku 上部署了 ActionCable。虽然我不得不承认,由于 ActionCable 是超级新的,所以很难得到任何支持。我不得不四处玩耍才能让它发挥作用。

在你的情况下,你可能有一个 cors 问题。查看文件 config/environments/production.rb,取消注释以下部分并添加您的 heroku 应用程序名称:

  # Action Cable endpoint configuration
  config.action_cable.url = 'wss://yourappname.herokuapp.com/cable'
  config.action_cable.allowed_request_origins = [ 'https://yourappname.herokuapp.com', /http:\/\/yourappname.herokuapp.com.*/ ]

以防万一,这里是我的 cable.yml 文件:

# Action Cable uses Redis by default to administer connections, channels, and sending/receiving messages over the WebSocket.
production:
  adapter: redis
  url: add-hereyour-redis-url-from-heroku-redis-to-go-addon

development:
  adapter: async

test:
  adapter: async

另外不要忘记添加 gem Redis 以在生产中运行 Action Cable。关于您的 room.coffee 文件,仅供参考,它应该看起来像这样(意味着客户端需要设置此连接的消费者实例):

#= require action_cable
#= require_self
#= require_tree .

@App = {}
App.cable = ActionCable.createConsumer()
于 2016-05-03T15:06:16.460 回答