Skip to content

Commit

Permalink
Working on feedback.
Browse files Browse the repository at this point in the history
  • Loading branch information
markbates committed Jul 30, 2009
1 parent e6b7afe commit cf4ae4f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
35 changes: 31 additions & 4 deletions lib/apn_on_rails/app/models/apn/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,34 @@ def open_for_delivery(options = {}, &block)
# configatron.apn.feedback.cert = File.join(rails_root, 'config', 'apple_push_notification_development.pem')) # Development
# configatron.apn.feedback.cert = File.join(rails_root, 'config', 'apple_push_notification_production.pem')) # Production
def open_for_feedback(options = {}, &block) # :nodoc:
options = {:cert => configatron.apn.feedback.cert,
:passphrase => configatron.apn.feedback.passphrase,
:host => configatron.apn.feedback.host,
:port => configatron.apn.feedback.port}.merge(options)
open(options, &block)

# 'c5280d58a5dda773f5d59578f895c0e16124fab77938ac00b903115a21d4'.unpack('N1n1H140')
# time (big endian), token length (big endian), device token (binary)
# [1664430648, 12388, "353861356464613737336635643539353738663839356330653136313234666162373739333861633030623930333131356132316434"]
# APN::Connection.open_for_feedback do |conn, sock|
# # begin
# # while data = sock.read(76)
# # next if data.size < 76
# # timestamp, token_length, device_token = data.unpack('N1n1H140')
# # puts timestamp
# # puts token_length
# # puts device_token
# # end
# # rescue SocketError => e
# # puts 'oops!'
# # puts e.message
# # end
#
# while line = sock.gets # Read lines from the socket
# puts line.chop.unpack('N1n1H140') # And print with platform line terminator
# end
#
# end
end

private
Expand All @@ -48,15 +75,15 @@ def open(options = {}, &block) # :nodoc:
ctx.key = OpenSSL::PKey::RSA.new(cert, options[:passphrase])
ctx.cert = OpenSSL::X509::Certificate.new(cert)

s = TCPSocket.new(options[:host], options[:port])
ssl = OpenSSL::SSL::SSLSocket.new(s, ctx)
sock = TCPSocket.new(options[:host], options[:port])
ssl = OpenSSL::SSL::SSLSocket.new(sock, ctx)
ssl.sync = true
ssl.connect

yield ssl if block_given?
yield ssl, sock if block_given?

ssl.close
s.close
sock.close
end

end
Expand Down
2 changes: 1 addition & 1 deletion lib/apn_on_rails/app/models/apn/notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class << self
def send_notifications(notifications = APN::Notification.all(:conditions => {:sent_at => nil}))
unless notifications.nil? || notifications.empty?

APN::Connection.open_for_delivery do |conn|
APN::Connection.open_for_delivery do |conn, sock|
notifications.each do |noty|
conn.write(noty.message_for_sending)
noty.sent_at = Time.now
Expand Down

0 comments on commit cf4ae4f

Please sign in to comment.