2015-12-24 27 views
11

Tôi đang cố gắng sử dụng API Apple Push Notification mới, dựa trên HTTP/2.Gửi yêu cầu HTTP/2 POST trong Ruby

Tôi đã tìm thấy đá quý Ruby của http-2, nhưng tài liệu không rõ ràng về cách đưa ra yêu cầu với tư cách khách hàng.

Cách tạo yêu cầu HTTP/2 trong Ruby/Rails?

+2

Bạn là người đầu tiên tôi nghe nói rằng đang cố gắng sử dụng HTTP2, chúc may mắn, bạn sẽ cần nó: D – bbozo

Trả lời

4

Có ví dụ về cách tạo ứng dụng khách HTTP/2 trong this file. Nó có thể được thích nghi với yêu cầu APN như thế này:

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

Tôi đã làm việc trên một thực khách hàng cho việc này: https://github.com/alloy/lowdown.

Cho đến nay tôi đã thử nghiệm rộng rãi nó, nhưng nó sẽ không được triển khai vào sản xuất cho đến tuần tới. Tôi muốn nó được thử nghiệm, phản hồi, v.v.

3

KHUYẾN CÁO: Tôi là tác giả của hai loại đá quý được liệt kê ở đây bên dưới.

Nếu bạn muốn phát hành cuộc gọi HTTP/2, bạn có thể xem xét NetHttp2, ứng dụng HTTP/2 cho Ruby.

Cách sử dụng ví dụ cho đồng bộ gọi:

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  

Trên đầu trang của văn bản HTTP/2 gọi cho mình, nếu bạn muốn một Apple Push Notification đá quý có sử dụng HTTP/2 chi tiết cụ thể mới và có thể được nhúng vào trong một Rails môi trường sau đó bạn cũng có thể xem xét Apnotic.

Cách sử dụng rất đơn giản:

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