-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
63 lines (47 loc) · 1.33 KB
/
app.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
var WebSocket = require('faye-websocket'),
ws = new WebSocket.Client('wss://wss.redditmedia.com/thebutton?h=739be6ff3871b5be07babfc4236b279fd4e2c02f&e=1428267932');
ws.on('open', function(event) {
console.log('opened connection to reddit the button ws serve');
});
ws.on('message', function(event) {
var data = JSON.parse(event.data);
if(data.type === 'ticking'){
handleTick(data.payload)
}
});
ws.on('close', function(event) {
console.log('close', event.code, event.reason);
ws = null;
});
function write (msg){
process.stdout.write(msg);
};
var numPressedTotal = 0;
var interval;
var lastPress = new Date();
var lastSecsLeft = 0;
function handleTick (payload) {
var newSecsLeft = payload.seconds_left,
numPressedNew = parseInt(payload.participants_text.replace(',', ''));
if(numPressedNew > numPressedTotal){
onPress(newSecsLeft);
numPressedTotal = numPressedNew;
}
};
function onPress (newSecsLeft){
var pressedDate = new Date(),
diff = (lastPress - pressedDate)/1000;
lastPress = pressedDate;
resetCountdown();
write((lastSecsLeft+diff).toFixed(2) + '\n');
lastSecsLeft = newSecsLeft;
};
function startCountdown (){
interval = setInterval(function processLoop () {
write('.');
}, 200);
}
function resetCountdown (){
clearInterval(interval);
startCountdown();
}