-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
147 lines (128 loc) · 3.97 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
"use strict";
const PORT = process.env.PORT || 3000;
import express from 'express';
import path from 'path';
import moment from 'moment';
import htmlencode from 'htmlencode';
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
app.use(express.static('public'));
const clientInfo = {};
const timestamp = moment.utc.valueOf();
const sendCurrentUsers = (socket) => {
const info = clientInfo[socket.id];
const users = [];
if (typeof info === 'undefined') {
return;
}
Object.keys(clientInfo).forEach((socketId) => {
let userInfo = clientInfo[socketId];
if (userInfo.room === info.room) {
users.push(userInfo.name);
}
});
return users;
};
/**
* @param {string} name
* The username, if spaces, username be a plus to separate the spaces.
*/
const getSocketIdByName = (name) => {
name = name.replace('+', ' ');
for (let socketId of Object.keys(clientInfo)) {
if (clientInfo[socketId].name.toLowerCase().trim() === name.toLowerCase()) {
return socketId;
break;
}
};
return false;
};
const getHelpCommand = () => {
return [
`<ul>`,
`<li><strong>#currentUsers</strong> - Display all the current users.</li>`,
`<li><strong>@[username] message</strong> - where [username] is the person you want to private message.</li>`,
`</ul>`
].join('');
};
io.on('connection', (socket) => {
socket.on('disconnect', () => {
const userData = clientInfo[socket.id];
if (typeof clientInfo[socket.id] !== 'undefined') {
socket.leave(userData.room);
io.to(userData.room).emit('message', {
timestamp,
name: 'System',
text: `${userData.name} has left the room!`,
});
delete clientInfo[socket.id];
}
});
socket.on('joinRoom', (request) => {
request.room = request.room.toLowerCase();
socket.join(request.room);
socket.broadcast.to(request.room).emit('message', {
timestamp,
name: 'System',
text: `${request.name} has join!`,
});
clientInfo[socket.id] = request;
});
socket.on('message', (message) => {
console.log(`Message received: ${message.text}`);
message.timestamp = timestamp;
const firstChr = message.text.substring(0, 1);
// Check if the code is running a command
if (firstChr === '@' || firstChr === '#') {
// Only separate the first space. Second array can contain many spaces.
const command = message.text.split(/ (.+)/, 2);
switch(command[0]) {
case '#help':
const helpCommand = getHelpCommand();
socket.emit('message', {
timestamp,
name: 'System',
text: helpCommand,
});
break;
// Get current user list.
case '#currentUsers':
let currentUsers = sendCurrentUsers(socket);
socket.emit('message', {
timestamp,
name: 'System',
text: `Current users: ${currentUsers.join(', ')}`,
});
break;
// TODO: Instead of doing pm, it's better to use @user and #command
// Private message a current user in any room.
default:
if (firstChr === '@' &&
typeof command[1] !== 'undefined'
&& command[1] !== '') {
// Grab the socket ID of the user when using @username
let socketId = getSocketIdByName(command[0].substring(1).trim());
if (socketId) {
socket.to(socketId).emit('message', {
timestamp,
name: message.name,
text: `Private: ${htmlencode.htmlEncode(command[1])}`,
});
}
}
break;
}
} else {
message.text = htmlencode.htmlEncode(message.text);
socket.broadcast.to(clientInfo[socket.id].room).emit('message', message);
}
});
socket.emit('message', {
timestamp,
text: 'Welcome to the chat application!',
});
});
http.listen(PORT, () => {
console.log('Server started!');
});