-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.js
More file actions
172 lines (141 loc) · 4.37 KB
/
client.js
File metadata and controls
172 lines (141 loc) · 4.37 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
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
import '@babel/polyfill/noConflict';
import io from 'socket.io-client';
import { createInterface } from 'readline';
import axios from 'axios';
import prompt from 'prompt';
import util from 'util';
import chalk from 'chalk';
import gettingStarted from './util/gettingStarted';
import inputValidator from './util/settings';
const { log, error } = console;
const validateUser = async (password) => {
const res = await axios.post('http://localhost:2018/auth', { password });
return res;
};
const renderMessage = (color, name, msg) => {
switch (color) {
case 'red':
log(`${chalk.italic.bold(chalk.red(name))}: ${chalk.white(msg)}`);
break;
case 'blue':
log(`${chalk.italic.bold(chalk.blue(name))}: ${chalk.white(msg)}`);
break;
case 'yellow':
log(`${chalk.italic.bold(chalk.yellow(name))}: ${chalk.white(msg)}`);
break;
case 'green':
log(`${chalk.italic.bold(chalk.green(name))}: ${chalk.white(msg)}`);
break;
case 'magenta':
log(`${chalk.italic.bold(chalk.magenta(name))}: ${chalk.white(msg)}`);
break;
case 'cyan':
log(`${chalk.italic.bold(chalk.cyan(name))}: ${chalk.white(msg)}`);
break;
case 'gray':
log(`${chalk.italic.bold(chalk.gray(name))}: ${chalk.white(msg)}`);
break;
default:
log(`${chalk.italic.bold(chalk.bgRed(name))}: ${chalk.white(msg)}`);
break;
}
};
const startApp = async () => {
try {
prompt.start();
prompt.message = '';
const get = util.promisify(prompt.get);
log(chalk.cyan('--------------- SIGN IN ---------------'));
const userSchema = [{
description: 'Enter a username',
name: 'username',
required: true,
},
{
description: 'Enter password',
name: 'password',
required: true,
}];
const { password } = await get(userSchema[1]);
const { status, message } = (await validateUser(password)).data;
if (status !== 200 && message !== 'Success') throw new Error('Incorrect password');
const { username } = await get(userSchema[0]);
const host = ['http://localhost:2018', 'https://trim-chat.herokuapp.com'];
const socket = io(host[1], {
path: '/chat',
});
socket.on('connect', () => {
log(chalk.green(`You (${username}) are connected`));
});
socket.on('user connected', (data) => {
log(chalk.yellow(`New user: ${data} has joined`));
socket.emit('Register', username, (data) => {
log(data);
gettingStarted();
});
});
const input = createInterface({
input: process.stdin,
});
socket.on('message', ({ sender, message: msg, color }) => {
renderMessage(color, sender, msg);
});
socket.on('Direct Message', ({ name, message: msg }) => {
log(`${chalk.bold.white('Direct Message =>')}${chalk.italic.bold.cyan(name)}: ${chalk.white(msg)}`);
});
input.on('line', async (value) => {
const {
inputType, message: mssg, chatroom, receipient,
} = inputValidator(value);
switch (inputType) {
case 'create room':
socket.emit('Create chatroom', chatroom, (result) => {
log(result);
});
break;
case 'join':
socket.emit('Join chatroom', chatroom, (result) => {
log(result);
});
break;
case 'leave':
socket.emit('Leave chatroom', chatroom, (result) => {
log(result);
});
break;
case 'delete room':
socket.emit('Delete chatroom', chatroom, (result) => {
log(result);
});
break;
case 'list members':
socket.emit('View chatroom members', (result) => {
log(result);
});
break;
case 'list chatrooms':
socket.emit('List all chatrooms', (result) => {
log(result);
});
break;
case 'Chatroom message':
socket.emit('Chatroom message', mssg, (result) => {
log(result);
});
break;
case 'Direct message':
socket.emit('Direct message', { mssg, receipient }, (result) => {
log(result);
});
break;
default:
log('try "trim_chat --help" for help on how to use app');
break;
}
});
} catch (err) {
error(chalk.red(err));
process.exit(0);
}
};
startApp();