-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat-cli.js
More file actions
28 lines (24 loc) · 733 Bytes
/
chat-cli.js
File metadata and controls
28 lines (24 loc) · 733 Bytes
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
const readline = require('readline');
const { io } = require('socket.io-client');
// Conectando ao servidor Socket.IO
const socket = io('http://localhost:3000'); // Substitua pela URL do seu servidor
// Configurando o readline para interagir via terminal
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: 'Você: ',
});
// Exibindo mensagens recebidas no terminal
socket.on('chatMessage', (msg) => {
console.log(`\nServidor: ${msg}`);
rl.prompt();
});
// Enviando mensagens digitadas para o servidor
rl.prompt();
rl.on('line', (line) => {
socket.emit('chatMessage', line.trim());
rl.prompt();
}).on('close', () => {
console.log('Chat encerrado.');
process.exit(0);
});