-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.js
271 lines (256 loc) · 7.59 KB
/
client.js
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
var io = require('socket.io-client')
, termPrompt = require('prompt')
, blessed = require('blessed')
, pixelr = require('pixelr')
, execute = require('child_process').exec
, asciize = require('./asciize').asciize;
var socket
, username
, other
, room = ''
, win
, box
, input
, video
, videoInterval
, scrollLock = false;
console.log('Connecting to server...');
socket = io.connect('http://safe-eyrie-8054.herokuapp.com/');
socket.on('connect', function(data) {
socket.emit('connection');
getName();
});
socket.on('validation', function(data) {
if (data.valid === 'valid') {
listen();
getUserInput();
}
else {
console.log('Sorry, that name is not available.');
getName();
}
});
function getName() {
termPrompt.message = "";
termPrompt.delimiter = "";
termPrompt.start();
termPrompt.get(
{
properties: {
name: {
description: 'Welcome! Enter a username (e.g. e-mail if you want '.magenta +
'to connect with people you know):'.magenta
}
}
}, checkName);
}
function checkName(err, result) {
if (!err) {
username = result.name;
socket.emit('check_name', {name: result.name});
}
}
function listen() {
socket.emit('join', {room: room, user: username});
socket.on('user_list', function(data) {
writeMessage('Current users: ' + JSON.stringify(data.users) + '\n');
});
socket.on('new_user', function(data) {
writeMessage(data.user + ' has joined the room.\n');
});
socket.on('leave_user', function(data) {
writeMessage(data.user + ' has left the room.\n');
});
socket.on('new_message', function(data) {
writeMessage('{bold}' + data.user + '{/bold}: ' + data.msg + '\n', false);
});
socket.on('chat?', function(data) {
other = data.user;
writeMessage(data.user + ' would like to chat with you.\n' +
'Type /y to accept their request.\n');
});
socket.on('video?', function(data) {
other = data.user;
writeMessage(data.user + ' would like to video chat with you.\n' +
'Type /y to accept their request.\n');
});
socket.on('no_user', function(data) {
writeMessage('Tried chatting with ' + data.user + ', but they are not on termchat.\n' +
'Type /users to see who\'s on termchat right now.\n');
});
socket.on('new_room', function(data) {
clearInterval(videoInterval);
room = data.room;
writeMessage('You\'ve entered a chat room with ' + data.user + '!\n');
});
socket.on('send_video', function(data) {
receiveVideo();
sendVideo(data);
});
}
function getUserInput() {
win = blessed.screen();
win.on('keypress', function() {
input.focus();
});
box = blessed.box({
left: '0',
width: '100%',
height: '90%',
content: 'Type /help at any time to see valid commands.\n',
tags: true,
mouse: true,
scrollable: true,
label: 'tskype',
border: {
type: 'line'
}
});
input = blessed.textbox({
left: '0',
width: '100%',
height: '10%',
top: '90%',
tags: true,
mouse: true,
inputOnFocus: true,
border: {
type: 'line'
}
});
win.append(box);
win.append(input);
input.key(['escape', 'C-c'], function(ch, key) {
return process.exit(0);
});
input.setValue('> ');
input.on('submit', sendMessage);
input.focus();
win.render();
}
function writeMessage(contents, useColor) {
// looks ridiculous but necessary because useColor by default is undefined
if (!(useColor === false)) {
contents = '{blue-fg}' + contents + '{/blue-fg}';
}
box.setContent(box.getContent() + contents);
if (box.getContent().split('\n').length > win.height) {
box.setScrollPerc(100);
}
win.render();
}
function sendMessage(value) {
input.setValue('> ');
input.render();
value = value.trim();
if (value.substring(0, 1) === '>') {
value = value.substring(1).trim();
}
if (value.substring(0, 1) === '/' && value.substring(1, 2) !== '/') {
var command = value.split(' ')[0].substring(1, value.split(' ')[0].length);
var args = value.split(' ').slice(1, value.split(' ').length).join(' ');
writeMessage(parseHelp(command, args));
}
else if (value.substring(0, 1) === '/') {
socket.emit('message', {room: room, user: username, msg: value.substring(1, value.length)});
}
else {
socket.emit('message', {room: room, user: username, msg: value});
}
}
function parseHelp(command, args) {
switch(command) {
case 'help':
return 'To send a message starting with \'/\', ' +
'type \'//\'.\nA list of valid commands:\n' +
'/help: display this help message\n' +
'/users: display a list of users in the room\n' +
'/chat {username}: enter a chatroom with someone\n' +
'/video {username}: enter a video call with someone\n' +
'/y: accept the last chat or video request you received\n' +
'/scroll: toggle scroll lock\n';
case 'users':
socket.emit('get_users', {room: room});
return '';
case 'chat':
if (!args.split(' ')[0]) {
return 'Enter a username to chat with after the /chat command.\n';
}
if (args.split(' ')[0] === username) {
return 'You can\'t chat with yourself, sorry!\n';
}
socket.emit('chat_request', {user: username, other: args.split(' ')[0]});
return 'Requesting to chat with ' + args.split(' ')[0] + '...\n';
case 'video':
if (!args.split(' ')[0]) {
return 'Enter a username to video chat with after the /video command.\n';
}
if (args.split(' ')[0] === username) {
return 'You can\'t chat with yourself, sorry!\n';
}
socket.emit('video_request', {user: username, other: args.split(' ')[0]});
return 'Requesting video call with ' + args.split(' ')[0] + '...\n';
case 'y':
if (!(!!other)) {
return 'There is no request you can accept right now.\n';
}
socket.emit('accept', {user: username, other: other});
return '';
case 'scroll':
scrollLock = !scrollLock;
return 'Scroll lock is now ' + scrollLock + '\n';
default:
return 'Command \'' + command + '\' not recognized. Enter /help for commands.\n';
}
}
function receiveVideo() {
if (win.height < 39 || win.width < 150) {
writeMessage('You need your terminal to be at least 150 characters wide ' +
'and 39 characters tall to display video\n');
}
else {
box.width = '50%';
input.width = '50%';
video = blessed.box({
left: '50%',
width: '50%',
height: '100%',
tags: true,
border: {
type: 'line'
}
});
win.append(video);
win.render();
}
socket.on('receive_frame', function(data) {
video.setContent(data.pixels);
win.render();
});
}
function sendVideo(data) {/*
videoInterval = setInterval(function() {
execute('streamer -o image.jpeg', function(error, stdout, stderr) {
if (error) {
writeMessage('Error capturing video: ' + stderr);
return;
}
try {
pixelr.read('image.jpeg', 'jpeg', function(image) {
socket.emit('send_frame', {user: username, other: data.other,
pixels: asciize(image, video.width, video.height)});
});
}
catch(err) {
writeMessage('Error reading video: ' + err);
}
});
}, 150);*/
pixelr.read('image.jpeg', 'jpeg', function(image) {
socket.emit('send_frame', {user:username, other: data.other, pixels:asciize(image, video.width, video.height)});
});
}
process.on('exit', function() {
socket.emit('disconnect');
clearInterval(videoInterval);
});