简体   繁体   English

Rails 5 Actioncable-失败:WebSocket握手期间出错:意外的响应代码:502

[英]Rails 5 Actioncable - failed: Error during WebSocket handshake: Unexpected response code: 502

I am trying to use ActionCable in rails 5 but i dont know why its not working. 我试图在Rails 5中使用ActionCable,但我不知道为什么它不起作用。

In browser console i receive: 在浏览器控制台中,我收到:

WebSocket connection to 'ws://my.website/cable' failed: WebSocket is closed before the connection is established. WebSocket与“ ws://my.website/cable”的连接失败:WebSocket在建立连接之前已关闭。

WebSocket connection to 'ws://my.website/cable' failed: Error during WebSocket handshake: Unexpected response code: 502 WebSocket与“ ws://my.website/cable”的连接失败:WebSocket握手期间出错:意外的响应代码:502

Uncaught TypeError: Cannot read property 'subscriptions' of undefined 未捕获的TypeError:无法读取未定义的属性“订阅”

I have Installed Passenger + Nginx 我已经安装了Passenger + Nginx

guide: https://www.phusionpassenger.com/library/install/nginx/install/oss/xenial/ 指南: https//www.phusionpassenger.com/library/install/nginx/install/oss/xenial/

sudo /usr/bin/passenger-config validate-install: 须藤/ usr / bin / passenger-config validate-install:

* Checking whether this Passenger install is in PATH... ✓  
* Checking whether there are no other Passenger installations... ✓

sudo /usr/sbin/passenger-memory-stats: 须藤/ usr / sbin / passenger-memory-stats:

Version: 5.1.4
Date   : 2017-06-14 10:05:30 +0000

------- Apache processes --------

### Processes: 0
### Total private dirty RSS: 0.00 MB


--------- Nginx processes ----------
PID   PPID  VMSize    Private  Name
------------------------------------
8176  1     142.8 MB  0.5 MB   nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
8179  8176  142.8 MB  0.7 MB   nginx: worker process
### Processes: 2
### Total private dirty RSS: 1.22 MB


---- Passenger processes -----
PID   VMSize    Private  Name
------------------------------
8156  421.3 MB  1.1 MB   Passenger watchdog
8159  636.5 MB  1.7 MB   Passenger core
8165  429.6 MB  1.2 MB   Passenger ust-router
### Processes: 3
### Total private dirty RSS: 4.07 MB

I have Integrated Action Cable with Passenger + Nginx due to this guide: Integrating Action Cable with Passenger + Nginx 由于此指南,我将动作电缆与Passenger + Nginx 集成在一起将动作电缆与Passenger + Nginx集成在一起

So nginx + passenger running well as app is online. 因此,nginx +乘客可以很好地运行在线应用程序。

My rails application name is "final" 我的Rails应用程序名称为“最终”

/opt/nginx/config/nginx.config: (inside http {...} ) /opt/nginx/config/nginx.config :(在http {...}内部)

        user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
    worker_connections 768;
    # multi_accept on;
}

http {
    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;
server {
    listen       80;
    server_name  my.website www.my.website;
    rails_env production;


root /var/www/final/public;
passenger_enabled on;
passenger_app_root   /var/www/final;        







location /cable {
    passenger_app_group_name final_websocket;
    passenger_force_max_concurrent_requests_per_process 0;

}

      }

final/config/routes.rb: mount ActionCable.server => '/cable' final / config / routes.rb: mount ActionCable.server => '/cable'

Also i added <%= action_cable_meta_tag %> inside application.html.erb 此外,我加入<%= action_cable_meta_tag %> application.html.erb

final/config/cable.yml: 最后/配置/ cable.yml:

production: &production
  url: redis://localhost:6379

less log/production.log: 较少的log / production.log:

I, [2017-06-14T11:55:49.841674 #7666]  INFO -- : [7b1f4cd9-5fbf-44f5-ba77-e4d7dc40c8f1]   Rendering account/seting.html.erb within layouts/application
I, [2017-06-14T11:55:49.842952 #7666]  INFO -- : [7b1f4cd9-5fbf-44f5-ba77-e4d7dc40c8f1]   Rendered account/_header.html.erb (1.0ms) [cache miss]
I, [2017-06-14T11:55:50.469493 #7666]  INFO -- : [7b1f4cd9-5fbf-44f5-ba77-e4d7dc40c8f1]   Rendered account/_footer.html.erb (0.5ms) [cache miss]
I, [2017-06-14T11:55:50.469629 #7666]  INFO -- : [7b1f4cd9-5fbf-44f5-ba77-e4d7dc40c8f1]   Rendered account/seting.html.erb within layouts/application (627.9ms)
I, [2017-06-14T11:55:50.470586 #7666]  INFO -- : [7b1f4cd9-5fbf-44f5-ba77-e4d7dc40c8f1] Completed 200 OK in 633ms (Views: 629.2ms | ActiveRecord: 0.4ms)
I, [2017-06-14T11:55:51.024845 #7709]  INFO -- : [2c674f25-f747-4cbc-9ad8-b6f517bd5b72] Started GET "/cable" for 213.7.71.70 at 2017-06-14 11:55:51 +0000
I, [2017-06-14T11:55:51.062689 #7709]  INFO -- : [2c674f25-f747-4cbc-9ad8-b6f517bd5b72] Started GET "/cable/" [WebSocket] for 213.7.71.70 at 2017-06-14 11:55:51 +0000
I, [2017-06-14T11:55:51.062890 #7709]  INFO -- : [2c674f25-f747-4cbc-9ad8-b6f517bd5b72] Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: upgrade, HTTP_UPGRADE: websocket)
D, [2017-06-14T11:55:51.081839 #7709] DEBUG -- :   ESC[1mESC[35m (0.5ms)ESC[0m  ESC[1mESC[35mSET NAMES utf8,  @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), log/production.log

enviroment/production.rb: 环境/ production.rb:

   config.action_cable.url = "/cable"  
   config.action_cable.allowed_request_origins = [ 'http://my-server-ip-address', /http:\/\/example.*/ ]

In rails console- ActionCable.server.config: 在Rails控制台中-ActionCable.server.config:

=> #<ActionCable::Server::Configuration:0x007f9ec2027598 @log_tags=[], @connection_class=#<Proc:0x007f9ec20224a8@/usr/local/lib/ruby/gems/2.4.0/gems/actioncable-5.1.1/lib/action_ca
ble/engine.rb:35 (lambda)>, @worker_pool_size=4, @disable_request_forgery_protection=false, @allow_same_origin_as_host=true, @logger=#<ActiveSupport::Logger:0x007f9ec3242c28 @level
=0, @progname=nil, @default_formatter=#<Logger::Formatter:0x007f9ec3242b60 @datetime_format=nil>, @formatter=#<ActiveSupport::Logger::SimpleFormatter:0x007f9ec3242a48 @datetime_for
mat=nil, @thread_key="activesupport_tagged_logging_tags:70159927743780">, @logdev=#<Logger::LogDevice:0x007f9ec3242b10 @shift_period_suffix=nil, @shift_size=nil, @shift_age=nil, @f
ilename=nil, @dev=#<IO:/dev/pts/2>, @mon_owner=nil, @mon_count=0, @mon_mutex=#<Thread::Mutex:0x007f9ec3242a98>>, @local_levels=#<Concurrent::Map:0x007f9ec32429d0 entries=0 default_
proc=nil>>, @cable={"adapter"=>"redis", "url"=>"redis://localhost:6379"}, @mount_path="/cable", @url="/cable", @allowed_request_origins=["http://my.website"]>

assets/javascripts/cable.js: 资产/ Java脚本/ cable.js:

// Action Cable provides the framework to deal with WebSockets in Rails.
// You can generate new channels where WebSocket features live using the `rails generate channel` command.
//
//= require action_cable
//= require_self
//= require_tree ./channels

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

  App.cable = ActionCable.createConsumer('/cable');

}).call(this);

channels/application_cable/connection.rb: 通道/ application_cable / connection.rb:

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


    identified_by :current_user

    def connect
      self.current_user = find_verified_user
    end

    private
      def find_verified_user
        if verified_user = User.find_by(id: cookies.signed[:user_id])
          verified_user
        else
          reject_unauthorized_connection
        end
      end
  end
end

var/log/nginx/error.log: 无功/日志/ nginx的/ error.log中:

[ 2017-06-14 12:46:00.0110 14157/7f41c30e9700 age/Cor/App/Poo/AnalyticsCollection.cpp:102 ]: Process (pid=15020, group=final_websocket) no longer exists! Detaching it from the pool.
[ 2017-06-14 12:46:00.0132 14157/7f41c30e9700 age/Cor/CoreMain.cpp:907 ]: Checking whether to disconnect long-running connections for process 15020, application final_websocket
App 15037 stdout: 
App 14181 stderr: /usr/local/lib/ruby/gems/2.4.0/gems/redis-3.3.3/lib/redis/connection/ruby.rb:229:in `getaddrinfo': getaddrinfo: Name or service not known (SocketError)
App 14181 stderr:   from /usr/local/lib/ruby/gems/2.4.0/gems/redis-3.3.3/lib/redis/connection/ruby.rb:229:in `connect'
App 14181 stderr:   from /usr/local/lib/ruby/gems/2.4.0/gems/redis-3.3.3/lib/redis/connection/ruby.rb:313:in `connect'
App 14181 stderr:   from /usr/local/lib/ruby/gems/2.4.0/gems/redis-3.3.3/lib/redis/client.rb:336:in `establish_connection'
App 14181 stderr:   from /usr/local/lib/ruby/gems/2.4.0/gems/redis-3.3.3/lib/redis/client.rb:101:in `block in connect'
App 14181 stderr:   from /usr/local/lib/ruby/gems/2.4.0/gems/redis-3.3.3/lib/redis/client.rb:293:in `with_reconnect'
App 14181 stderr:   from /usr/local/lib/ruby/gems/2.4.0/gems/redis-3.3.3/lib/redis/client.rb:100:in `connect'
App 14181 stderr:   from /usr/local/lib/ruby/gems/2.4.0/gems/redis-3.3.3/lib/redis/client.rb:276:in `with_socket_timeout'
App 14181 stderr:   from /usr/local/lib/ruby/gems/2.4.0/gems/redis-3.3.3/lib/redis/client.rb:133:in `call_loop'
App 14181 stderr:   from /usr/local/lib/ruby/gems/2.4.0/gems/redis-3.3.3/lib/redis/subscribe.rb:43:in `subscription'
App 14181 stderr:   from /usr/local/lib/ruby/gems/2.4.0/gems/redis-3.3.3/lib/redis/subscribe.rb:12:in `subscribe'
App 14181 stderr:   from /usr/local/lib/ruby/gems/2.4.0/gems/redis-3.3.3/lib/redis.rb:2765:in `_subscription'
App 14181 stderr:   from /usr/local/lib/ruby/gems/2.4.0/gems/redis-3.3.3/lib/redis.rb:2143:in `block in subscribe'
App 14181 stderr:   from /usr/local/lib/ruby/gems/2.4.0/gems/redis-3.3.3/lib/redis.rb:58:in `block in synchronize'
App 14181 stderr:   from /usr/local/lib/ruby/2.4.0/monitor.rb:214:in `mon_synchronize'
App 14181 stderr:   from /usr/local/lib/ruby/gems/2.4.0/gems/redis-3.3.3/lib/redis.rb:58:in `synchronize'
App 14181 stderr:   from /usr/local/lib/ruby/gems/2.4.0/gems/redis-3.3.3/lib/redis.rb:2142:in `subscribe'
App 14181 stderr:   from /usr/local/lib/ruby/gems/2.4.0/gems/actioncable-5.1.1/lib/action_cable/subscription_adapter/redis.rb:77:in `block in listen'
App 14181 stderr:   from /usr/local/lib/ruby/gems/2.4.0/gems/redis-3.3.3/lib/redis/client.rb:293:in `with_reconnect'
App 14181 stderr:   from /usr/local/lib/ruby/gems/2.4.0/gems/redis-3.3.3/lib/redis.rb:64:in `block in with_reconnect'
App 14181 stderr:   from /usr/local/lib/ruby/gems/2.4.0/gems/redis-3.3.3/lib/redis.rb:58:in `block in synchronize'
App 14181 stderr:   from /usr/local/lib/ruby/2.4.0/monitor.rb:214:in `mon_synchronize'
App 14181 stderr:   from /usr/local/lib/ruby/gems/2.4.0/gems/redis-3.3.3/lib/redis.rb:58:in `synchronize'
App 14181 stderr:   from /usr/local/lib/ruby/gems/2.4.0/gems/redis-3.3.3/lib/redis.rb:63:in `with_reconnect'
App 14181 stderr:   from /usr/local/lib/ruby/gems/2.4.0/gems/redis-3.3.3/lib/redis.rb:70:in `without_reconnect'
App 14181 stderr:   from /usr/local/lib/ruby/gems/2.4.0/gems/actioncable-5.1.1/lib/action_cable/subscription_adapter/redis.rb:74:in `listen'
App 14181 stderr:   from /usr/local/lib/ruby/gems/2.4.0/gems/actioncable-5.1.1/lib/action_cable/subscription_adapter/redis.rb:148:in `block in ensure_listener_running'

two problem was here: 1. cable.js should be this 这里有两个问题:1. cable.js应该是这个

:`// app/assets/javascripts/cable.js
//= require action_cable
//= require_self
//= require_tree ./channels
(function() {
  this.App || (this.App = {});

  App.cable = ActionCable.createConsumer();
}).call(this);`
  1. I comment this line in nginx.conf: #include /etc/nginx/sites-enabled/*; 我在nginx.conf中注释了这一行: #include /etc/nginx/sites-enabled/*; Becaouse config in file sites-enabled/default overlaps your config in nginx.conf 因为启用了文件站点/默认文件中的配置与您在nginx.conf中的配置重叠

I hope this question and answer can save someones time. 我希望这个问题和答案可以节省别人的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ActionCable Rails 5(Passenger) - 失败:WebSocket握手期间出错:意外响应代码:404 - ActionCable Rails 5 (Passenger) - failed: Error during WebSocket handshake: Unexpected response code: 404 WebSocket 握手期间的 Rails 5 错误:意外响应代码:502 - Rails 5 Error during WebSocket handshake: Unexpected response code: 502 ActionCable - WebSocket 握手期间出错:意外响应代码:404 - ActionCable - Error during WebSocket handshake: Unexpected response code: 404 WebSocket握手期间出错:意外的响应代码:502 - Error during WebSocket handshake: Unexpected response code: 502 在websocket握手期间Rails ActionCable错误 - Rails ActionCable error during websocket handshake 在 WebSocket 握手期间 Rails 6 Production ActionCable 错误 - Rails 6 Production ActionCable Error during WebSocket handshake 带Rails(Puma)的Websockets-WebSocket握手期间出错:意外的响应代码:200 - Websockets with Rails(Puma) - Error during WebSocket handshake: Unexpected response code: 200 WebSocket握手期间出错:意外的响应代码:503 - Error during WebSocket handshake: Unexpected response code: 503 Actioncable Nginx和Puma WebSocket握手:意外响应 - Actioncable Nginx and Puma WebSocket handshake: Unexpected response Rails 5 - ActionCable - 无法升级到WebSocket - Rails 5 - ActionCable - Failed to upgrade to WebSocket
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM