-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
70 lines (53 loc) · 1.65 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
const express = require('express');
const app = express();
const http = require('http').createServer(app);
const io = require('socket.io')(http);
app.use(express.static(__dirname + '/public'));
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
const port = process.env.PORT || 3000;
http.listen(port, () => {
console.log(`Listening on *:${port}`);
});
const username = {};
function getRandomNumber(min, max)
{
var n = Math.floor(Math.random() * (max - min) + min); // Random no b/w 0 to 16
return n;
}
function getRandomColor()
{
var characters = '0123456789ABCDEF';
var color = '#';
for(var i = 0; i < 6; i++)
{
color += characters[getRandomNumber(0, 15)];
}
return color;
}
const userColor = {};
io.on('connection', (socket) => {
socket.on('user-connected', (name) => {
username[socket.id] = name;
userColor[socket.id] = getRandomColor();
var message = ' joined the chat';
io.emit('show-status', message, name);
});
// Server will send message to all other users except the one who has sent the message.
socket.on('message', (data) => {
socket.broadcast.emit('message', data, userColor[socket.id]);
});
socket.on('disconnect', () => {
var message = ' left the chat';
io.emit('show-status', message, username[socket.id]);
delete username[socket.id];
delete userColor[socket.id];
});
socket.on('typing', () => {
socket.broadcast.emit('show-typing', username[socket.id]);
});
socket.on('stopped-typing', () => {
socket.broadcast.emit('hide-typing');
});
});