事件机器:如何接收文件上传?
对于普通的 Ruby/Rails 应用程序来说,这有点不寻常。我正在构建一个主要在事件机器之上运行的应用程序。 WEBrick 提供了一些 HTML 文件,但大多数应用程序使用 javascript 在客户端运行,并通过 Web 套接字连接到我的 Event Machine 应用程序。我需要能够接受文件上传并将其存储在本地。这怎么能做到呢?
更新:如果您有兴趣,这里是源代码的链接 。
This is a little out of the ordinary for a normal Ruby/Rails application. I am building an application that mostly runs on top of Event Machine. There are a few HTML files that are served up from WEBrick, but the majority of the application runs client-side with javascript, with a Web Socket connection to my Event Machine application. I need to be able to accept file uploads and store them locally. How can this be done?
Update: If you're interested, here is a link to the source code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先是如何使用 sinatra 构建一个简单的文件上传: 使用 Sinatra 进行文件上传
现在使用 Thin 运行 Web 服务器,您可以在代码中执行此操作:
thin 在内部使用 eventmachine,我认为 webrick 使用线程,但老实说,我从未真正研究过它。
您应该在 ruby 进程之前使用 apache 或 nginx 至少用于文件上传(我认为 websockets 无法通过它工作)。
如果您需要,我可以包含一个简单的 nginx 配置(只需在我的磁盘上找到它 xD)。
编辑:另一个解决方案是使用 goliath 作为 Web 服务器,这里有一个示例: https://github.com/postrank-labs/goliath/blob/master/examples/async_upload.rb
如果您不需要显示上传进度,您应该继续使用 sinatra+nginx/apache。
First here is how to build a simple file upload with sinatra: File upload with Sinatra
So now to run your web server with thin you can do this in your code:
thin uses eventmachine internally, I suppose webrick uses threads but honestly I never really looked into it.
You should use apache or nginx in front of your ruby process at least for the file upload (I think websockets won't work through it).
I can include a simple nginx config if you need (just need to find it on my disk xD).
Edit: Another solution is to use goliath as web server, you have an example here: https://github.com/postrank-labs/goliath/blob/master/examples/async_upload.rb
If you don't need to display upload progress you should stay with sinatra+nginx/apache.
请在 GitHub 上查看此项目: http://www.github.com/igrigorik/em -websocket
以下链接中的代码(server.rb 中的代码可能是一个起点),唯一需要注意的是它使用 Sinatra,但您可以(希望)轻松地将其改编为 WEBrick :http://www.github.com/thirtysixthspan/waterunderice
Please have a look at this project on GitHub : http://www.github.com/igrigorik/em-websocket
The code on the following link ( the code in server.rb might be a starting point), the only caveat is its using Sinatra but you can (hopefully) easily adapt it for WEBrick : http://www.github.com/thirtysixthspan/waterunderice
如果您需要通过 Web 套接字连接进行上传,因此无法使用标准 Rails 控制器来执行此操作,请使用 EventMachine.defer 生成一个新线程来管理文件上传,而不会阻塞您的反应器。
If you need the upload to come through a web socket connection, and thus can't use standard Rails controllers to do this, use EventMachine.defer to spawn a new thread to manage the file upload without blocking your reactor.