2015-12-24 13 views
11

Sto tentando di utilizzare la nuova API Apple Push Notification, che è basata su HTTP/2.Invio di POST HTTP/2 in Ruby

Ho trovato la gemma Rubino http-2, ma la documentazione non è chiara su come effettuare richieste come client.

Come effettuare una richiesta HTTP/2 in Ruby/Rails?

+2

tu sei la prima persona che ho sentito che sta cercando di utilizzare HTTP2, buona fortuna, ne avrete bisogno: D – bbozo

risposta

4

C'è un esempio di creazione di client HTTP/2 in this file. Si potrebbe essere adattato alle richieste APN come questo:

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

ho lavorato su un client di attuazione per questo: https://github.com/alloy/lowdown.

Finora l'ho testato estensivamente, ma non verrà distribuito in produzione fino alla prossima settimana. Mi piacerebbe che si verificassero più test, feedback, ecc.

3

NOTA BENE: Sono l'autore delle due gemme elencate qui sotto.

Se si desidera inviare chiamate HTTP/2, è possibile considerare NetHttp2, un client HTTP/2 per Ruby.

esempio di utilizzo per la sincronizzazione chiama:

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  

In cima a scrivere il HTTP/2 chiamate voi stessi, se si vuole una Push Notification gioiello di Apple che utilizza i nuovi/2 specifiche HTTP e può essere integrato in un L'ambiente delle rotaie può anche essere considerato Apnotic.

uso è molto semplice:

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 
Problemi correlati