Rails异步通信和xhr轮询
我需要编写一个 Rails 应用程序 (JRuby),它在后台与另一个服务进行异步通信。每个浏览器会话需要一个连接。 (它实际上不需要是一个打开的TCP连接,但我需要在会话结束后释放资源。)与后台服务的通信不是严格的请求-响应。任何时候都可以从服务向 Rails 应用程序发送消息。
我还需要实施该协议。我该怎么做?是否有适用于 Ruby on Rails 的异步框架(例如 Twisted 或 Node.js)?我只需要一些起点。
我已经用 Python 编写了客户端协议的快速实现。事实上,完整的协议是我编写的(服务器是用JavaScript编写的),所以我可以完全改变协议。然而,由于问题的性质,异步性质无法改变。
我还需要 XHR 轮询/WebSockets 才能将异步更改推送到浏览器。有没有相关的 gem/howto/tutorial?我不能只是将请求设置为睡眠(资源!)。
如果您想了解更多背景信息: Rails 应用程序是一种辅助工具。它与之通信的服务实际上是一个加载和呈现网页的 Firefox 插件。异步特性来自 (i) 框架、弹出窗口(window.open()
、window.alert()
...)、DOM 树的动态更改我需要使用 JRuby,因为我需要使用 Jena (Java) 框架进行 RDF 处理。
I need to write a Rails application (JRuby) that does asynchronous communication with another service in the background. There needs to be one connection per browser session. (It does not really need to be a open TCP connection but I need to free resources after the session ends.) The communication with the background service is not strict request - response. At any time there can be a message sent from the service to the rails app.
I also need to implement the protocol. How do I do this? Is there a asynchronous framework (e.g. like Twisted or Node.js) for Ruby on Rails? I just need some starting points.
I already wrote a quick implementation of the client side protocol in Python. In fact the complete protocol is made up by me (the server is written in JavaScript), so I could change the protocol completely. However, the asynchronous nature cannot be changed because of the nature of the problem.
Also I need XHR polling/WebSockets in order to push the async changes to the browser. Is there a gem/howto/tutorial for that? I can't just set the request to sleep (resources!).
In case you want to know more about the background:
The Rails application is a accessibility tool. The service with which it communicates is actually a Firefox Add-on that loads and renders webpages. The asynchronous nature comes from (i)frames, popup windows (window.open()
, window.alert()
, ...), dynamic changes to the DOM tree that have to be communicated to the Rails app, redirects (like after posing in a forum) etc. I need to use JRuby because I need to use the Jena (Java) framework for RDF processing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Ruby On Rails 也是一个异步框架。使用瘦服务器。 Node.js 或 Twisted 不是异步框架。只有事件框架。
在 ruby 中,事件框架是 EventMachine,瘦服务器使用它。您可以使用rack::middleware 创建一些websocket 并使用它。
Ruby On Rails is an asynchronous framework too. with a thin server. Node.js or Twisted are not a Asynchrone framework. There just Event framework.
In ruby the Event Framework is EventMachine and thin serveur use it. You can create some websocket with a rack::middleware and use it.
看看Juggernaut 2,它正是这样做的(并且它也使用节点)。我用它来将消息从我的服务器推送到所有连接的客户端,效果很好。实施很容易(尽管自述文件对某些事情有点不清楚),让我知道您是否需要任何帮助。
Have a look at Juggernaut 2, it does exactly this (and it also uses node). I am using it to push messages from my server to all connected clients and it works great. Implementation was a breeze (although the readme is a bit unclear on certain things), lemme know if you need any help with it.