repl.it
@Vandesm14/

Chat Server

Express

No description

fork
loading
Files
  • index.js
  • public
  • db.js
  • db.sqlite3
  • index.new.js
  • notes.txt
  • 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
const express = require('express');
const bodyParser = require('body-parser');
const fs = require('fs');
const uid = require('rand-token').uid;

const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);

/* --------------------Variables-------------------- */
var rooms = []; // Global to server (server list)
var users = []; // Local to each room (Used per room)
var serverList = {};
var cmds = ['/users'];

/* --------------------Server-------------------- */
// app.listen(3000, () => console.log('server started on port 3000'));
http.listen(3000, function () {
	console.log('listening on *:3000');
	console.log('Runtime ID:' + uid(16));
});

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static('public'));

/* --------------------Routes-------------------- */
// Homepage
app.get('/', (req, res) => {
	res.sendFile(__dirname + '/public/pages/home.html');
});

// Get Chatroom Page
app.get('/room/*', (req, res) => {
	// req.url;
	res.sendFile(__dirname + '/public/pages/room.html');
});

app.get('/error', (req, res) => {
	res.sendFile(__dirname + '/public/pages/error.html');
});

/* --------------------WebSockets-------------------- */
// Basic connection
io.on('connection', function (socket) {
	let id = socket.id;

	socket.on('chat message', function (msg, user, url) {
		newMessage(socket.id, formatDate(), user, msg, url);
	});

	socket.on('new user', function (user, url) {
		newMessage('N/A', formatDate(), '<span style="color:grey">--&gt; ', statMsg(user, socket.id, url, 'joined') + '</span>', url, true);
	});

	socket.on('disconnect user', function (user, url) {
		newMessage('N/A', formatDate(), '<span style="color:grey">&lt;-- ', statMsg(user, socket.id, url, 'left') + '</span>', url, true);
		let users = io.of('/').in(url).clients;
	});

	socket.on('get room', function (user, url, key = '') {
		socket.join(url);
	});

	socket.on('send users', function (user, url) {
		if (serverList[url] == undefined) {
			serverList[url] = [];
		}
		serverList[url].push(user);
	});
});

/* --------------------Functions-------------------- */
function newMessage(id, time, user, msg, url, bypass = false) {
	if (cmds.includes(msg)) {
		io.sockets.in(url).emit('chat message', cmdMsg(msg, url) + '</span>', '<span style="color:grey">&gt; ', formatDate());
	} else {
		if (bypass == false) {
			io.sockets.in(url).emit('chat message', sanitize(msg), sanitize(user), formatDate(), url);
		} else {
			io.sockets.in(url).emit('chat message', msg, user, formatDate(),url , true);
		}
	}
}

function statMsg(user, id, url, status) {
	// console.log('(' + formatDate() + ') "' + sanitize(user) + '" has ' + status + ' (' + id + ')\n[' + url + ']');

	if (serverList[url] == undefined) {
		serverList[url] = [];
	}
	if (status == 'joined') {
		serverList[url].push(user);
	} else if (status == 'left') {
		serverList[url].splice(serverList[url].indexOf(user), 1);
	}
	console.log(serverList); // Log users
	io.emit('list users', serverList);
	return '<b>' + sanitize(user) + '</b> has ' + status;
}

function cmdMsg(msg, url) {
	// msg = msg.split(' ');
	switch (msg) {
		case '/users':
			return serverList[url].join(', ');
			break;
		case '/ping':
			// Ping Code here (placeholder)
			return 'Ping 10ms';
			break;
		default:
			return false;
	}
}

function formatDate(date = new Date()) {
	time = date.getMonth() + '/' + date.getDate() + '/' + date.getFullYear() + ' ' + date.getHours() + ':' + date.getMinutes() + ' GMT';
	return time;
}

function sanitize(text) {
	// console.log(text);
	return text.replace(/<(?:.|\n)*>/gi, '[html]');
}
?