-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
107 lines (82 loc) · 1.86 KB
/
client.js
File metadata and controls
107 lines (82 loc) · 1.86 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
const io = require('socket.io-client');
const readline = require('readline');
const os = require('os');
const socket = io('http://localhost:3001');
class Client {
constructor() {
this.token;
this.cli;
this.userName = process.argv[2] || '';
}
init() {
socket.on('hello', (message) => this.writeLine({line: message}));
socket.on('messageClient', (message) => {
this.writeLine(message)
});
socket.on('newUser', (message) => {
this.writeLine({line: `New user "${message.userName}" has joined the channel.`})
});
socket.on('register', (response) => {
if (response) {
console.log('Registration successful');
} else {
console.log('Registration fail');
}
});
socket.on('login', (response) => {
if (response) {
console.log('Login successful');
this.token = response.token;
this.userName = response.userName;
cli.setPrompt(this.userName + '> ');
cli.prompt();
} else {
console.log('Login fail');
}
});
const cli = readline.createInterface({
input: process.stdin,
output: process.stdout
});
this.cli = cli;
cli.setPrompt('> ');
cli.prompt();
cli.on('line', (line) => {
socket.emit('message', {
line: line,
token: this.token,
userName: this.userName
});
cli.setPrompt(this.userName + '> ');
cli.prompt();
});
}
writeLine(message) {
// process.stdout.clearLine();
process.stdout.cursorTo(0);
if (message.userName) {
process.stdout.write(message.userName + ': ' + message.line + os.EOL);
} else {
process.stdout.write(message.line + os.EOL);
}
this.cli.prompt(true);
}
}
const client = new Client();
client.init();
//
// class Message {
// constructor(line, username) {
// this.line = line;
// this.user
// }
// }
class Command {
constructor(type, value) {
this.type = type;
this.value = value;
}
gtType() {
return type;
}
}