Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

removed whitespace, added ApnsClient with standard ruby SSL socket code

  • Loading branch information...
commit 1f9d180cb681d9def81305368d923da49af6673f 1 parent 3308718
@bpoweski authored
Showing with 39 additions and 23 deletions.
  1. +39 −22 lib/apnserver/server.rb
  2. +0 −1  test/notification_test.rb
View
61 lib/apnserver/server.rb
@@ -1,9 +1,10 @@
-require 'socket'
-
require 'rubygems'
require 'eventmachine'
require 'apnserver/notification'
+require 'socket'
+require 'openssl'
+
class ApnProxyServer < EventMachine::Connection
attr_accessor :queue
@@ -26,28 +27,45 @@ def receive_data(data)
end
end
-class ApnClient < EventMachine::Connection
- def post_init
- puts "Starting TLS"
- start_tls(
- :private_key_file => $1,
- :cert_chain_file => $2,
- :verify_peer => false
- )
+class ApnsClient
+
+ attr_accessor :certificate, :key, :host, :port
+
+
+ def intialize(certificate, key, host = 'gateway.push.apple.com', port = 2195)
+ @certificate, @key, @host, @port = certificate, path, host, port
end
- def receive_data data
- # we won't receive anything
+ def connect!
+ raise "The path to your pem file is not set." 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)
+
+ @sock = TCPSocket.new(self.host, self.port)
+ @ssl = OpenSSL::SSL::SSLSocket.new(@sock, @context)
+ @ssl.connect
+
+ return @sock, @ssl
end
- def ssl_handshake_completed
- puts get_peer_cert
- end
+ def disconnect!
+ @ssl.close
+ @sock.close
+ end
+
+ def write(bytes)
+ @ssl.write(bytes)
+ end
+
+ def connected?
+ @ssl
+ end
- def unbind
- puts "#{Time.now} DISCONNECT from APNS"
- end
end
+
EventMachine::run do
puts "Starting APN Server: #{Time.now}"
@@ -57,18 +75,17 @@ def unbind
s.queue = queue
end
- client = EM.connect('localhost', 2195, ApnClient)
+ client = ApnsClient.new($1, $2)
EventMachine::PeriodicTimer.new(1) do
unless queue.empty?
size = queue.size
size.times do
queue.pop do |notification|
- puts notification.inspect
+ client.connect! unless client.connected?
+ client.write(notification.to_bytes)
end
end
end
end
-
- puts client.inspect
end
View
1  test/notification_test.rb
@@ -77,4 +77,3 @@ def test_should_pack_and_unpack_json
end
end
end
-
Please sign in to comment.
Something went wrong with that request. Please try again.