从 Ruby on Rails 应用程序启动和停止 eventmachine 服务器的最佳选择是什么

发布于 2024-10-25 12:55:50 字数 867 浏览 1 评论 0原文

更正:

具体来说,我希望从 Ruby 启动和停止 EventMachine (EM)在 Rails 控制器上。

(有人告诉我“薄”很适合这个。)

注意: 此 EM 服务器与运行 Ruby on Rails 的 Mongrel 服务器是分开的应用。 (EM 服务器正在接受来自 Arduino 微控制器的连接。)

作为服务器运行“Thin”。我应该能够接受 HTTP 请求和 Arduino 的连接。

# 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

如果您推荐除直接 EventMachine 之外的其他服务器,请提供执行上述代码的代码。

完整的控制器代码可在此处获取:http://pastie.org/1698383

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 recommending 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小猫一只 2024-11-01 12:55:50

我猜您没有在另一个控制器方法中调用 start_control_server 和 stop_control_server 。这意味着当您调用 stop 时,您的实例变量 (@EchoServer) 将不存在。

一种解决方案可能是将 start_server 返回的 id 存储在会话中。另外

  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

,如果您使用 Thin 运行 Rails 应用程序,那么您已经处于 eventmachine 循环内,因此您不需要调用 EventMachine::run。调用 EventMachine.stop_server 似乎不会断开任何已连接的连接,但会停止与指定端口建立任何进一步的连接。

希望这有一些用处!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文