-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
75 lines (66 loc) · 1.74 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
'use strict'
const io = require('socket.io-client')
const socket = io.connect('http://192.168.1.35:3000')
const R = require('ramda')
const inquirer = require('inquirer')
const colors = require('colors')
const client = require('./lib/clientFunctions')
var userName
var textColor
const myLastMessages = []
socket.on('connect', () => {
inquirer.prompt([introQuestion], (answers) => {
socket.emit('newuser', answers)
userName = answers.username
if (userName) {
startChatInput()
}
})
socket.on('commandRes', (data) => {
client.executeCommand(data, socket)
})
socket.on('disconnect', (data) => {
console.log('You\'ve been Disconnected'.red)
})
})
const startChatInput = () => {
socket.on('message', (data) => {
if(R.last(myLastMessages) !== data) {
console.log(data)
}
})
process.stdin.resume()
process.stdin.setEncoding('utf8')
process.stdin.on('data', (text) => {
if (text.split('')[0] === '/') {
client.parseCommand(text, socket)
} else {
text = R.replace(/\n/, '', text)
text = `${userName}: ${text}`
myLastMessages.push(text)
if(textColor) {
socket.emit('message', colors[textColor](text))
} else {
socket.emit('message', text)
}
}
})
}
const introQuestion = {
validate: function(input) {
// Declare function as asynchronous, and save the done callback
var done = this.async()
// Do async stuff
setTimeout(function() {
if (typeof input !== "string") {
// Pass the return value in the done callback
done("Provide a string dude!");
return;
}
// Pass the return value in the done callback
done(true);
}, 10)
},
message: 'Whats your user name? \n',
name: 'username'
}