2013-10-10 31 views
1

我正在构建一个运行EM :: WebSocket服务器以及Sinatra服务器的Ruby应用程序。单独来说,我相信这些都能够处理SIGINT。但是,当在同一个应用程序中运行这两个应用程序时,应用程序会继续按Ctrl + C。我的假设是其中一个正在捕获SIGINT,阻止另一个捕获它。不过,我不确定如何去修复它。Ctrl + C没有杀死Sinatra + EM :: WebSocket服务器

这里有简而言之代码:

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 
+0

这是任何帮助吗? http://stackoverflow.com/questions/6456912/thin-doesnt-respond-to-sigint-or-sigterm –

+0

感谢分享。我尝试了一些基于SO问题/接受答案来捕获INT和TERM的变体,但似乎没有任何工作适合我。 –

回答

0

我降级薄1.5.1版本,它只是工作。有线。

1

我有同样的问题。对我来说,关键似乎是薄开始在反应器循环与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