Permalink
Browse files

Merge pull request #2 from papr/master

New 'tls'-API
  • Loading branch information...
2 parents f6379a5 + 73334ab commit ca53467ea1f27db0c750f96fd3dfd9fb7a8b9a53 @jpoz committed May 30, 2011
Showing with 88 additions and 106 deletions.
  1. +2 −2 README.md
  2. +80 −98 apns.js
  3. +6 −6 examples/simple.js
View
@@ -2,9 +2,9 @@
Apple push notification service for Node.js
-## Warning
+## Warning-Update
-You need node v0.1.92 for apns-node :-)
+apns-node uses now the new tls-API. It should work with node v0.4.8.
## Usage
View
178 apns.js
@@ -1,113 +1,95 @@
var sys = require("sys"),
- net = require("net"),
+ tls = require("tls"),
fs = require("fs"),
- crypto = require('crypto'),
- Buffer = require('buffer').Buffer
-
-exports.createServer = function(pem_path, host, port) {
- var options = {};
- options.pem_path = pem_path;
- options.host = host || 'gateway.sandbox.push.apple.com';
- options.port = port || 2195;
-
- var server = new APNS(options);
-
- return server
-}
+ url = require("url"),
+ Buffer = require('buffer').Buffer;
+
+exports.createServer = function(cert_path, keys_path, host, port) {
+ var options = {};
+ options.cert_path = cert_path;
+ options.keys_path = keys_path;
+ options.host = host || 'gateway.sandbox.push.apple.com';
+ options.port = port || 2195;
+
+ var server = new APNS(options);
+
+ return server;
+};
APNS = function(options) {
- var self = this;
- var pem = fs.readFileSync(options.pem_path);
-
- var client = this.client = net.createConnection(options.port, options.host);
- this.waiting_buffers = [];
-
- var credentials = crypto.createCredentials({ca:pem, key:pem, cert:pem});
-
- client.setEncoding("UTF8");
- client.setNoDelay(true)
- client.setKeepAlive(true)
-
- client.addListener("connect", function () {
- sys.puts("APNS connected.");
- client.setSecure(credentials);
- });
-
- client.addListener("secure", function () {
- self.waiting_buffers.forEach(function(b){
- client.write(b);
- })
- });
-
- client.addListener("error", function (chunk) {
- sys.puts("APNS ERROR: " + chunk);
- });
-
- client.addListener("end", function () {
- sys.puts("APNS disconnected.");
- });
-
- client.addListener("close", function () {
- sys.puts("APNS closes.");
- });
-}
-
-// Notify device
-//
-// JSON Object keys:
-// alert
-// badge
-// sound
-//
-// TODO custom keys
-//
-// usage: apns_instance.notify('12345abcdefg1235abcdefg', {"alert":"Hello World!"});
+ var self = this;
+ var keyPem = fs.readFileSync(url.resolve(__dirname,options.keys_path));
+ var certPem = fs.readFileSync(url.resolve(__dirname,options.cert_path));
+ var cred = { key:keyPem, cert:certPem };
+
+ var client = this.client = tls.connect(options.port, options.host, cred, function() {
+ this.connected = true;
+ if (client.authorized) {
+ client.setEncoding('utf-8');
+ connected(self);
+ } else {
+ console.log(client.authorizationError);
+ connected(null);
+ }
+ });
+ self.waiting_buffers = [];
+
+ client.addListener('data', function(data) { console.log(data); });
+ client.addListener('error', function(error) {
+ if (!this.connected) { connected(null); }
+ console.log("FAIL: "+error);
+ });
+ client.addListener('close', function() {});
+};
+
+function connected(server) {
+ if (server.client) {
+ server.waiting_buffers.forEach(function(b){
+ server.client.write(b);
+ });
+ } else {
+ console.log("Connection failed");
+ server.end();
+ }
+};
APNS.prototype.notify = function(device_id, obj) {
- var json = JSON.stringify({'aps':obj});
-
- var buffer = this.notification_buffer(device_id, json);
- if (this.client.readyState == 'open') {
- this.client.write(buffer);
- } else {
- this.waiting_buffers.push(buffer)
- }
+ var json = JSON.stringify({'aps':obj});
- return this;
-}
+ var buffer = this.notification_buffer(device_id, json);
+ if (this.client.readyState == 'open') {
+ this.client.write(buffer);
+ } else {
+ this.waiting_buffers.push(buffer);
+ }
+ return this;
+};
APNS.prototype.HEXpack = function(str){
- var p=[];
- for (var i=0;i<str.length;i=i+2) { p.push(parseInt(str[i]+str[i+1], 16)); }
- return p
-}
+ var p=[];
+ for (var i=0;i<str.length;i=i+2) { p.push(parseInt(str[i]+str[i+1], 16)); }
+ return p
+};
APNS.prototype.ASCIIpack = function(str){
- var p=[];
- for (var i=0;i<str.length;i++) { p.push(str.charCodeAt(i)); }
- return p;
-}
+ var p=[];
+ for (var i=0;i<str.length;i++) { p.push(str.charCodeAt(i)); }
+ return p;
+};
APNS.prototype.notification_buffer = function(device_id, json) {
- var a = [0,0,32].concat( this.HEXpack(device_id), [0,json.length], this.ASCIIpack(json) );
-
- var buffer = new Buffer(a.length);
- var count = 0;
- a.forEach(function(e) {
- buffer[count] = e;
- count++;
- });
-
- return buffer;
-}
-
+ var a = [0,0,32].concat( this.HEXpack(device_id), [0,json.length], this.ASCIIpack(json) );
+
+ var buffer = new Buffer(a.length);
+ var count = 0;
+ a.forEach(function(e) {
+ buffer[count] = e;
+ count++;
+ });
+
+ return buffer;
+};
APNS.prototype.end = function() {
- this.client.end();
-}
-
-
-
-
-
-
+ this.client.end();
+};
View
@@ -1,8 +1,8 @@
-var pem_path = './secure.pem'
-// openssl pkcs12 -in mycert.p12 -out client-cert.pem -nodes -clcerts
-
-var device_id = require('fs').readFileSync('./device_id.txt');
+var cert_path = './apns-dev-cert.pem';
+var keys_path = './apns-dev-keys.pem';
+var device_id = require('fs').readFileSync('./device_id.txt', 'utf-8');
// remember to remove "<", ">" and " "'s
-var APNS = require('../apns').createServer(pem_path);
-APNS.notify(device_id, { alert:"Hello", sound: "default", badge:7 }).end();
+var APNS = require('../apns').createServer(cert_path, keys_path);
+APNS.notify(device_id, { alert:"Hello", sound: "default", badge:7 });
+//APNS.end();

0 comments on commit ca53467

Please sign in to comment.