forked from rauchg/chat-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
115 lines (90 loc) · 3.45 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
var express = require('express');
var cookieParser = require('cookie-parser');
var app = express();
var http = require('http').createServer(app);
var io = require('socket.io')(http);
var port = process.env.PORT || 3000;
var CircularBuffer = require("circular-buffer");
var dateFormat = require('dateformat');
// Buffer for the messages
var messageBuffer = new CircularBuffer(250);
var uniqueUsers = [];
var onlineUsers = [];
http.listen( port, function () {
console.log('listening on port', port);
});
app.use(cookieParser());
// Generate username / color on connection if needed
app.get('/', function(req, res, next) {
if (req.cookies.user === '' || req.cookies.user === undefined) {
res.cookie('user', 'User' + uniqueUsers.length);
}
if (req.cookies.color == '' || req.cookies.color === undefined) {
res.cookie('color', '#ffffff');
}
next();
});
app.use(express.static(__dirname + '/public'));
// listen to 'chat' messages
io.on('connection', function(socket) {
// Emit the chatlog when a user first connects
socket.emit('chatlog', messageBuffer.toarray());
socket.on('chat', function(msg, name, color){
let changeColorCommand = "/nickcolor";
let changeNameCommand = "/nick";
let splitMsg = msg.split(" ");
if (changeColorCommand === splitMsg[0]) {
let newColor = splitMsg[1];
//If this is a valid hex color
if (/^[0-9A-F]{6}$/i.test(newColor)) {
socket.emit('recolor', '#'+ newColor);
} else {
socket.emit('errorMsg', newColor + ' is not a valid color! Please enter a color of the form RRGGBB');
}
} else if (changeNameCommand === splitMsg[0]) {
let newName = splitMsg[1];
let newNamePos = uniqueUsers.indexOf(newName);
// Update lists and rename the user if this is an unused name.
if (-1 === newNamePos) {
uniqueUsers.splice(uniqueUsers.indexOf(socket.user), 1);
uniqueUsers.push(newName);
onlineUsers.splice(onlineUsers.indexOf(socket.user), 1);
onlineUsers.push(newName);
socket.emit('rename', newName);
io.emit('userlistChange', onlineUsers);
socket.user = newName;
// Otherwise alert the user that this name is taken.
} else {
socket.emit('errorMsg', 'The nickname ' + newName + ' is already taken!');
}
} else {
let msgData = { time: getTimeString(), color: color, user: socket.user, msg: msg };
messageBuffer.push(msgData);
io.emit('chat', msgData);
}
});
// Set the username of this socket.
socket.on('username', function(name) {
socket.user = name;
socket.emit('rename', name);
let namePos = uniqueUsers.indexOf(name);
if (-1 === namePos) {
uniqueUsers.push(name);
}
onlineUsers.push(name);
io.emit('userlistChange', onlineUsers);
});
// Remove user from online user list and update clients.
socket.on('disconnect', function() {
let userIdx = onlineUsers.indexOf(socket.user);
if (-1 != userIdx) {
onlineUsers.splice(userIdx, 1);
}
io.emit('userlistChange', onlineUsers);
});
});
// Returns the current time
function getTimeString() {
let now = new Date();
return dateFormat(now, 'h:MM TT');
}