I assume that you are not calling start_control_server and stop_control_server inside another controller method. This means that your instance variable (@EchoServer) will not exist when you call stop.
One solution could be to keep the identifier 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
In addition, if you use the rails application using thin ones, then you are already in the eventmachine loop, so you do not need to call EventMachine :: run. Calling EventMachine.stop_server does not seem to disconnect anything that is already connected, but stops further connections from the specified port.
Hope this will be helpful!
source share