This repository has been archived by the owner on Oct 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
214 lines (197 loc) · 5.54 KB
/
app.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
var app = require('http').createServer(handler)
, io = require('socket.io')(app)
, fs = require('fs')
, path = require('path')
, utils = require('./utils.js');
// we're using herokus PORT or 8888 locally
var port = process.env.PORT || 8888;
app.listen(port);
console.log("Application running on port " + port);
// create FIFO for last sent messages
function handler(req, res) {
let filePath = '.' + req.url;
if (filePath === './') {
filePath = './index.html';
}
// get file extension
var extname = path.extname(filePath);
var contentType = 'text/html;charset=utf-8';
switch (extname) {
case '.js':
contentType = 'text/javascript';
break;
case '.css':
contentType = 'text/css';
break;
case '.json':
contentType = 'application/json';
break;
case '.png':
contentType = 'image/png';
break;
case '.jpg':
contentType = 'image/jpg';
break;
}
fs.readFile(filePath, function(error, content) {
if (error) {
if(error.code === 'ENOENT'){
fs.readFile('./404.html', function(error, content) {
res.writeHead(404, { 'Content-Type': contentType });
res.end(content, 'utf-8');
});
}
else {
res.writeHead(500);
res.end('Sorry, check with the site admin for error: '+error.code+' ..\n');
res.end();
}
}
else {
res.writeHead(200, { 'Content-Type': contentType });
res.end(content, 'utf-8');
}
});
}
var queue = {};
var users = [];
const MESSAGE = 'msg';
const NOTICE = 'notice';
const FAILLOGIN = 'faillogin';
const READY = 'ready';
const ERROR = 'msgerr';
function emit(socket, cmd, data) {
socket.emit(cmd, data);
}
function emitRoom(socket, room, cmd, data) {
socket.broadcast.to(room).emit(cmd, data);
}
function resendQueue(room, socket) {
if (queue[room] && queue[room].length > 0) {
emit(socket, NOTICE, {
timestamp: new Date(),
msg: 'Sending you the last ' + queue[room].length + ' recent messages...'
});
for (let i in queue[room]) {
emit(socket, MESSAGE, queue[room][i]);
}
}
}
function handleCommand(line, socket, time, room) {
// special commands
if (line === '/users') {
emit(socket, NOTICE, {
timestamp: time,
msg: 'Users: ' + utils.escapeHTML(users.join(','))
});
} else if (line === '/rooms') {
const roomlist = [room];
for (let r in Object.entries(io.sockets.adapter.rooms)) {
if (r) {
roomlist.push(r);
}
}
emit(socket, NOTICE, {
timestamp: time,
msg: 'Rooms: ' + utils.escapeHTML(roomlist.join(','))
});
} else if (line.indexOf('/join') === 0) {
// leave current channel
socket.leave(room);
emitRoom(socket, room, NOTICE, {
timestamp: time,
msg: 'User ' + socket.username + ' left this channel.'
});
// join other channel
room = line.match(/\w+$/);
socket.join(room);
emit(socket, NOTICE, {
timestamp: time,
msg: 'You switched to room ' + room
});
emitRoom(socket, room, NOTICE, {
timestamp: time,
msg: 'User ' + socket.username + ' joined this channel',
username: ''
});
resendQueue(room, socket);
} else if (line.indexOf('/help') === 0) {
emit(socket, NOTICE, {
timestamp: time,
msg: 'Available commands:<ul>' +
'<li>/users - show list of users</li>' +
'<li>/rooms - show list of rooms</li>' +
'<li>/join <room> - switch to another chat room</li>' +
'<li>/help - show this help</li>' +
'</ul>'
});
} else {
emit(socket, ERROR, {
timestamp: time,
msg: 'Unknown command. Type /help to see a list of available commands.'
});
}
}
io.sockets.on('connection', function (socket) {
var room = 'Lobby';
socket.on('login', function (data) {
if (data['username'] === '') {
emit(socket, FAILLOGIN, {timestamp: new Date(), users: users, reason: 'No username given'});
} else if (users.includes(data['username'])) {
emit(socket, FAILLOGIN, {timestamp: new Date(), users: users, reason: 'Username already taken'});
} else {
var time = new Date();
socket.join(room);
// store username
socket.username = data['username'];
emitRoom(socket, room, NOTICE, {
timestamp: time,
msg: 'User ' + utils.escapeHTML(data['username']) + ' joined'
});
users.push(data['username']);
emit(socket, READY, {
timestamp: time,
username: utils.escapeHTML(data['username']),
users: utils.escapeHTML(users.join(','))
});
resendQueue(room, socket);
}
});
socket.on('disconnect', function () {
for (let i in users) {
if (users[i] === socket.username) {
users.splice(i, 1);
}
}
const time = new Date();
emitRoom(socket, room, MESSAGE, {
timestamp: time,
source: utils.escapeHTML(socket.username),
msg: 'User left'
});
});
socket.on(MESSAGE, function (data) {
const time = new Date();
if (data['msg'][0] === '/') {
handleCommand(data['msg'], socket, time, room)
} else {
var msg = {
source: utils.escapeHTML(socket.username),
msg: utils.escapeHTML(data['msg']),
timestamp: time
};
emitRoom(socket, room, MESSAGE, msg);
// add message to FIFO
if (!queue[room]) {
queue[room] = [];
}
queue[room].push(msg);
if (queue[room].length > 10) queue[room].shift();
emit(socket, MESSAGE, {
source: 'You',
msg: utils.escapeHTML(data['msg']),
timestamp: time
});
}
});
});