-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
250 lines (212 loc) · 7.29 KB
/
server.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/**
* Webchat using Node.js
* - based upon https://github.com/nodecode/Node.js-Chat
* - now in cool
*
* @author Alexander Ochs, http://noxmiles.de
*
* + + LICENCES + +
* Notification Sound:
* https://notificationsounds.com/notification-sounds/you-wouldnt-believe-510
* Licensed under the Creative Commons Attribution license
*
* Icons from www.flaticon.com
* flash by 'freepik' http://www.flaticon.com/packs/photography-skills
* speaker by 'Madebyoliver' http://www.flaticon.com/packs/essential-set-2
* sun/moon by 'Vectors Market' http://www.flaticon.com/packs/weather-elements
*/
// Server in Variablen bereitstellen
var express = require('express'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server),
conf = require('./config.json'),
fs = require("fs"),
helmet = require("helmet"),
compression = require("compression"),
aktiveNutzer = 0,
connections = [],
data = fs.readFileSync("quotes", "utf8"),
quotefile = data.toString(),
quotes = quotefile.split("\n");
server.listen(conf.port);
// Express konfigurieren, die Dateien aus public anzuzeigen
app.use(helmet());
app.use(helmet.noCache());
app.use(compression());
app.set("trust proxy", 1);
app.use(express.static(__dirname + '/public'));
// Gibt index.html zurück, wenn / aufgerufen wird
app.get('/', function (req, res) {
//console.log(req.ip);
//console.log(req.ips);
res.sendFile(__dirname + '/public/index.html');
});
// # # # F U N K T I O N E N # # #
function Item(socketId, socketIp, username) {
this.socketId = socketId;
this.socketIp = socketIp;
this.username = username;
}
function addIpToId(socketId, socketIp) {
for (var i = 0; i < connections.length; i++) {
if (connections[i].socketId == socketId) {
connections[i].socketIp = socketIp;
}
}
}
function addNameToId(socketId, username) {
for (var i = 0; i < connections.length; i++) {
if (connections[i].socketId == socketId) {
connections[i].username = username;
}
}
}
function deleteSocket(IdToDelete) {
for (var i = 0; i < connections.length; i++) {
if (connections[i].socketId == IdToDelete) {
connections.splice(i);
}
}
}
function getUserName(name) {
if (name === "") {
return "n/a";
} else {
return name;
}
}
function getUserIp(ip) {
if (ip === "") {
return "n/a";
} else {
return ip;
}
}
function getIpFromSocketId(socketId) {
for (var i = 0; i < connections.length; i++) {
if (connections[i].socketId == socketId) {
return connections[i].socketIp;
}
}
}
function listAllUserString() {
var stringTemp = "";
for (var i = 0; i < connections.length; i++) {
stringTemp += getUserName(connections[i].username) + " (" + getUserIp(connections[i].socketIp) + "); ";
}
return aktiveNutzer + " Nutzer: " + stringTemp;
}
function timestamp() {
function pad(n) {
return n < 10 ? "0" + n : n;
}
var d = new Date();
var dash = "-";
var punkt = ":";
var leer = " ";
return d.getFullYear() + dash +
pad(d.getMonth() + 1) + dash +
pad(d.getDate()) + leer +
pad(d.getHours()) + punkt +
pad(d.getMinutes()) + punkt +
pad(d.getSeconds());
}
function niceTimestamp() {
var time = new Date();
time = time.toString();
time = time.substring(25, 39);
return timestamp() + " " + time;
}
function quoteDay() {
var now = new Date();
var start = new Date(now.getFullYear(), 0, 0);
var diff = now - start;
var oneDay = 1000 * 60 * 60 * 24;
var day = Math.floor(diff / oneDay);
return quotes[day % quotes.length];
}
function quoteRandom() {
var now = new Date();
return quotes[now.getMilliseconds() % quotes.length];
}
function stringMaxLength(string, lengz) {
if(string.length > lengz) {
return string.substring(0, lengz);
} else {
return string;
}
}
// # # # S O C K E T # # #
// Socket Konfiguration
io.sockets.on('connection', function (socket) {
// Sobald ein Client neu per Socket verbunden ist
connections.push(new Item(socket.id, "", ""));
addIpToId(socket.id, (socket.handshake.headers['x-forwarded-for'] || socket.request.connection.remoteAddress));
//console.log(connections);
var derzeitigeSocketId = socket.id;
aktiveNutzer++;
console.log("+ " + (socket.handshake.headers['x-forwarded-for'] || socket.request.connection.remoteAddress) + " " + socket.id + " (" + aktiveNutzer + ") " + niceTimestamp());
socket.emit('chat', {
zeit: new Date(),
text: "Willkommen im Node.js Chat",
bold: true
});
socket.emit('chat', {
zeit: new Date(),
text: quoteDay()
});
io.sockets.emit("chat", {
zeit: new Date(),
text: getIpFromSocketId(socket.id) + " hat den Chat betreten (" + aktiveNutzer + ((aktiveNutzer > 1) ? " aktive Nutzer)." : " aktiver Nutzer).")
});
// Sobald eine Client Verbindung beendet wurde
socket.on("disconnect", function () {
aktiveNutzer--;
console.log("- " + (socket.handshake.headers['x-forwarded-for'] || socket.request.connection.remoteAddress) + " " + socket.id + " (" + aktiveNutzer + ") " + niceTimestamp());
io.sockets.emit("chat", {
zeit: new Date(),
text: getIpFromSocketId(derzeitigeSocketId) + " hat den Chat verlassen. (" + aktiveNutzer + ((aktiveNutzer > 1) ? " aktive Nutzer)." : " aktiver Nutzer).")
});
deleteSocket(derzeitigeSocketId);
});
// Sobald der Benutzer einen Text sendet
socket.on('chat', function (data) {
addNameToId(socket.id, data.name);
addIpToId(socket.id, (socket.handshake.headers['x-forwarded-for'] || socket.request.connection.remoteAddress));
// Der Text wird an alle verbundenen Sockets gesendet
if (data.text.length > 0 && data.text != " ") {
var realText = stringMaxLength(data.text, 1000);
var realName = stringMaxLength(data.name, 22);
if (data.text.search("/bold") === 0) {
realText = realText.substring(5, realText.length);
io.sockets.emit("chat", {
zeit: new Date(),
name: realName + " (" + (socket.handshake.headers['x-forwarded-for'] || socket.request.connection.remoteAddress) + ")",
text: realText,
bold: true
});
} else {
io.sockets.emit('chat', {
zeit: new Date(),
name: realName + " (" + (socket.handshake.headers['x-forwarded-for'] || socket.request.connection.remoteAddress) + ")",
text: realText,
});
}
if (data.text == "/who") {
console.log(connections);
io.sockets.emit('chat', {
zeit: new Date(),
text: listAllUserString()
});
}
if (data.text == "/quote") {
io.sockets.emit("chat", {
zeit: new Date(),
text: quoteRandom()
});
}
}
});
});
console.log('Der Server läuft unter localhost:' + conf.port);