ruby Rails + ActionCable + Passenger + AWS Elasticache(Redis):如何修复“WebSocket在建立连接之前关闭,”在生产环境中?

ggazkfy8  于 2023-08-04  发布在  Ruby
关注(0)|答案(1)|浏览(154)

我有一些麻烦工作我的行动电缆在生产环境。开发环境工作正常。

**要解决的错误:**WebSocket连接到'wss://myapp.com/cable'失败:WebSocket在建立连接之前关闭。

我在Chrome的控制台上多次收到此错误。

堆叠

  • Ruby 2.3
  • Rails 5.0.7.2
  • 简体中文
  • 乘客5.0.30
  • Redis 5.0.3(在AWS Elasticache上)
    nginx.conf
server {
  listen 443 ssl;
  server_name myapp.com *myapp.com;
  root /home/ubuntu/myapp/current/public;
  ssl_certificate ...;
  ssl_certificate_key ...;
  passenger_enabled on;
  passenger_min_instances 1;
  ...

  location /cable {
    passenger_app_group_name my_app_action_cable;
    passenger_force_max_concurrent_requests_per_process 0;
  }
}

字符串

cable.yml

production:
  adapter: redis
  url: redis://myapp.grzdsm.0001.euw1.cache.amazonaws.com:6379

production.rb

config.action_cable.url = "wss://myapp.com/cable"
config.action_cable.allowed_request_origins = ["https://myapp.com", "myapp.com"]

routes.rb

mount ActionCable.server => '/cable'

notification_channel.rb

class NotificationChannel < ApplicationCable::Channel

  def subscribed
    stream_from "notification"
  end

  def unsubscribed
  end

end

connection.rb

module ApplicationCable
  class Connection < ActionCable::Connection::Base
  end
end

channel.rb

module ApplicationCable
  class Channel < ActionCable::Channel::Base
  end
end

cable.js

//= require action_cable
//= require_self
//= require_tree ./channels

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

  App.cable = ActionCable.createConsumer();

}).call(this);

通知.coffee

App.notification = App.cable.subscriptions.create "NotificationChannel",
  connected: ->
    console.log("connected");

  disconnected: ->
    console.log("disconnected");

  received: (data) ->
    console.log("received");


我的日志显示成功的WebSocket连接

I, [2019-05-06T11:00:57.688540 #4986]  INFO -- : Finished "/cable/" [WebSocket] for 144.85.191.180 at 2019-05-06 11:00:57 +0200
I, [2019-05-06T11:01:09.799045 #4986]  INFO -- : Started GET "/cable/" [WebSocket] for 144.85.191.180 at 2019-05-06 11:01:09 +0200
I, [2019-05-06T11:01:09.799154 #4986]  INFO -- : Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: upgrade, HTTP_UPGRADE: websocket)
I, [2019-05-06T11:01:09.803010 #4986]  INFO -- : Registered connection (Z2lkOi8veXBqL1VzZXIvMjkx)
I, [2019-05-06T11:01:20.541988 #4986]  INFO -- : Finished "/cable/" [WebSocket] for 144.85.191.180 at 2019-05-06 11:01:20 +0200
I, [2019-05-06T11:01:21.784567 #4986]  INFO -- : Started GET "/cable/" [WebSocket] for 144.85.191.180 at 2019-05-06 11:01:21 +0200
I, [2019-05-06T11:01:21.784672 #4986]  INFO -- : Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: upgrade, HTTP_UPGRADE: websocket)
I, [2019-05-06T11:01:21.788734 #4986]  INFO -- : Registered connection (Z2lkOi8veXBqL1VzZXIvMjkx)
I, [2019-05-06T11:01:43.768741 #4986]  INFO -- : Finished "/cable/" [WebSocket] for 144.85.191.180 at 2019-05-06 11:01:43 +0200
I, [2019-05-06T11:01:44.784226 #4986]  INFO -- : Started GET "/cable/" [WebSocket] for 144.85.191.180 at 2019-05-06 11:01:44 +0200
I, [2019-05-06T11:01:44.784333 #4986]  INFO -- : Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: upgrade, HTTP_UPGRADE: websocket)
I, [2019-05-06T11:01:44.788154 #4986]  INFO -- : Registered connection (Z2lkOi8veXBqL1VzZXIvMjkx)


Redis监视器显示订阅和取消订阅

1557133304.788584 [0 172.30.1.177:59731] "subscribe" "action_cable/Z2lkOi8veXBqL1VzZXIvMjkx"
1557133326.654308 [0 172.30.1.177:59731] "unsubscribe" "action_cable/Z2lkOi8veXBqL1VzZXIvMjkx"
1557133327.910010 [0 172.30.1.177:59731] "subscribe" "action_cable/Z2lkOi8veXBqL1VzZXIvMjkx"
1557133349.255254 [0 172.30.1.177:59731] "unsubscribe" "action_cable/Z2lkOi8veXBqL1VzZXIvMjkx"
1557133351.183697 [0 172.30.1.177:59731] "subscribe" "action_cable/Z2lkOi8veXBqL1VzZXIvMjkx"
1557133356.974099 [0 172.30.1.177:59731] "unsubscribe" "action_cable/Z2lkOi8veXBqL1VzZXIvMjkx"
1557133357.707420 [0 172.30.1.177:59731] "subscribe" "action_cable/Z2lkOi8veXBqL1VzZXIvMjkx"
1557133371.886670 [0 172.30.1.177:59731] "unsubscribe" "action_cable/Z2lkOi8veXBqL1VzZXIvMjkx"


但我仍然得到这个错误:WebSocket连接到“wss://myapp.com/cable”失败:WebSocket在建立连接之前关闭。
任何线索都将不胜感激,谢谢!

8ehkhllq

8ehkhllq1#

您可能需要在config/environments/production.rb文件中将“wss://...”更改为“ws://...”

相关问题