forked from pubnub/javascript
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmany-channels.js
More file actions
57 lines (47 loc) · 1.22 KB
/
Copy pathmany-channels.js
File metadata and controls
57 lines (47 loc) · 1.22 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
var PUBNUB = require('../pubnub.js')
var pubnubs = {};
var received = 0;
function subscribe(channel) {
/*
create container
*/
};
(new Array(100)).join().split(',').forEach(function( _, a ){ setTimeout(function(){
var channel = 'channel-'+a;
pubnubs[channel] = create_pubnub(channel);
pubnubs[channel].subscribe({
channel : channel,
message : message,
connect : function() {
console.log( 'connected! ',channel );
setTimeout( function(){ pubit(channel) }, 1000 );
}
})
},a*300);});
function pubit(channel) {
pubnubs[channel].publish({
channel : channel,
message : channel
});
}
function create_pubnub(channel) {
return PUBNUB.init({
origin : uuid().split('-')[4] + '.pubnub.com'
});
}
function error(result) {
console.log( 'Error with', result )
}
function message(result) {
received++;
console.log( received, 'RECEIVED!', result );
}
function uuid(callback) {
var u = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g,
function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
if (callback) callback(u);
return u;
}