forked from pubnub/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrant-revoke.js
More file actions
106 lines (93 loc) · 3.21 KB
/
Copy pathgrant-revoke.js
File metadata and controls
106 lines (93 loc) · 3.21 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
95
96
97
98
99
100
101
102
103
104
105
106
/* ---------------------------------------------------------------------------
Init Supervisor Client
--------------------------------------------------------------------------- */
var PUBNUB = require("./../pubnub.js")
, auth_key = 'NzVqS3NsMmJOZGtsM2pzbEhEamxrczNnamFrbHM'
, channel = 'mychannel'
, pubnub = PUBNUB.init({
publish_key : 'pub-c-a2650a22-deb1-44f5-aa87-1517049411d5',
subscribe_key : 'sub-c-a478dd2a-c33d-11e2-883f-02ee2ddab7fe',
secret_key : 'sec-c-YjFmNzYzMGMtYmI3NC00NzJkLTlkYzYtY2MwMzI4YTJhNDVh'
});
/* ---------------------------------------------------------------------------
- Main -
--------------------------------------------------------------------------- */
grant()
.then(open_stream_listen)
.then(publish_expected_successful)
.then(revoke)
.then(publish_expected_fail);
/* ---------------------------------------------------------------------------
Open Stream Listener.
--------------------------------------------------------------------------- */
function open_stream_listen(cb) {
log('connecting');
pubnub.auth(auth_key);
pubnub.subscribe({
channel : channel,
callback : stream_receiver,
connect : proceed
});
return next();
}
/* ---------------------------------------------------------------------------
Client Test - Access Granted
--------------------------------------------------------------------------- */
function publish_expected_successful() {
log('publish_expected_successful');
pubnub.publish({
channel : channel,
message : 'test-data',
callback : proceed
});
return next();
}
/* ---------------------------------------------------------------------------
Client Test - Access Denied
--------------------------------------------------------------------------- */
function publish_expected_fail() {
log('publish_expected_fail');
pubnub.publish({
channel : 'foo',
message : 'test-data',
error : proceed
});
return next();
}
/* ---------------------------------------------------------------------------
Grant
--------------------------------------------------------------------------- */
function grant(cb) {
log('grant');
pubnub.grant({
channel : channel,
auth_key : auth_key,
ttl : 300,
read : true,
write : true,
callback : proceed
});
return next();
}
/* ---------------------------------------------------------------------------
Revoke
--------------------------------------------------------------------------- */
function revoke(cb) {
log('revoke');
pubnub.revoke({
channel : channel,
auth_key : auth_key,
callback : proceed
});
return next();
}
/* ---------------------------------------------------------------------------
Stream Receiver
--------------------------------------------------------------------------- */
function log(d) { console.log(d) }
function stream_receiver(message) { log( " > " + JSON.stringify(message) ) }
function proceed(d) { var cb = next.cb.shift();cb&&cb();stream_receiver(d) }
function next() {
if (!next.cb) next.cb = [];
return { then : function(cb) { cb&&next.cb.push(cb); return next(); } };
}