Sinatra 与 EventMachine WebSockets 合作是否成功?
我已经使用 Sinatra 一段时间了,我想通过 websockets 推送数据来向我的网络应用程序添加一些实时功能。
我已经成功地单独使用了 gem 'em-websocket',但无法编写一个包含 sinatra Web 服务器和 Web-socket 服务器的 ruby 文件。
我试过旋转跑步!或开始!方法在单独的线程中关闭但没有成功。
有人让它发挥作用吗?
我希望将它们放在同一个文件中,这样我就可以在两台服务器之间共享变量。
I have been using Sinatra for sometime now and I would like to add some realtime features to my web-app by pushing the data via websockets.
I have successfully used the gem 'em-websocket' on its own, but have not been able to write one ruby file that has a sinatra web server AND a web-socket server.
I've tried spinning the run! or start! methods off in separate threads with no success.
Has anyone gotten this to work?
I want to have them in the same file as I can then share variables between the two servers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
没有尝试过,但应该不会太难:
此外, Cramp 有一个 websocket 实现,可以直接使用细/彩虹!您也许能够解压,因此您甚至不需要在另一个端口上运行服务器。
Did not try it, but should not be too hard:
Also, Cramp has a websocket implementation that works directly with Thin/Rainbows! you might be able to extract, so you won't even need to run the server on another port.
谢谢康斯坦丁...这有效!我不得不稍微调整一下你的代码。我在修改的地方添加了评论。
-波尔
Thanks Konstantin... that worked! I had to tweak your code slightly. I added comments where I changed it.
-poul
我偶然发现了这个 websocket-rack github 项目,它基本上是一个机架化的 em-websocket 并且实际上让它与 Sinatra 应用程序很好地并行工作。这是我的 config.ru:
玩得开心!
科林
I stumbled onto this websocket-rack github project which is basically a rackified em-websocket and actually got it to work nicely side-by-side with a Sinatra app. Here's my config.ru:
Have fun!
Colin
我一直在使用 sinatra-websocket。它允许您在与 Sinatra 相同的进程和相同的端口上运行 websocket 服务器。
免责声明:我是维护者。
I've been using sinatra-websocket. It let's you run the websocket server in the same process and on the same port as Sinatra.
Disclaimer: I'm the maintainer.
仅供参考,您还可以将 Padrino 应用程序与 EventMachine 一起使用(因为它们是 Sinatra 应用程序的子集):
FYI, you can also use Padrino apps with EventMachine (as they are subsets of Sinatra apps):