2015-12-24 37 views
11

HTTP/2に基づいて新しいApple Push Notification APIを使用しようとしています。RubyでHTTP/2 POSTリクエストを送信

ルビーの宝石を見つけましたが、クライアントとしてのリクエストの作成方法に関するドキュメントは明確ではありません。

Ruby/RailsでHTTP/2リクエストを作成するにはどうすればよいですか?

+2

あなたは、私はそれがHTTP2を使用しようとしている聞いた最初の人、幸運です、あなたはそれが必要になります – bbozo

答えて

4

this fileにHTTP/2クライアントを作成する例があります。 https://github.com/alloy/lowdown:私は、このためのクライアントの実装に取り​​組んできました

require 'socket' 
require 'http/2' 

payload = '{"foo":"bar"}' 
device_token = '00fc13adff785122b4ad28809a3420982341241421348097878e577c991de8f0' # example 
sock = TCPSocket.new('api.development.push.apple.com', 443) 

conn = HTTP2::Client.new 
conn.on(:frame) do |bytes| 
    puts "Sending bytes: #{bytes.unpack("H*").first}" 
    sock.print bytes 
    sock.flush 
end 

stream = conn.new_stream 

stream.on(:close) do 
    sock.close 
end 

head = { 
    ':scheme' => 'https', 
    ':method' => 'POST', 
    ':path' => "/3/device/#{device_token}", 
    'apns-id' => '123e4567-e89b-12d3-a456-42665544000', # or you could omit this header 
    'content-length' => payload.bytesize.to_s # should be less than or equal to 4096 bytes 
} 

puts 'Sending HTTP 2.0 request' 
stream.headers(head, end_stream: false) 
stream.data(payload) 

while !sock.closed? && !sock.eof? 
    data = sock.read_nonblock(1024) 
    puts "Received bytes: #{data.unpack("H*").first}" 

    begin 
    conn << data 
    rescue => e 
    puts "Exception: #{e}, #{e.message} - closing socket." 
    sock.close 
    end 
end 
3

:それは、このようなAPNの要求に適合させることができます。

これまでのところ、私はそれを広範にテストしてきましたが、来週までは生産に展開されません。私はそれがより多くのテスト、フィードバックなどを得ることが大好きです

3

免責事項:私は以下に記載されている2つの宝石の著者です。

HTTP/2コールを発行する場合は、NetHttp2、つまりRubyのHTTP/2クライアントと考えることができます。

同期呼び出しの使用例に:あなたは新しいHTTP/2仕様を使用し、中に埋め込むことができるアップルのプッシュ通知の宝石をしたい場合はHTTP/2を書き​​込みの上に

require 'net-http2' 

# create a client 
client = NetHttp2::Client.new("http://106.186.112.116") 

# send request 
response = client.call(:get, '/') 

# read the response 
response.ok?  # => true 
response.status # => '200' 
response.headers # => {":status"=>"200"} 
response.body  # => "A body" 

# close the connection 
client.close  

は、自分自身を呼び出しますRails環境では、Apnoticも考えられます。

使い方は非常に簡単です:D:

require 'apnotic' 

# create a persistent connection 
connection = Apnotic::Connection.new(cert_path: "apns_certificate.pem", cert_pass: "pass") 

# create a notification for a specific device token 
token = "6c267f26b173cd9595ae2f6702b1ab560371a60e7c8a9e27419bd0fa4a42e58f" 

notification  = Apnotic::Notification.new(token) 
notification.alert = "Notification from Apnotic!" 

# send (this is a blocking call) 
response = connection.push(notification) 

# read the response 
response.ok?  # => true 
response.status # => '200' 
response.headers # => {":status"=>"200", "apns-id"=>"6f2cd350-bfad-4af0-a8bc-0d501e9e1799"} 
response.body  # => "" 

# close the connection 
connection.close