你如何产生一个 EventMachine “inside"?Rails 应用程序? [英] How do you spawn an EventMachine "inside" a Rails app?

查看:39
本文介绍了你如何产生一个 EventMachine “inside"?Rails 应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Rails 应用程序,我希望为它添加某种 WebSocket 支持.从各种谷歌搜索来看,最好的基于 Ruby 的 WebSocket 解决方案似乎是在 EventMachine 上运行的 em-websocket.

I've got a Rails application, and am looking to add some sort of WebSocket support to it. From various googling, it appears that the best Ruby based WebSocket solution is em-websocket running on EventMachine.

我想知道是否有办法将 EventMachine 反应器集成"到 Rails 中?我在哪里放置初始化代码?这是实现此目的的正确方法吗?

I was wondering if there was a way to "integrate" an EventMachine reactor into Rails? Where do I put the initialization code? Is this the proper way to accomplish this?

我见过 这个 依靠 Sinatra 执行 EventMachine 的示例 GET 请求,但这并不是我想要的.

I've seen this example that falls back on Sinatra to do an EventMachine GET request, but that isn't quite what I'm looking for.

感谢任何帮助.

推荐答案

我会尝试使用 em-synchrony 在光纤中启动反应器.在 rails 应用程序中,您可能可以在初始化程序中启动它,因为听起来您只想让反应器运行以响应 websocket 请求.正如其他答案所建议的那样,我认为您想要设置与您的反应器的套接字通信,或者使用异步客户端之一连接到您的反应器和 rails 代码都可以读取和写入以交换数据的数据存储.

I'd try using em-synchrony to start a reactor in a fiber. In a rails app you can probably start it in an initializer since it sounds like you just want to leave the reactor running to respond to websocket requests. As suggested by the other answers I think you want to either setup socket communication with your reactor or use one of the asynchronous clients to a data store which both your reactor and rails code can read from and write to to exchange data.

我的一些同事整理了一些在 ruby​​ 代码中按需启动 EM 反应器的示例,以在 EventMachine 中运行他们的测试.我会尝试使用它作为一个可能的例子;使用 eventmachine 进行倾斜和测试

Some of my coworkers put together some examples of starting EM reactors on demand in ruby code to run their tests within EventMachine. I'd try using that as a possible example; raking and testing with eventmachine

这篇关于你如何产生一个 EventMachine “inside"?Rails 应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆