2017-06-14 171 views
0

我想在rails 5中使用ActionCable,但我不知道爲什麼它不工作。Rails 5 Actioncable - 失敗:在WebSocket握手期間出錯:意外的響應代碼:502

在瀏覽器控制檯我得到:

WebSocket connection to 'ws://my.website/cable' failed: WebSocket is closed before the connection is established.

WebSocket connection to 'ws://my.website/cable' failed: Error during WebSocket handshake: Unexpected response code: 502

Uncaught TypeError: Cannot read property 'subscriptions' of undefined

我已經安裝了客運+ Nginx的

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

須藤的/ usr/bin中/乘客配置驗證安裝:

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

須藤/ usr/sbin目錄/乘客內存統計:

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 

我有整合作用電纜與客運+ Nginx的由於本指南:Integrating Action Cable with Passenger + Nginx

因此,作爲應用程序運行良好的nginx +乘客在線。

我的Rails應用程序的名稱是 「最後」

/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; 

} 

     } 

最終/配置/ routes.rb中: mount ActionCable.server => '/cable'

此外,我加入<%= action_cable_meta_tag %>內部application.html.erb

最終/配置/ cable.yml:

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

少數/ 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 

環境/ production.rb:

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

在導軌控制檯 - ActionCable.server.config:

=> #<ActionCable::Server::Configuration:0x007f9ec2027598 @log_tags=[], @connection_class=#<Proc:[email protected]/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"]> 

資產/ Javascript角/ 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); 

通道/ 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 /錯誤。日誌:

[ 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' 

回答

0

2問題是這裏: 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);` 
  • 我在nginx.conf註釋此行:#include /etc/nginx/sites-enabled/*; Becaouse配置文件的網站啓用/默認重疊nginx.conf
  • 我希望這個問題的答案可以節省時間某人你的配置。

    相關問題