repl.it
@minx28/

chatroom

Nodejs

Start building your own chatroom in a CLI!

fork
loading
Files
  • index.js
  • package-lock.json
index.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
var io = require('socket.io-client');
var socket = io("https://chat-server--minx28.repl.co");
const chalk=require("chalk")

const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});


var id = "";
var buffer = "";

function chat(){
	rl.question("» ", (answer) => {
    /*if colour==0 socket.emit("message", id+": "+chalk.red(answer));
    if colour==1 socket.emit("message", id+": "+chalk.green(answer));
    if colour==2 socket.emit("message", id+": "+chalk.yellow(answer));
    if colour==3 socket.emit("message", id+": "+chalk.blue(answer));
    if colour==4 socket.emit("message", id+": "+chalk.magenta(answer));
    if colour==5 socket.emit("message", id+": "+chalk.cyan(answer));*/
    socket.emit("message",id+": "+chalk.red(answer));
    chat();
  });
}


socket.on('connect', () => {
	rl.question(`What's your name? `, (answer) => {
			socket.emit("message", `👤  : ${answer} has joined the chat`);
			id = answer;
      //colour = 0;
      //colour=round(Math.random()*5);
      //console.log(colour)
			chat();
  });
  socket.on('msg', function(data){
    console.log("\n" + data);
    chat();
  });
})
node v10.16.0