forked from pubnub/javascript
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkeystroke.js
More file actions
75 lines (64 loc) · 2.56 KB
/
Copy pathkeystroke.js
File metadata and controls
75 lines (64 loc) · 2.56 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
/* ---------------------------------------------------------------------------
Init PubNub and Get your PubNub API Keys:
http://www.pubnub.com/account#api-keys
--------------------------------------------------------------------------- */
var pubnub = require("./../pubnub.js").init({
publish_key : "demo",
subscribe_key : "demo"
}) // PubNub Module
, userid = Math.random() * 1000000 | 1 // Random UID
, channel = "my_channel" // Communication Channel
, users = {} // List of Chaters
, lines = []; // Local History
/* ---------------------------------------------------------------------------
Open Network Connection to PubNub
--------------------------------------------------------------------------- */
pubnub.subscribe({
channel : channel,
connect : function() {
console.log("Ready To Receive Messages");
},
callback : function(update) {
// Create New User if Need Be.
update.userid in users || (users[update.userid] = '');
// New Line?
if (
update.key.indexOf('\r') >= 0 ||
update.key.indexOf('\n') >= 0
) {
lines.push(chatln( update.userid, users[update.userid] ));
users[update.userid] = '';
return render();
}
// Append Key(s)
users[update.userid] += update.key;
// Draw
render();
}
});
/* ---------------------------------------------------------------------------
Render Chat
--------------------------------------------------------------------------- */
function chatln( user, message ) { return user + ': ' + message + '\r\n' }
function render() {
// Clear Screen
process.stdout.write('\u001B[2J\u001B[0;0f');
// Print History Lines
lines.forEach(function(line){ process.stdout.write(line) });
console.log('---------------------------------------------------------');
// Print Active Lines
for (var user in users) process.stdout.write(chatln( user, users[user] ));
}
/* ---------------------------------------------------------------------------
Keystrokes
--------------------------------------------------------------------------- */
require('tty').setRawMode(true);
process.openStdin().on( 'keypress', function (chunk, key) {
// Watch for Exit Command
if (key && key.ctrl && key.name == 'c') return process.exit();
// Send Keystroke
pubnub.publish({
channel : channel,
message : { userid : userid, key : chunk }
});
} );