repl.it
Nodejs

NODE JS chat engine inspired by @jajoosam.

fork
loading
Files
  • index.js
  • Packager files
  • package-lock.json
  • package.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
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
var io = require('socket.io-client');
var SimpleCrypto = require('simple-crypto-js').default;
var keypress = require('keypress');
const chalk = require('chalk').constructor({ enabled: true, level: 3 });;
var socket = io("https://server.williampenrod.repl.co");
const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});
// build an interiactive interface


keypress(process.stdin);
var id = "";
var buffer = "";
var logi = "1232132312313";
var sc = new SimpleCrypto(logi);
function validate(){
  socket.emit("message", chalk.yellow("[!] New unverified connection."));
  rl.question("[!!]::: ", function (answer) {
    logi = answer
    verify();
  });}

function verify(){
  if(hash(logi) != "1997809077"){
    socket.emit("message", chalk.red("[!] There was an unsucsussful login. Killing there process..."));
    process.exit();
  }else {
    sc.setSecret(logi);
      rl.question(`What's your name: `, (answer) => {
			socket.emit("message", chalk.green(`[i] : ${chalk.magenta(answer)} has joined and can now recive all encrypted messages!`));
			id = answer;
		chat();
});}}

function hash(s) {
    for(var i = 0, h = 0; i < s.length; i++)
        h = Math.imul(31, h) + s.charCodeAt(i) | 0;
    return h;
}
// »
function chat(){
	rl.question(chalk.magenta("» "), (answer) => {
		buffer = `${chalk.cyan(id)} : ${chalk.blue(answer)}`;
		socket.emit("emessage", sc.encrypt(buffer));
		chat();
	});
}

  socket.on('connect', () => {
    validate();
});

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

  	socket.on('emsg', function(data){
		if(buffer!=data){
			console.log("\n" + sc.decrypt(data));
			chat();
		}
	});

?