forked from pubnub/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusage.js
More file actions
70 lines (61 loc) · 2.18 KB
/
usage.js
File metadata and controls
70 lines (61 loc) · 2.18 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
/* ---------------------------------------------------------------------------
Init PubNub and Get your PubNub API Keys:
http://www.pubnub.com/account#api-keys
--------------------------------------------------------------------------- */
var pubnub = require("./../pubnub.js");
var network = pubnub.init({
publish_key : "demo",
subscribe_key : "demo",
secret_key : "",
ssl : true,
origin : "pubsub.pubnub.com"
});
var delivery_count = 0;
//var crazy = ' ~`!@#$%^&*(顶顅Ȓ)+=[]\\{}|;\':"./<>?abcd'
var crazy = ' ~`!@#$%^&*(顶顅Ȓ)+=[]\\{}|;\':"./<>abcd'
/* ---------------------------------------------------------------------------
Listen for Messages
--------------------------------------------------------------------------- */
network.subscribe({
channel : "hello_world",
connect : function() {
console.log('connected');
// Publish a Message on Connect
network.publish({
channel : "hello_world",
message : {
count : ++delivery_count,
some_key : "Hello World!",
crazy : crazy
},
error : function(info){
console.log(info);
},
callback : function(info){
if (!info[0]) console.log("Failed Message Delivery")
console.log(info);
network.history({
channel : "hello_world",
limit : 1,
callback : function(messages){
// messages is an array of history.
console.log(messages);
}
});
}
});
},
callback : function(message) {
console.log(message);
console.log('MESSAGE RECEIVED!!!');
},
error : function() {
console.log("Network Connection Dropped");
}
});
/* ---------------------------------------------------------------------------
Utility Function Returns PubNub TimeToken
--------------------------------------------------------------------------- */
network.time(function(time){
console.log(time);
});