-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
183 lines (168 loc) · 6.11 KB
/
index.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
const express = require('express');
const cookieParser = require('cookie-parser');
const session = require('express-session');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var totalUserCount = 0;
var users = []; // Online users
var allUsers = []; // All users
var chatLog = [];
app.use(express.static('public'));
app.use(cookieParser());
function findUserBySocket(socketid) {
for(i=0; i<users.length; i++){
if(users[i].id === socketid){
return i;
}
}
return -1;
}
function findUserByName(name) {
for(i=0; i<users.length; i++) {
if(users[i].username === name) {
return i;
}
}
return -1;
}
function isUniqueName(name) {
for(i=0; i<allUsers.length; i++){
if(name === allUsers[i].username){
return false;
}
}
return true;
}
function updateAllUserByName(oldName, newName){
for(i=0; i<allUsers.length; i++){
if(allUsers[i].username === oldName){
allUsers[i].username = newName;
return;
}
}
}
function updateAllUserByColor(name, newColor){
for(i=0; i<allUsers.length; i++){
if(allUsers[i].username === name){
allUsers[i].color = newColor;
return;
}
}
}
io.on('connection', function(socket) { // A new connection to the server
var new_user = {};
socket.on('get cookie', function(username, color, fn){
// Check the cookie from client
if(!username){ // create a new user name
// Assign color and name
let rdmcolor = "hsl(" + (totalUserCount * 77 % 360) + ",100%,50%)";
new_user.color = rdmcolor;
totalUserCount ++;
let nickname = "User#" + totalUserCount.toString();
new_user.username = nickname;
// console.log("userCookie undefined.");
}else {
new_user.username = username;
new_user.color = color;
}
fn({n: new_user.username, c: new_user.color});
new_user.id = socket.id;
users.push(new_user);
allUsers.push(new_user);
console.log(new_user.username + " has connected");
socket.emit('assign nickname', {username: new_user.username, color: new_user.color});
io.emit('update user list', users);
// Send chat log to new user
socket.emit('new chatLog', chatLog);
});
// Server received new message from clients
socket.on('chat message', function (data) {
if (data.msg.trim().length === 0) return; // Don't send empty msg
let timestamp = Date.now(); // get the timestamp when received new msg
//let user_index = findUserBySocket(socket.id);
let user_index = findUserByName(data.username);
let user = users[user_index];
// Check if it's /nick or /nickcolor
let command = data.msg.split(" ");
if (command.length === 2 && command[0] === "/nick") {
if (command[1].trim().length === 0) { // Invalid empty name
socket.emit('command reply', {time: timestamp, msg: "[Error] Nickname cannot be empty."});
return;
}
// Check if the new name is unique
if (!isUniqueName(command[1])) {
socket.emit('command reply', {
time: timestamp,
msg: "[Error] Nickname \"" + command[1] + "\" has been taken."
});
return;
}
// Change the name
let oldname = users[user_index].username;
users[user_index].username = command[1];
socket.emit('assign nickname', {username: command[1], color: users[user_index].color});
// Update user list
io.emit('update user list', users);
// Update allUser[]
updateAllUserByName(oldname, command[1]);
// Update chatLog
for (i = 0; i < chatLog.length; i++) {
if (chatLog[i].username === oldname) {
chatLog[i].username = command[1];
}
}
io.emit('update chatLog', chatLog);
// Emit success message
socket.emit('command reply', {time: timestamp, msg: "[Done] Successfully changed nickname."});
}
else if (command.length === 2 && command[0] === "/nickcolor") {
if (command[1].length !== 6 || parseInt("0x" + command[1]) > 0xFFFFFF) {
socket.emit('command reply', {
time: timestamp,
msg: "[Error] Invalid Hex code. Color should be RRGGBB."
});
return;
}
// Change the color
let newColor = "#" + command[1];
users[user_index].color = newColor;
socket.emit('assign nickname', {username: user.username, color: newColor});
// Update user list
io.emit('update user list', users);
// Update allUser[]
updateAllUserByColor(user.name, newColor);
// Update chatLog
for (i = 0; i < chatLog.length; i++) {
if (chatLog[i].username === user.username) {
chatLog[i].color = newColor;
}
}
io.emit('update chatLog', chatLog);
// Emit success message
socket.emit('command reply', {time: timestamp, msg: "[Done] Successfully changed color."});
}
else { // regular message
let msgWrap = {
time: timestamp,
msg: data.msg,
username: user.username,
color: user.color
};
chatLog.push(msgWrap);
io.emit('new message', msgWrap);
}
});
// User disconnected
socket.on('disconnect', function (){
let index = findUserBySocket(socket.id);
if(index !== -1){
console.log(users[index].username + ' has disconnected');
users.splice(index, 1);
io.emit('update user list', users);
}
});
});
http.listen(3000, function(){
console.log('listening on *:3000');
});