Permalink
Browse files

Merge branch 'develop' of git://github.com/rwtombaugh/node-apn into m…

…aster
  • Loading branch information...
2 parents 316522c + e2438ed commit 70f807765c2927a5cd9c19813f2e4e2cd0ffb6c0 @argon argon committed Dec 21, 2012
Showing with 23 additions and 4 deletions.
  1. +1 −0 README.md
  2. +5 −1 lib/connection.js
  3. +17 −3 lib/feedback.js
View
@@ -109,6 +109,7 @@ Using the `Feedback` object it is possible to periodically query the server for
address: 'feedback.push.apple.com', /* feedback address */
port: 2196, /* feedback port */
feedback: false, /* enable feedback service, set to callback */
+ batchFeedback: false, /* if feedback should be called once per connection. */
interval: 3600 /* interval in seconds to connect to feedback service */
};
View
@@ -366,10 +366,14 @@ Connection.prototype.handleTransmissionError = function (data) {
*/
Connection.prototype.raiseError = function(errorCode, notification) {
debug("Raising error:", errorCode, notification);
+
if(errorCode instanceof Error) {
debug("Error occurred with trace:", errorCode.stack);
}
- if (typeof this.options.errorCallback == 'function') {
+
+ if (notification && typeof notification.errorCallback == 'function' ) {
+ notification.errorCallback(errorCode);
+ } else if (typeof this.options.errorCallback == 'function') {
this.options.errorCallback(errorCode, notification);
}
};
View
@@ -29,7 +29,8 @@ if(process.env.DEBUG) {
* @config {String} [passphrase] The passphrase for the connection key, if required
* @config {String} [address="feedback.push.apple.com"] The feedback server to connect to.
* @config {Number} [port=2195] Feedback server port
- * @config {Function} [feedback] A callback which accepts 2 parameters (timestamp, {@link Device}). See: {@link <a href="https://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingWIthAPS/CommunicatingWIthAPS.html#//apple_ref/doc/uid/TP40008194-CH101-SW3">Communicating with APS</a>.
+ * @config {Function} [feedback] A callback which accepts 2 parameters (timestamp, {@link Device}) or an array of (timestamp, {@link Device}) object tuples, depending on the value of batchFeedback option. See: {@link <a href="https://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingWIthAPS/CommunicatingWIthAPS.html#//apple_ref/doc/uid/TP40008194-CH101-SW3">Communicating with APS</a>.
+ * @config {Boolean} [batchFeedback] If true, the feedback callback will only be called once per connection with an array of timestamp and device token tuples.
* @config {Function} [errorCallback] Callback which will capture connection errors
* @config {Number} [interval=3600] Interval to automatically connect to the Feedback service.
*/
@@ -48,6 +49,7 @@ function Feedback(options) {
port: 2196, /* feedback port */
rejectUnauthorized: true, /* Set this to false incase using a local proxy, reject otherwise */
feedback: false, /* enable feedback service, set to callback */
+ batchFeedback: false, /* If the feedback callback should only be called once per connection. */
errorCallback: false, /* error handler to catch connection exceptions */
interval: 3600, /* interval in seconds to connect to feedback service */
};
@@ -175,6 +177,7 @@ Feedback.prototype.connect = function () {
}.bind(this));
this.readBuffer = new Buffer(0);
+ this.feedbackData = [];
this.socket.on('data', this.receive.bind(this));
this.socket.on("error", this.destroyConnection.bind(this));
this.socket.once('close', this.resetConnection.bind(this));
@@ -210,9 +213,13 @@ Feedback.prototype.receive = function (data) {
this.readBuffer.copy(token, 0, 6, 6 + tokenLength);
debug("Parsed device token: %s, timestamp: %d", token.toString("hex"), time);
- if (typeof this.options.feedback == 'function') {
+ var device = new Device(token);
+ if (!this.options.batchFeedback &&
+ typeof this.options.feedback == 'function') {
debug("Calling feedback method for device");
- this.options.feedback(time, new Device(token));
+ this.options.feedback(time, device);
+ } else {
+ this.feedbackData.push({ time: time, device: device });
}
this.readBuffer = this.readBuffer.slice(6 + tokenLength);
}
@@ -236,6 +243,13 @@ Feedback.prototype.resetConnection = function () {
if (this.socket) {
this.socket.removeAllListeners();
}
+
+ if (this.options.batchFeedback &&
+ typeof this.options.feedback == 'function') {
+ debug("Calling feedback method for a batch of devices");
+ this.options.feedback(this.feedbackData);
+ this.feedbackData = [];
+ }
if(!this.deferredConnection.promise.isResolved()) {
debug("Connection error occurred before TLS Handshake");

0 comments on commit 70f8077

Please sign in to comment.