开发者

What's the best option for starting and stopping an eventmachine server from a Ruby on Rails application

CORRECTION:

Specifically I'm looking to start and stop an EventMachine (EM) from a Ruby on Rails controller.

(I've been told that 'Thin' would work well for this.)

NOTE: This EM server is SEPARATE from the Mongrel server running the Ruby on Rails application. (The EM server is accepting connections from an Arduino microcontroller.)

Running 'Thin' as the server .. I SHOULD be able to accept both HTTP requests and the Arduino's connections.

# Starts Server
def start_control_server
    EventMachine::run {
        @EchoServer = EventMachine::start_server "0.0.0.0", 40013, EchoServer
    }
end

# Attempts ( fails ) to stop server
def stop_control_server
    EventMachine.stop_server(@EchoServer)
end

If you're 开发者_如何学JAVArecommending other servers OTHER than a straight-up EventMachine please provide code to execute the above code.

Complete controller code available here: http://pastie.org/1698383


I'm guessing you're not calling start_control_server and stop_control_server inside another controller method. That means your instance variable (@EchoServer) isn't going to exist when you call stop.

One solution might be to store the id returned from start_server in the session. As in

  def start_control_server
    session[:em_server_id] = EventMachine::start_server "0.0.0.0", 4000, EchoServer
  end

  def stop_control_server
    EventMachine.stop_server(session[:em_server_id]) if session[:em_server_id]
    session[:em_server_id] = nil
  end

Also, if you're running your rails app using thin then you are already inside an eventmachine loop, so you don't need to call EventMachine::run. Calling EventMachine.stop_server doesn't seem to disconnect anything that is already connected but stops any further connections from being established to the specified port.

Hope thats of some use!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜