forked from pubnub/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubUnsubHereNow.js
More file actions
85 lines (66 loc) · 1.97 KB
/
Copy pathsubUnsubHereNow.js
File metadata and controls
85 lines (66 loc) · 1.97 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
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
var pubnub = require("./../pubnub.js").init({
publish_key: "demo",
subscribe_key: "demo",
uuid: "ptest",
origin: "pubsub.pubnub.com"
});
var myChannel = "zzz";
var iteration = 0;
var joinMode = true;
var hereNowIntervals = [1.5];
var queue = [];
sub();
function setTimeouts() {
var timeout = queue.pop();
setTimeout(function () {
hereNow(timeout)
}, (timeout * 1000));
}
function getOccupancy() {
for (var i = 0; i < hereNowIntervals.length; i++) {
setTimeouts();
}
}
function sub() {
queue = hereNowIntervals.slice(0);
joinMode = true;
pubnub.subscribe({
channel: myChannel,
connect: function () {
console.log("%s - BEGIN JOIN TEST CYCLE", new Date());
getOccupancy();
},
callback: function (message) {
//console.log("%s - %s", new Date(), message);
},
disconnect: function () {
console.log("DISCONNECTED");
}
});
}
function hereNow(interval) {
iteration = interval;
//console.log("%s - HERE_NOW ITERATION %s", new Date(), interval);
pubnub.here_now({"channel": myChannel, "callback": onHereNow});
}
function onHereNow(occupancyStats) {
var uuids = occupancyStats.uuids;
// console.log("uuids: %s", uuids);
var modeSwitch = joinMode ? -1 : 0;
if (uuids.indexOf("ptest") != modeSwitch) {
console.log("%s - OK: joinMode: %s - iteration: %s - UUIDS: %s", new Date(), joinMode, iteration, uuids);
} else {
console.log("%s - *** FAIL *** : %s - iteration: %s - UUIDS: %s", new Date(), joinMode, iteration, uuids);
}
if (iteration == hereNowIntervals[0]) {
if (joinMode) {
pubnub.unsubscribe({"channel": myChannel});
console.log("%s - BEGIN LEAVE TEST CYCLE", new Date());
joinMode = false;
queue = hereNowIntervals.slice(0);
setTimeout(getOccupancy, 2000);
} else {
sub();
}
}
}