Class: Pushr::Daemon::Apns2Support::Connection
- Inherits:
-
Object
- Object
- Pushr::Daemon::Apns2Support::Connection
- Defined in:
- lib/pushr/daemon/apns2_support/connection.rb
Constant Summary collapse
- TIMEOUT =
30
- RENEW_TOKEN =
60 * 55
Instance Attribute Summary collapse
-
#configuration ⇒ Object
readonly
Returns the value of attribute configuration.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#close ⇒ Object
def write(notification) request = Request.new(notification, token) http2_request = @client.prepare_request(:post, request.path, body: request.body, headers: request.headers) push = Push.new(http2_request) push.on(:response) do |response| # read the response puts response.ok? # => true puts response.status # => ‘200’ puts response.headers # => “apns-id”=>“6f2cd350-bfad-4af0-a8bc-0d501e9e1799” puts response.body # => “” end @client.call_async(push.http2_request) end.
- #connect ⇒ Object
-
#initialize(configuration, i) ⇒ Connection
constructor
A new instance of Connection.
-
#on(event, &block) ⇒ Object
def join @client.join end.
- #token ⇒ Object
- #url ⇒ Object
- #write(notification) ⇒ Object
Constructor Details
#initialize(configuration, i) ⇒ Connection
Returns a new instance of Connection.
11 12 13 14 |
# File 'lib/pushr/daemon/apns2_support/connection.rb', line 11 def initialize(configuration, i) @name = "#{configuration.app}: Connection #{i}" @configuration = configuration end |
Instance Attribute Details
#configuration ⇒ Object (readonly)
Returns the value of attribute configuration.
9 10 11 |
# File 'lib/pushr/daemon/apns2_support/connection.rb', line 9 def configuration @configuration end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
9 10 11 |
# File 'lib/pushr/daemon/apns2_support/connection.rb', line 9 def name @name end |
Instance Method Details
#close ⇒ Object
def write(notification)
request = Request.new(notification, token)
http2_request = @client.prepare_request(:post, request.path, body: request.body, headers: request.headers)
push = Push.new(http2_request)
push.on(:response) do |response|
# read the response
puts response.ok? # => true
puts response.status # => '200'
puts response.headers # => {":status"=>"200", "apns-id"=>"6f2cd350-bfad-4af0-a8bc-0d501e9e1799"}
puts response.body # => ""
end
@client.call_async(push.http2_request)
end
74 75 76 |
# File 'lib/pushr/daemon/apns2_support/connection.rb', line 74 def close @client.close end |
#connect ⇒ Object
16 17 18 19 |
# File 'lib/pushr/daemon/apns2_support/connection.rb', line 16 def connect @client = NetHttp2::Client.new(url, connect_timeout: TIMEOUT) @client.on(:error) { |exception| puts Pushr::Daemon.logger.error(exception) } end |
#on(event, &block) ⇒ Object
def join
@client.join
end
82 83 84 |
# File 'lib/pushr/daemon/apns2_support/connection.rb', line 82 def on(event, &block) @client.on(event, &block) end |
#token ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/pushr/daemon/apns2_support/connection.rb', line 25 def token if !@token || (Time.now - @timestamp > RENEW_TOKEN) @timestamp = Time.now @token = Token.new(configuration).generate end @token end |
#url ⇒ Object
21 22 23 |
# File 'lib/pushr/daemon/apns2_support/connection.rb', line 21 def url @configuration.sandbox ? 'https://api.development.push.apple.com' : 'https://api.push.apple.com' end |
#write(notification) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/pushr/daemon/apns2_support/connection.rb', line 33 def write(notification) retry_count = 0 request = Request.new(notification, token) begin response = @client.call(:post, request.path, body: request.body, headers: request.headers, timeout: TIMEOUT) puts response.inspect Response.new(headers: response.headers, body: response.body) if response rescue SocketError => e retry_count += 1 if retry_count == 1 Pushr::Daemon.logger.error("[#{name}] Lost connection to #{url} (#{e.class.name}), reconnecting...") end if retry_count <= 3 Pushr::Daemon.logger.error("[#{name}] retrying #{retry_count}") # connect sleep 1 retry end raise ConnectionError, "#{name} tried #{retry_count - 1} times but failed (#{e.class.name})." end end |