-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
132 lines (104 loc) · 3.99 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
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
//Enable CORS
// app.use(function(req, res, next) {
// res.header("Access-Control-Allow-Origin", "*");
// res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
// res.header("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE");
// next();
// });
//Store user data in variable room
var rooms = [];
//Get Room id from url
app.get('/:roomName', function(req, res) {
activeChat = req.params.roomName;
res.sendFile(__dirname + '/public/chat.html');
});
app.get('/:roomName/', function(req, res) {
activeChat = req.params.roomName;
res.sendFile(__dirname + '/public/chat.html');
});
app.use(express.static(__dirname + '/public/'));
//Socket.io on connection event
io.on('connection', function(socket) {
/*Check if the socket was already connected previously
.user .room custom properties are added to the on connection.
In cases when server fails and restarts
*/
if (socket.user === undefined || socket.room === undefined)
io.to(socket.id).emit("giveuser");
//User disconnect event listener.
socket.on('disconnect', function() {
/* Code:
Remove user from socket.
Search user in it's room and remove it from user array of the room.
If room is empty, remove the room.
*/
socket.leave(socket.room);
var user_idx = -1;
//Check if user room is lost. Common cause: Server restart.
if (rooms[socket.room] != undefined) {
for (var i = 0; i < rooms[socket.room].user_array.length; i++) {
if (rooms[socket.room].user_array[i][0] === socket.user) {
//Find user in user array. O(length)
user_idx = i;
break;
}
}
//Remove user from user_array.
rooms[socket.room].user_array.splice(user_idx, 1);
if (rooms[socket.room].user_array.length === 0) {
delete rooms[socket.room];
}
//Update people online for other users.
io.sockets.in(socket.room).emit('user_disconnect', socket.user);
console.log(socket.user + " disconnected.");
} else
console.log("Probable Server Restart. Disconnecting user to reconnect. user: %s room: %s", socket.user, socket.room);
});
socket.on('addToRoom', function(roomName) {
/* Code:
Add user to room.
Add custom property .user .room to socket for later identification.
Search if room exists. Add user.
*/
socket.room = roomName.room;
socket.user = roomName.user;
var flag = 0; //NOTE: No race conditions observed now
for (var key in rooms) {
if (key === socket.room) {
flag = 1;
break;
}
}
if (flag === 0) {
rooms[socket.room] = {
name: socket.room,
user_array: []
}
rooms[socket.room].user_array.push([socket.user, socket.id]);
} else {
rooms[socket.room].user_array.push([socket.user, socket.id]);
}
//Add socket to provided room
socket.join(socket.room);
//Send user_connect msg to other users in room.
io.sockets.in(socket.room).emit('user_connect', rooms[socket.room].user_array);
console.log(socket.user + " connected.");
});
//Broadcast users chat message to its room.
socket.on('chatmsg', function(msg) {
socket.broadcast.to(socket.room).emit('chatmsg', {
"user": socket.user,
"msg": msg
});
});
socket.on('usertyping', function(user) {
socket.broadcast.to(socket.room).emit('usertyping', user);
});
});
http.listen((process.env.PORT || 5000), function() {
console.log('Server running');
});