Ctrl + C不会杀死Sinatra + EM :: WebSocket服务器

我正在构建一个运行EM :: WebSocket服务器和Sinatra服务器的Ruby应用程序。 单独地,我相信这两个都配备了处理SIGINT。 但是,当在同一个应用程序中运行时,当我按Ctrl + C时,应用程序会继续运行。 我的假设是其中一个捕获SIGINT,阻止另一个捕获它。 不过,我不知道如何修复它。

这里的代码简而言之:

require 'thin' require 'sinatra/base' require 'em-websocket' EventMachine.run do class Web::Server < Sinatra::Base get('/') { erb :index } run!(port: 3000) end EM::WebSocket.start(port: 3001) do |ws| # connect/disconnect handlers end end 

我遇到过同样的问题。 我的关键似乎是在反应器循环中用signals: false启动Thin signals: false

  Thin::Server.start( App, '0.0.0.0', 3000, signals: false ) 

这是简单聊天服务器的完整代码:

 require 'thin' require 'sinatra/base' require 'em-websocket' class App < Sinatra::Base # threaded - False: Will take requests on the reactor thread # True: Will queue request for background thread configure do set :threaded, false end get '/' do erb :index end end EventMachine.run do # hit Control + C to stop Signal.trap("INT") { puts "Shutting down" EventMachine.stop } Signal.trap("TERM") { puts "Shutting down" EventMachine.stop } @clients = [] EM::WebSocket.start(:host => '0.0.0.0', :port => '3001') do |ws| ws.onopen do |handshake| @clients << ws ws.send "Connected to #{handshake.path}." end ws.onclose do ws.send "Closed." @clients.delete ws end ws.onmessage do |msg| puts "Received message: #{msg}" @clients.each do |socket| socket.send msg end end end Thin::Server.start( App, '0.0.0.0', 3000, signals: false ) end 

我将瘦降级到版本1.5.1,它只是工作。 有线。