Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixes the feedback method to read the correct number of bytes and to …

…do it from the correct socket
  • Loading branch information...
commit 962c76bcc255782462d68ced7e418dd5af57ef47 1 parent 60f8dd8
@MichaelBaker MichaelBaker authored
Showing with 18 additions and 20 deletions.
  1. +18 −20 lib/apns/core.rb
View
38 lib/apns/core.rb
@@ -8,50 +8,49 @@ module APNS
# openssl pkcs12 -in mycert.p12 -out client-cert.pem -nodes -clcerts
@pem = nil # this should be the path of the pem file not the contentes
@pass = nil
-
+
class << self
attr_accessor :host, :pem, :port, :pass
end
-
+
def self.send_notification(device_token, message)
n = APNS::Notification.new(device_token, message)
self.send_notifications([n])
end
-
+
def self.send_notifications(notifications)
sock, ssl = self.open_connection
-
+
notifications.each do |n|
ssl.write(n.packaged_notification)
end
-
+
ssl.close
sock.close
end
-
+
def self.feedback
sock, ssl = self.feedback_connection
-
+
apns_feedback = []
-
- while line = sock.gets # Read lines from the socket
- line.strip!
- f = line.unpack('N1n1H140')
- apns_feedback << [Time.at(f[0]), f[2]]
+
+ while message = ssl.read(38)
+ timestamp, token_size, token = message.unpack('N1n1H*')
+ apns_feedback << [Time.at(timestamp), token]
end
-
+
ssl.close
sock.close
-
+
return apns_feedback
end
-
+
protected
def self.open_connection
raise "The path to your pem file is not set. (APNS.pem = /path/to/cert.pem)" unless self.pem
raise "The path to your pem file does not exist!" unless File.exist?(self.pem)
-
+
context = OpenSSL::SSL::SSLContext.new
context.cert = OpenSSL::X509::Certificate.new(File.read(self.pem))
context.key = OpenSSL::PKey::RSA.new(File.read(self.pem), self.pass)
@@ -62,23 +61,22 @@ def self.open_connection
return sock, ssl
end
-
+
def self.feedback_connection
raise "The path to your pem file is not set. (APNS.pem = /path/to/cert.pem)" unless self.pem
raise "The path to your pem file does not exist!" unless File.exist?(self.pem)
-
+
context = OpenSSL::SSL::SSLContext.new
context.cert = OpenSSL::X509::Certificate.new(File.read(self.pem))
context.key = OpenSSL::PKey::RSA.new(File.read(self.pem), self.pass)
fhost = self.host.gsub('gateway','feedback')
puts fhost
-
+
sock = TCPSocket.new(fhost, 2196)
ssl = OpenSSL::SSL::SSLSocket.new(sock,context)
ssl.connect
return sock, ssl
end
-
end
Please sign in to comment.
Something went wrong with that request. Please try again.