簡體   English   中英

Thin HTTP服務器下的Ruby AMQP

[英]Ruby AMQP under Thin HTTP server

我正在運行一個簡單的瘦服務器,該服務器將一些消息發布到不同的隊列中,代碼如下所示:

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)

現在,當我運行服務器並執行類似http://0.0.0.0:4001/command=r&number=123123123的操作時,我總是得到重復的輸出,例如:

“沒有命令”“沒有數字”“沒有命令”“沒有數字”

第一件事是為什么我會收到重復的請求? 與瀏覽器有關嗎? 因為當我使用curl時,我沒有相同的行為,其二,為什么我無法獲得參數?

有關此類服務器最佳實現的任何技巧將不勝感激

提前致謝 。

第二個請求來自瀏覽器,查找favicon.ico 您可以通過在處理程序中添加以下代碼來檢查請求:

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

或者,您可以使用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

然后在命令行中運行ruby the_server.rb以啟動http服務器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM