lib/apns_simple/client.rb in apns_simple-0.8.0 vs lib/apns_simple/client.rb in apns_simple-0.8.1

- old
+ new

@@ -20,10 +20,11 @@ 7 => 'Invalid payload size', 8 => 'Invalid token', 10 => 'Shutdown', 255 => 'Unknown error' } + TIMEOUT = 5 # In seconds def initialize(options) certificate = options.fetch(:certificate) current_time = Time.now.utc cert = OpenSSL::X509::Certificate.new(certificate) @@ -47,10 +48,15 @@ ssl = OpenSSL::SSL::SSLSocket.new(sock, ssl_context) ssl.connect ssl.write(notification.payload) ssl.flush - if IO.select([ssl], nil, nil, 1) && error = ssl.read(6) + unless IO.select([ssl], nil, nil, TIMEOUT) + notification.error = "No response from APNS server received in #{TIMEOUT} seconds. Exit by timeout." + return + end + + if (error = ssl.read(6)) command, status, _index = error.unpack("ccN") notification.error = command == COMMAND ? "#{status}: #{CODES[status]}" : "Unknown command received from APNS server: #{command}" end ensure ssl.close if ssl \ No newline at end of file