From 2ffbbda275f923e46121dba9f363ed0d416916a4 Mon Sep 17 00:00:00 2001 From: Andrew Naylor Date: Sun, 15 Aug 2010 00:46:28 +0100 Subject: [PATCH] I appear to have set this up as an EventEmitter. Not sure it really needs to be for the moment. Callbacks seem more appropriate. We shall see. --- node-apn.js | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/node-apn.js b/node-apn.js index 581b44d..d4091c8 100644 --- a/node-apn.js +++ b/node-apn.js @@ -1,10 +1,11 @@ var net = require('net'); +var EventEmitter = require('events').EventEmitter; var crypto = require('crypto'); var sys = require('sys'); var fs = require('fs'); var Buffer = require('buffer').Buffer; -exports.create = function (optionArgs) { +var Connection = function (optionArgs) { this.socket = new net.Stream(); this.credentials = crypto.createCredentials(); @@ -16,6 +17,7 @@ exports.create = function (optionArgs) { , gateway: 'gateway.push.apple.com' /* gateway address */ , port: 2195 /* gateway port */ , enhanced: false /* enable enhanced format */ + , errorCallback: undefined /* Callback when error occurs */ , feedback: false /* enable feedback service, set to callback */ , feedbackInterval: 3600 /* interval in seconds to connect to feedback service */ }; @@ -29,7 +31,7 @@ exports.create = function (optionArgs) { } self.socket.on('connect', function() { console.log("connect."); }); - self.socket.on('data', function(data) { /* Handle error data sent back */ }); + self.socket.on('data', function(data) { handleTransmissionError(data); }); self.socket.on('end', function () { console.log('closed'); self.socket.end(); }); fs.readFile(options['cert'], function(err, data) { @@ -87,6 +89,12 @@ exports.create = function (optionArgs) { pos += data.write(int16val(message.length), pos, 'binary'); pos += data.write(message, pos); + // Generate our own identifiers? + // Need to check notification length at some point + // Push to array + // If array exceeds a certain length then pop and item off + // If error occurs then slice array and resend all stored notes. + if(self.socket.readyState != 'open') { if(self.socket.readyState == 'closed' && hasKey && hasCert) { startSocket(); @@ -101,8 +109,17 @@ exports.create = function (optionArgs) { self.socket.write(data); } } + + var handleTransmissionEror = function() { + + } } +Connection.prototype = new EventEmitter; +Connection.prototype.constructor = Connection; + +exports.connection = Connection; + exports.notification = function () { this.payload = {aps: {}}; this.expiry = 0;