Nodejs

Chat on the terminal with repl.run!

fork
loading
Files
  • index.js
  • package-lock.json
  • package.json

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.4668612474188436","path":"index.js","file":{"path":"index.js","content":{"asEncoding":{"base64":"dmFyIGlvID0gcmVxdWlyZSgnc29ja2V0LmlvLWNsaWVudCcpOwpjb25zdCBjaGFsayA9IHJlcXVpcmUoJ2NoYWxrJykuY29uc3RydWN0b3IoeyBlbmFibGVkOiB0cnVlLCBsZXZlbDogMyB9KTs7Cgp2YXIgc29ja2V0ID0gaW8oImh0dHBzOi8vY2hhdC1zZXJ2ZXItLWpham9vc2FtLnJlcGwuY28iKTsKCmNvbnN0IHJlYWRsaW5lID0gcmVxdWlyZSgncmVhZGxpbmUnKTsKCmNvbnN0IHJsID0gcmVhZGxpbmUuY3JlYXRlSW50ZXJmYWNlKHsKICBpbnB1dDogcHJvY2Vzcy5zdGRpbiwKICBvdXRwdXQ6IHByb2Nlc3Muc3Rkb3V0Cn0pOwoKCnZhciBpZCA9ICIiOwp2YXIgYnVmZmVyID0gIiI7CgoKZnVuY3Rpb24gY2hhdCgpewoJcmwucXVlc3Rpb24oY2hhbGsubWFnZW50YSgiwrsgIiksIChhbnN3ZXIpID0+IHsKCQlidWZmZXIgPSBgJHtjaGFsay5jeWFuKGlkKX0gOiAke2NoYWxrLmJnQ3lhbihhbnN3ZXIpfWA7CgkJc29ja2V0LmVtaXQoIm1lc3NhZ2UiLCBidWZmZXIpOwoJCWNoYXQoKTsKCX0pOwp9Cgpzb2NrZXQub24oJ2Nvbm5lY3QnLCAoKSA9PiB7CgoJcmwucXVlc3Rpb24oYFdoYXQncyB5b3VyIG5hbWU/IGAsIChhbnN3ZXIpID0+IHsKCQkJc29ja2V0LmVtaXQoIm1lc3NhZ2UiLCBg8J+RpCAgOiAke2NoYWxrLmdyZWVuKGFuc3dlcil9IGhhcyBqb2luZWQgdGhlIGNoYXRgKTsKCQkJaWQgPSBhbnN3ZXI7CgkJCWNoYXQoKTsKCgl9KTsKCglzb2NrZXQub24oJ21zZycsIGZ1bmN0aW9uKGRhdGEpewoJCWlmKGJ1ZmZlciE9ZGF0YSl7CgkJCWNvbnNvbGUubG9nKCJcbiIgKyBkYXRhKTsKCQkJY2hhdCgpOwoJCX0KCX0pOwoJCn0p"},"asBuffer":null},"loaded":true}}
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');
const chalk = require('chalk').constructor({ enabled: true, level: 3 });;

var socket = io("https://chat-server--jajoosam.repl.co");

const readline = require('readline');

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


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


function chat(){
	rl.question(chalk.magenta("» "), (answer) => {
		buffer = `${chalk.cyan(id)} : ${chalk.bgCyan(answer)}`;
		socket.emit("message", buffer);
		chat();
	});
}

socket.on('connect', () => {

	rl.question(`What's your name? `, (answer) => {
			socket.emit("message", `👤  : ${chalk.green(answer)} has joined the chat`);
			id = answer;
			chat();

	});

	socket.on('msg', function(data){
		if(buffer!=data){
			console.log("\n" + data);
			chat();
		}
	});
	
})
node v10.16.0