-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
50 lines (42 loc) · 1.04 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
const io = require('socket.io-client');
const readline = require('readline');
const Promise = require('promise');
//var socket = io('http://localhost:3000');
var socket = io('http://cmd-chatter-cmd-chatter.193b.starter-ca-central-1.openshiftapps.com');
var clientName;
var getCmd = () => {
var p = new Promise((resolve, reject) => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('> ', (cmd) => {
resolve(cmd);
rl.close();
});
});
return p;
}
var keepChatting = (name) => {
getCmd().then((cmd) => {
if(cmd !== 'exit') {
socket.emit(name, cmd);
} else {
socket.close();
}
});
}
socket.emit('client', 'teste', (name) => {
if(name) {
clientName = name;
console.log('name = ' + clientName);
socket.on(clientName, (output) => {
console.log(output);
keepChatting(clientName);
});
keepChatting(clientName);
} else {
console.log('Error connecting to ' + 'teste');
socket.close();
}
});