-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
63 lines (52 loc) · 1.66 KB
/
app.js
File metadata and controls
63 lines (52 loc) · 1.66 KB
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
var express = require('express');
var app = express();
var serv = require('http').Server(app);
app.get('/',function(req, res) {
res.sendFile(__dirname + '/client/index.html');
});
app.use('/client',express.static(__dirname + '/client'));
var port = process.env.PORT || 80;
if(process.env.PORT == undefined) {
console.log("no port defined using default (80)");
}
serv.listen(port);
var io = require("socket.io")(serv, {});
console.log("Socket started on port " + port);
var SOCKET_LIST = {};
var MESSAGES = [];
function disconnectSocket(id) {
SOCKET_LIST[id].disconnect();
delete SOCKET_LIST[id];
console.log("User with id " + id + " Disconnected");
}
io.sockets.on("connection", function(socket) {
socket.id = Math.round(Math.random() * 1000);
socket.uName = "Unnamed";
SOCKET_LIST[socket.id] = socket;
console.log("User with id " + socket.id + " Connected");
socket.emit("chat_data", MESSAGES);
socket.on("disconnect", function() {
disconnectSocket(socket.id);
});
socket.on('change_name', function(data){
try {
if(data.length < 1 || data.length > 16) {
return;
}
socket.uName = data;
console.log("User with id " + socket.id + " changed name to " + socket.uName);
} catch(err) {}
});
socket.on('send_chat', function(data){
try {
data = "[" + socket.uName + "]: " + data;
console.log("<Chat>: " + data + " | by user with id " + socket.id);
MESSAGES.push(data);
for(let s in SOCKET_LIST) {
SOCKET_LIST[s].emit("chat", data);
}
} catch(err) {}
});
});
console.log("[Warning] This chat is vulnerable to xss attacks and should only be used to learn how to perform or protect against xss attacks");
console.log("Server started");