-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitter.js
More file actions
51 lines (43 loc) · 1.18 KB
/
gitter.js
File metadata and controls
51 lines (43 loc) · 1.18 KB
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
module.exports = {
stream: stream
};
var https = require('https');
function stream(roomId, token, callback) {
var hostname = roomId
? 'stream.gitter.im'
: 'api.gitter.im';
var path = roomId
? '/v1/rooms/' + roomId + '/chatMessages'
: '/v1/rooms/';
var options = {
hostname: hostname,
port: 443,
path: path,
method: 'GET',
headers: { 'Authorization': 'Bearer ' + token }
};
if (!callback)
callback = function (data) { console.log(data); };
var req = https.request(options, function (res) {
var cache = '';
res.on('data', function (chunk) {
var message = chunk.toString();
var heartbeat = ' \n';
if (message === heartbeat)
return;
cache += message;
try {
var parsed = JSON.parse(cache);
cache = '';
callback(parsed);
}
catch (exception) {
// noop: stream is split
}
});
});
req.on('error', function (e) {
console.log('Something went wrong: ' + e.message);
});
req.end();
}