lib/apns_simple/client.rb in apns_simple-1.1.0 vs lib/apns_simple/client.rb in apns_simple-1.1.1

- old
+ new

@@ -44,36 +44,29 @@ gateway_uri = options[:gateway_uri] || DEFAULT_GATEWAY_URI @host, @port = parse_gateway_uri(gateway_uri) end def push(notification) - notification.error = true sock = TCPSocket.new(host, port) ssl = OpenSSL::SSL::SSLSocket.new(sock, ssl_context) + ssl.sync = true ssl.connect ssl.write(notification.payload) - ssl.flush - ready = IO.select([ssl], [], [], TIMEOUT) - - unless ready - notification.error_message = "No response from APNS server received in #{TIMEOUT} seconds. Exit by timeout." - return - end - - readable_ssl_socket = ready.first.first - - if (error = readable_ssl_socket.read(ERROR_BYTES_COUNT)) - command, code, _index = error.unpack('ccN') - if command == COMMAND - notification.error_code = code - notification.error_message = "CODE: #{code}, DESCRIPTION: #{CODES[code]}" - else - notification.error_message = "Unknown command received from APNS server: #{command}" + if (ready = IO.select([ssl], [], [], TIMEOUT)) + readable_ssl_socket = ready.first.first + if (error = readable_ssl_socket.read(ERROR_BYTES_COUNT)) + command, code, _index = error.unpack('ccN') + notification.error = true + if command == COMMAND + notification.error_code = code + notification.error_message = "CODE: #{code}, DESCRIPTION: #{CODES[code]}" + else + notification.error_message = "Unknown command received from APNS server: #{command}" + end end - else - notification.error = false end + ensure ssl.close if ssl sock.close if sock end \ No newline at end of file