-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
55 lines (45 loc) · 1.37 KB
/
client.js
File metadata and controls
55 lines (45 loc) · 1.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
// client.js
import io from 'socket.io-client';
import readline from 'readline';
import terminalImage from 'terminal-image';
const socket = io('http://localhost:3000'); // Replace with your server URL
let clientName = ''; // Store the chosen client name
socket.on('connect', () => {
console.log('Connected to server!');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question('Enter your desired name', (name) => {
clientName = name.trim();
socket.emit('set_name', clientName);
console.log(`Your chosen name is: ${clientName}`);
startMessaging(rl);
});
});
function startMessaging(rl) {
const promptInput = () => {
rl.question('', (message) => {
socket.emit('message', { message: message }); // Send only the message
promptInput();
});
};
promptInput();
}
socket.on('message', (data) => {
console.log(`${data.name}: ${data.message}`); // Display colored name with message
});
socket.on('image', async (data) => {
try {
console.log(await terminalImage.buffer(data.message.data.buffer, { width: '100%' }));
} catch (error) {
console.error('Error displaying image:', error);
}
});
socket.on('disconnect', () => {
console.log('Disconnected from server');
process.exit(0);
});
socket.on('connect_error', (err) => {
console.log(`connect_error due to ${err.message}`);
});