Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
84 lines (67 loc) · 1.77 KB
/
Copy pathapp.js
File metadata and controls
84 lines (67 loc) · 1.77 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
(function(){
// --
// -- DOM ELEMENT POINTERS
// --
var logger = PUBNUB.$('pubnub-logger')
, publish_button = PUBNUB.$('publish-button')
, subscribe_button = PUBNUB.$('subscribe-button')
, subscribe_channel = PUBNUB.$('subscribe-channel')
, publish_text = PUBNUB.$('publish-text')
, connected = PUBNUB.$('pubnub-connected')
, channel_name = '';
// --
// -- BASIC LOG OUTPUT FUNCTION
// --
function log(data) {
logger.innerHTML =
'\n' + log.line++ + ': ' +
JSON.stringify(data) + (
channel_name ?
' - from "' + channel_name + '" Channel.' :
'' ) + logger.innerHTML;
}
log.line = 1;
// --
// -- SEND A MESSAGE FUNCTION
// --
function send_message(message) {
PUBNUB.publish({
channel : channel_name,
message : message
});
}
// --
// -- LISTING FOR MESSAGES
// --
function listen(channel) {
// -- UNSUBSCRIBE FROM PREVIOUS CHANNEL
PUBNUB.unsubscribe({ channel : channel_name});
// -- SAVE NEW CHANNEL NAME
channel_name = channel || 'titanium-demo';
// -- SUBSCRIBE TO NEW CHANNEL
PUBNUB.subscribe({
channel : channel_name,
callback : log
});
// -- UPDATE CONNECTED STATUS
connected.innerHTML = 'CONNECTED to "' + channel_name + '"';
PUBNUB.css( connected, { color : "green" } );
}
// --
// -- BIND SUBSCRIBE BUTTON
// --
PUBNUB.bind( 'mousedown,touchstart', subscribe_button, function() {
listen(subscribe_channel.value);
} );
// --
// -- BIND PUBLISH BUTTON
// --
PUBNUB.bind( 'mousedown,touchstart', publish_button, function() {
// -- PUBLISH THE VALUE OF THE TEXTBOX INPUT
send_message( publish_text.value || 'EMPTY MESSAGE' );
} );
// --
// -- GENERAL STARTUP COMPLETE MESSAGE
// --
log("Startup Complete");
})();