@Vandesm14/

Chat Server

Express

No description

fork
loading
Files
  • index.js
  • public
  • db.js
  • db.sqlite3
  • index.new.js
  • notes.txt
  • package-lock.json
  • package.json

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.534325866235666","path":"index.js","file":{"path":"index.js","content":{"asEncoding":{"base64":"Y29uc3QgZXhwcmVzcyA9IHJlcXVpcmUoJ2V4cHJlc3MnKTsKY29uc3QgYm9keVBhcnNlciA9IHJlcXVpcmUoJ2JvZHktcGFyc2VyJyk7CmNvbnN0IGZzID0gcmVxdWlyZSgnZnMnKTsKY29uc3QgdWlkID0gcmVxdWlyZSgncmFuZC10b2tlbicpLnVpZDsKCmNvbnN0IGFwcCA9IGV4cHJlc3MoKTsKY29uc3QgaHR0cCA9IHJlcXVpcmUoJ2h0dHAnKS5TZXJ2ZXIoYXBwKTsKY29uc3QgaW8gPSByZXF1aXJlKCdzb2NrZXQuaW8nKShodHRwKTsKCi8qIC0tLS0tLS0tLS0tLS0tLS0tLS0tVmFyaWFibGVzLS0tLS0tLS0tLS0tLS0tLS0tLS0gKi8KdmFyIHJvb21zID0gW107IC8vIEdsb2JhbCB0byBzZXJ2ZXIgKHNlcnZlciBsaXN0KQp2YXIgdXNlcnMgPSBbXTsgLy8gTG9jYWwgdG8gZWFjaCByb29tIChVc2VkIHBlciByb29tKQp2YXIgc2VydmVyTGlzdCA9IHt9Owp2YXIgY21kcyA9IFsnL3VzZXJzJ107CgovKiAtLS0tLS0tLS0tLS0tLS0tLS0tLVNlcnZlci0tLS0tLS0tLS0tLS0tLS0tLS0tICovCi8vIGFwcC5saXN0ZW4oMzAwMCwgKCkgPT4gY29uc29sZS5sb2coJ3NlcnZlciBzdGFydGVkIG9uIHBvcnQgMzAwMCcpKTsKaHR0cC5saXN0ZW4oMzAwMCwgZnVuY3Rpb24gKCkgewoJY29uc29sZS5sb2coJ2xpc3RlbmluZyBvbiAqOjMwMDAnKTsKCWNvbnNvbGUubG9nKCdSdW50aW1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L2I+IGhhcyAnICsgc3RhdHVzOwp9CgpmdW5jdGlvbiBjbWRNc2cobXNnLCB1cmwpIHsKCS8vIG1zZyA9IG1zZy5zcGxpdCgnICcpOwoJc3dpdGNoIChtc2cpIHsKCQljYXNlICcvdXNlcnMnOgoJCQlyZXR1cm4gc2VydmVyTGlzdFt1cmxdLmpvaW4oJywgJyk7CgkJCWJyZWFrOwoJCWNhc2UgJy9waW5nJzoKCQkJLy8gUGluZyBDb2RlIGhlcmUgKHBsYWNlaG9sZGVyKQoJCQlyZXR1cm4gJ1BpbmcgMTBtcyc7CgkJCWJyZWFrOwoJCWRlZmF1bHQ6CgkJCXJldHVybiBmYWxzZTsKCX0KfQoKZnVuY3Rpb24gZm9ybWF0RGF0ZShkYXRlID0gbmV3IERhdGUoKSkgewoJdGltZSA9IGRhdGUuZ2V0TW9udGgoKSArICcvJyArIGRhdGUuZ2V0RGF0ZSgpICsgJy8nICsgZGF0ZS5nZXRGdWxsWWVhcigpICsgJyAnICsgZGF0ZS5nZXRIb3VycygpICsgJzonICsgZGF0ZS5nZXRNaW51dGVzKCkgKyAnIEdNVCc7CglyZXR1cm4gdGltZTsKfQoKZnVuY3Rpb24gc2FuaXRpemUodGV4dCkgewoJLy8gY29uc29sZS5sb2codGV4dCk7CglyZXR1cm4gdGV4dC5yZXBsYWNlKC88KD86LnxcbikqPi9naSwgJ1todG1sXScpOwp9"},"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
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]');
}