forked from pubnub/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubUnsubHereNow.html
More file actions
94 lines (75 loc) · 2.11 KB
/
subUnsubHereNow.html
File metadata and controls
94 lines (75 loc) · 2.11 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
85
86
87
88
89
90
91
92
93
94
<html>
<body>
<div id="output"></div>
<script src=http://cdn.pubnub.com/pubnub-3.5.47.min.js ></script>
<script>
var options = {
publish_key: "demo",
subscribe_key: "demo",
uuid: "ptest",
origin: "pubsub.pubnub.com"
};
var myChannel = "zzz";
var iteration = 0;
var joinMode = true;
var hereNowIntervals = [5];
var queue = [];
var pubnub = PUBNUB.init(options);
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();
}
}
}
</script>
</body>
</html>