从 Ruby on Rails 应用程序启动和停止 eventmachine 服务器的最佳选择是什么
更正:
具体来说,我希望从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜您没有在另一个控制器方法中调用 start_control_server 和 stop_control_server 。这意味着当您调用 stop 时,您的实例变量 (@EchoServer) 将不存在。
一种解决方案可能是将 start_server 返回的 id 存储在会话中。另外
,如果您使用 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
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!