2015-12-24 211 views

回答

4

this file中有一个创建HTTP/2客户端的例子。它可以适应这样的APN请求:

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

我一直在为此工作的客户端实现:https://github.com/alloy/lowdown

到目前为止,我一直在广泛地测试它,但它将不会在下周之前投入生产。我很乐意获得更多的测试,反馈等。

3

免责声明:我是下面列出的两个宝石的作者。

如果你想发出HTTP/2调用,你可以考虑NetHttp2,一个Ruby的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  

对写作的HTTP/2调用自己,顶部,如果你想使用新的HTTP/2的细节,并且可以嵌入在苹果推送通知的宝石Rails环境,那么你也可以考虑Apnotic

用法很简单:

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