-
Notifications
You must be signed in to change notification settings - Fork 208
/
sender.js
163 lines (137 loc) · 4.39 KB
/
sender.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*!
* node-gcm
* Copyright(c) 2012 Marcus Farkas <marcus.farkas@spaceteam.at>
* MIT Licensed
*/
var Constants = require('./constants');
var Message = require('./message');
var Result = require('./result');
var MulitcastResult = require('./multicastresult');
var https = require('https');
var timer = require('timers');
exports = module.exports = Sender;
function Sender (key) {
this.key = key;
};
Sender.prototype.send = function(message, registrationId, retries, callback) {
var attempt = 1;
var backoff = Constants.BACKOFF_INITIAL_DELAY;
var tryAgain;
if(registrationId.length === 1) {
this.sendNoRetry(message, registrationId, function lambda (result) {
if(result === undefined) {
if(attempt < retries) {
var sleepTime = backoff * 2 * attempt;
if (sleepTime > Constants.MAX_BACKOFF_DELAY)
sleepTime = Constants.MAX_BACKOFF_DELAY;
timer.setTimeout(function () {
sendNoRetryMethod(message, registrationId, lambda);
},sleepTime);
}
else {
console.log('Could not send message after ' + retries + ' attempts');
callback(result);
}
attempt++;
}
else callback(result);
});
}
else if (registrationId.length > 1) {
this.sendNoRetry(message, registrationId, function lambda (result) {
if(attempt < retries) {
var sleepTime = backoff * 2 * attempt;
if (sleepTime > Constants.MAX_BACKOFF_DELAY)
sleepTime = Constants.MAX_BACKOFF_DELAY;
var unsentRegIds = [];
for (var i = 0; i < registrationId.length;i++) {
if (result.results[i].error === 'Unavailable')
unsentRegIds.push(registrationId[i]);
}
registrationId = unsentRegIds;
if(registrationId.length !== 0) {
timer.setTimeout(function () {
sendNoRetryMethod(message, registrationId, lambda);
},sleepTime);
attempt++;
}
else callback(result);
}
else {
console.log('Could not send message to all devices after ' + retries + ' attempts');
callback(result);
}
});
}
else console.log('No RegistrationIds given!');
};
var sendNoRetryMethod = Sender.prototype.sendNoRetry = function(message, registrationIds, callback) {
var body = {}, result = new Result();
body[Constants.JSON_REGISTRATION_IDS] = registrationIds;
if (message.delayWhileIdle !== undefined) {
body[Constants.PARAM_DELAY_WHILE_IDLE] = message.delayWhileIdle;
}
if (message.collapseKey !== undefined) {
body[Constants.PARAM_COLLAPSE_KEY] = message.collapseKey;
}
if (message.timeToLive !== undefined) {
body[Constants.PARAM_TIME_TO_LIVE] = message.timeToLive;
}
if (message.hasData) {
body[Constants.PARAM_PAYLOAD_KEY] = message.data;
}
var requestBody = JSON.stringify(body);
var post_options = {
host: Constants.GCM_SEND_ENDPOINT,
port: '443',
path: Constants.GCM_SEND_ENDPATH,
method: 'POST',
headers: {
'Content-Type' : 'application/json',
'Content-length' : Buffer.byteLength(requestBody, 'utf8'),
'Authorization' : 'key=' + this.key
}
};
var post_req = https.request(post_options, function(res) {
res.setEncoding('utf-8');
var statusCode = res.statusCode;
var buf = '';
res.on('data', function (data) {
buf += data;
});
res.on('end', function () {
if (statusCode === 503) {
console.log('GCM service is unavailable');
callback();
}
else if (statusCode !== 200) {
console.log('Invalid request: ' + statusCode);
callback();
}
// Make sure that we don't crash in case something goes wrong while
// handling the response.
try {
var data = JSON.parse(buf);
if (registrationIds.length === 1) {
if(data.results[0].message_id)
result.messageId = data.results[0].message_id;
else if(data.results[0].error)
result.errorCode = data.results[0].error;
else if(data.results[0].registration_id)
result.canonicalRegistrationId = data.results[0].registration_id;
callback(result);
}
else callback(data);
} catch(e) {
console.log("Error handling GCM response " + e);
callback();
}
});
});
post_req.on('error', function(e) {
console.log("Exception during GCM request: " + e);
callback();
});
post_req.write(requestBody);
post_req.end();
};