我正在尝试支持我的Rails应用程序的WebSocket接口。我在Rails 4上,没有计划立即转移到Rails 5。我在nginx后面使用Phusion Passenger 5.0.30。
我想使用em-websocket(+ eventmachine)来构建这种支持。我必须使用哪些乘客配置设置?
server {
listen 80 default_server;
server_name ergo;
root /home/user/www/testapp/public;
passenger_enabled on;
passenger_app_env development;
passenger_max_preloader_idle_time 0;
location /notify {
proxy_pass http://ergo:3004/notify;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
passenger_force_max_concurrent_requests_per_process 0;
}
}
使用上面的配置和以下用于启动config/initializers/event_machine_init.rb
中的EM循环的代码,我无法使websocket服务器工作。是否需要进行其他配置更改?
require 'eventmachine'
require 'em-websocket'
module EventMachineInit
def self.start
if defined?(PhusionPassenger)
PhusionPassenger.on_event(:starting_worker_process) do |forked|
# for passenger, we need to avoid orphaned threads
if forked && EM.reactor_running?
EM.stop
end
Thread.new do
puts "Starting EventMachine thread"
EM.run do
EM::WebSocket.run(host: "0.0.0.0", port: 3004) do |ws|
ws.onopen do |handshake|
puts "WebSocket connection open"
ws.send "Hello Client, you connected to #{handshake.path}"
end
ws.onclose { puts "WebSocket connection closed" }
ws.onmessage do |msg|
puts "Received message: #{msg}"
ws.send "Pong: #{msg}"
end
end
end
end
die_gracefully_on_signal
end
end
end
def self.die_gracefully_on_signal
Signal.trap("INT") { EM.stop }
Signal.trap("TERM") { EM.stop }
end
end
编辑:我能够通过@Priyank Gupta在问题评论中提出的更改来完成上述工作。变化是:
proxy_pass http://ergo:3004/notify;
成为:
proxy_pass http://localhost:3004/notify;
我还补充说:
proxy_redirect off;