手机
当前位置:查字典教程网 >脚本专栏 >ruby专题 >ruby实现的一个异步文件下载HttpServer实例
ruby实现的一个异步文件下载HttpServer实例
摘要:1.使用rubyeventmachine和em-http-servergem,完成一个简单的提供文件下载功能的HttpServer2.使用了...

1.使用ruby eventmachine和em-http-server gem,完成一个简单的提供文件下载功能的HttpServer

2.使用了EM的FileStreamer来异步发送文件,发送文件时先组装了header,然后调用FileStreamer

require 'rubygems' require 'eventmachine' require 'em-http-server' class HTTPHandler < EM::HttpServer::Server attr_accessor :filename, :filesize, :path def process_http_request #send file async if @http_request_method.to_s =~ /GET/ && @http_request_uri.to_s.end_with?(filename) send_data "HTTP/1.1 200 OKn" send_data "Server: XiaoMin" send_data "Connection: Keep-Aliven" send_data "Keep-Alive: timeout=15n" send_data "Content-Type: application/octet-streamn" send_data "Content-Disposition: filename='#{filename}'n" send_data "Content-Length: #{filesize}n" send_data "n" streamer = EventMachine::FileStreamer.new(self, path) streamer.callback { # file was sent successfully close_connection_after_writing } else response = EM::DelegatedHttpResponse.new(self) response.status = 200 response.content_type 'text/html' response.content = "Package HttpServer<br>usage: wget http://host:port/#{filename}" response.send_response end end end EM::run do path = '/tmp/aaa.tar.gz' EM::start_server("0.0.0.0", 8080, HTTPHandler) do |conn| conn.filename = File.basename(path) conn.filesize = File.size(path) conn.path = path end end

【ruby实现的一个异步文件下载HttpServer实例】相关文章:

Ruby中对一元操作符重载实例

Ruby中数组的一些相关使用方法

用Ruby实现一个单元测试框架的教程

ruby开发的交互式程序例子

Ruby元编程的一些值得注意的地方

Ruby中的反射(Reflection)应用实例

Ruby中访问SQL Server数据库的配置实例

Ruby实现的最短编辑距离计算方法

ruby元编程之method_missing的一个使用细节

PHP实现的一个保存远程文件到本地的函数分享

精品推荐
分类导航