简体   繁体   English

Thin HTTP服务器下的Ruby AMQP

[英]Ruby AMQP under Thin HTTP server

I'm running a simple thin server, that publish some messages to different queues, the code looks like : 我正在运行一个简单的瘦服务器,该服务器将一些消息发布到不同的队列中,代码如下所示:

require "rubygems"
require "thin"
require "amqp"
require 'msgpack'

app = Proc.new do |env|

 params  = Rack::Request.new(env).params

 command = params['command'].strip rescue "no command"
 number  = params['number'].strip  rescue "no number"

 p command
 p number

AMQP.start do
  if command =~ /\A(create|c|r|register)\z/i
    MQ.queue("create").publish(number)
  elsif m = (/\A(Answer|a)\s?(\d+|\d+-\d+)\z/i.match(command))
    MQ.queue("answers").publish({:number => number,:answer => "answer" }.to_msgpack )
  end
end

 [200, {'Content-Type' => "text/plain"} , command ]

end

Rack::Handler::Thin.run(app, :Port => 4001)

Now when I run the server, and do something like http://0.0.0.0:4001/command=r&number=123123123 I'm always getting duplicate outputs, something like : 现在,当我运行服务器并执行类似http://0.0.0.0:4001/command=r&number=123123123的操作时,我总是得到重复的输出,例如:

"no command" "no number" "no command" "no number" “没有命令”“没有数字”“没有命令”“没有数字”

The first thing is why I'm getting like duplicate requests ? 第一件事是为什么我会收到重复的请求? is it something has to do with the browser ? 与浏览器有关吗? since when I use curl I'm not having the same behavior , and the second thing why I can't get the params ? 因为当我使用curl时,我没有相同的行为,其二,为什么我无法获得参数?

Any tips about the best implementation for such a server would be highly appreciated 有关此类服务器最佳实现的任何技巧将不胜感激

Thanks in advance . 提前致谢 。

The second request comes from the browser looking for the favicon.ico . 第二个请求来自浏览器,查找favicon.ico You can inspect the requests by adding the following code in your handler: 您可以通过在处理程序中添加以下代码来检查请求:

 params  = Rack::Request.new(env).params
 p env # add this line to see the request in your console window

Alternatively you could use Sinatra : 或者,您可以使用Sinatra

require "rubygems"
require "amqp"
require "msgpack"
require "sinatra"

get '/:command/:number' do
    command = params['command'].strip rescue "no command"
    number  = params['number'].strip  rescue "no number"
    p command
    p number
    AMQP.start do
        if command =~ /\A(create|c|r|register)\z/i
            MQ.queue("create").publish(number)
        elsif m = (/\A(Answer|a)\s?(\d+|\d+-\d+)\z/i.match(command))
            MQ.queue("answers").publish({:number => number,:answer => "answer" }.to_msgpack )
        nd
    end
    return command
end

and then run ruby the_server.rb at the command line to start the http server. 然后在命令行中运行ruby the_server.rb以启动http服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM