forked from sdciitmandi/campus-chat-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
283 lines (222 loc) · 8.89 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
var compression = require("compression");
var express = require('express');
var app = express();
var path = require("path");
// Compress the responses delivered to save bandwidth
app.use(compression());
//Express does not deliver anything beyond route
// So need to set it up to use the public directory for static content
app.use(express.static(path.join(__dirname, 'public')));
var http = require('http');
var httpServer = http.Server(app);
var socketIO = require('socket.io');
var io = socketIO(httpServer);
var freeUsers = []; // IDs of the users who are conncected and free
idmap = {}; // Map containing ids of sockets chatting with each other. Map is symmetric
var usernames = {};// usernames which are currently connected to the chat
var rooms = {};// rooms which are currently available in chat
var userstatus = {}; //current status of users, either they are single or in some room
var welcomeMessage = "You are chatting with a random stranger, Say Hi!\n";
var disconnectMessage = "Stranger has left. Please Reload this page or wait for us to connect you with someone.\n";
app.get('/', function(req, res) {
res.sendFile(__dirname + '/public/index.html');
});
io.sockets.on('connection', function(socket){
//console.log('New user joined' + socket.id);
//asks name to the user
socket.emit('askname');
socket.on('changename', function(newname){
if(usernames[newname]!=undefined){
socket.emit('alert', 'username already exists');
return ;
}
var oldname=socket.username;
socket.username=newname;
delete usernames[oldname];
usernames[newname]=newname;
if(userstatus[socket.id]== "free"){
if(idmap[socket.id]) io.to(idmap[socket.id]).emit('updatechat','SERVER' ,oldname+' has changed his name to '+newname);
io.to(socket.id).emit('updatechat','SERVER' ,'You have changed your name to '+newname);}
else if(userstatus[socket.id] =='member'){
socket.broadcast.to(socket.room).emit('updatechat', 'SERVER', oldname+' has changed his name to '+newname);
socket.emit('updatechat', 'SERVER', 'You have changed your name to '+newname);
}
});
// when the user emits 'adduser', this listens and executes
socket.on('adduser', function(username){
// store the username in the socket session for this client
if(usernames[username]!=undefined){
socket.emit('tryagain','username exits..Try another one');
}
else {
// add the client's username to the global list
usernames[username] = username;
socket.username = username;
// console.log('\n' + usernames[username]);
}
// console.log('\n' + socket.username);
// store the room name in the socket session for this client
userstatus[socket.id]='free';
if(freeUsers.length && socket.username!=undefined) { // Chechk if there are some free users
var st1 = freeUsers.shift(); // Remove the user who has been waiting longest
idmap[socket.id] = st1;
idmap[st1] = socket.id;
console.log('Connected' + socket.id + ' to ' + st1);
io.to(socket.id).emit('welcome message',welcomeMessage);
io.to(st1).emit('welcome message',welcomeMessage);
}
else if(socket.username!= undefined){
freeUsers.push(socket.id); // Else push the user in the queue for later processing
}
socket.emit('updaterooms',rooms);
// send client to room 1
//socket.join('room1');
// echo to client they've connected
//socket.emit('updatechat', 'SERVER', 'you have connected to room1');
// echo to room 1 that a person has connected to their room
//socket.broadcast.to('room1').emit('updatechat', 'SERVER', username + ' has connected to this room');
});
socket.on('changeroom', function(newroom){
console.log('hello1\n');
//socket.broadcast.to(newroom).emit('updatechat', 'SERVER', socket.username+' has joined this room');
if(userstatus[socket.id]== "member"){
var room_count=Object.keys(io.sockets.adapter.rooms[socket.room]).length;
socket.leave(socket.room);
rooms[socket.room]=room_count-1;
console.log('hello2\n');
// sent message to OLD room
socket.broadcast.to(socket.room).emit('updatechat', 'SERVER', socket.username+' has left this room');
}
else{
var st2 = idmap[socket.id];
if(st2) {
io.to(st2).emit('disconnect message',disconnectMessage);
delete idmap[socket.id];
delete idmap[st2];
console.log('hello3\n');
//Map the stranger to another user if there exists some free user
if(freeUsers.length) {
var st3 = freeUsers.shift();
idmap[st2] = st3;
idmap[st3] = st2;
io.to(st2).emit('welcome message',welcomeMessage);
io.to(st3).emit('welcome message',welcomeMessage);
}
else freeUsers.push(st2);
}
else freeUsers.shift();
}
socket.join(newroom);
rooms[newroom]=Object.keys(io.sockets.adapter.rooms[newroom]).length;
userstatus[socket.id]='member';
socket.emit('updatechat', 'SERVER', 'you have connected to '+ newroom);
// update socket session room title
socket.room = newroom;
socket.broadcast.to(newroom).emit('updatechat', 'SERVER', socket.username+' has joined this room');
socket.broadcast.emit('updaterooms',rooms);
socket.emit('updaterooms', rooms);
});
socket.on('createroom',function(roomname){
if(userstatus[socket.id] == 'free'){
var st2 = idmap[socket.id];
if(st2) {
io.to(st2).emit('disconnect message',disconnectMessage);
delete idmap[socket.id];
delete idmap[st2];
//Map the stranger to another user if there exists some free user
if(freeUsers.length) {
var st3 = freeUsers.shift();
idmap[st2] = st3;
idmap[st3] = st2;
console.log('hello3\n');
io.to(st2).emit('welcome message',welcomeMessage);
io.to(st3).emit('welcome message',welcomeMessage);
}
else freeUsers.push(st2);
//console.log('hello4\n');
}
else{freeUsers.shift();}
}
else if(userstatus[socket.id] == 'member'){
var room_count=Object.keys(io.sockets.adapter.rooms[socket.room]).length;
socket.leave(socket.room);
rooms[socket.room]=room_count-1;
socket.broadcast.to(socket.room).emit('updatechat', 'SERVER', socket.username+' has left this room');
}
console.log('\n'+roomname+' '+ socket.username);
socket.join(roomname);
rooms[roomname]=Object.keys(io.sockets.adapter.rooms[roomname]).length;
socket.room = roomname;
userstatus[socket.id]="member";
socket.emit('updatechat', 'SERVER', 'you have connected to '+roomname);
socket.broadcast.emit('updaterooms',rooms);
socket.emit('updaterooms',rooms);
/*for(var i=0; i<rooms.length;++i){
console.log('\n'+rooms[i]);
socket.broadcast.to(rooms[i]).emit('updaterooms',rooms,rooms[i]);
}*/
});
// when the client emits 'sendchat', this listens and executes
socket.on('sendchat', function (data) {
// we tell the client to execute 'updatechat' with 2 parameters
if(userstatus[socket.id]!= "free"){
io.sockets.in(socket.room).emit('updatechat', socket.username, data);}
else{
if(idmap[socket.id]) io.to(idmap[socket.id]).emit('updatechat',socket.username ,data);
io.to(socket.id).emit('updatechat','YOU' ,data);
}
});
socket.on('user image', function (data) {
// console.log(msg);
if(userstatus[socket.id]!= "free"){
io.sockets.in(socket.room).emit('userimage', socket.username, data);}
else{
if(idmap[socket.id]) io.to(idmap[socket.id]).emit('userimage',socket.username ,data);
io.to(socket.id).emit('userimage','YOU' ,data);
}
// socket.broadcast.emit('user image', socket.username, msg);
});
//Handling Code when a User disconnects
socket.on('disconnect', function() {
if(userstatus[socket.id] == "member"){
// remove the username from global usernames list
delete usernames[socket.username];
// update list of users in chat, client-side
io.sockets.emit('updateusers', usernames);
// echo globally that this client has left
socket.broadcast.emit('updatechat', 'SERVER', socket.username + ' has disconnected');
socket.leave(socket.room);
//socket.leave(socket.room);
rooms[socket.room]-=1;
socket.broadcast.emit('updaterooms',rooms);
}
else if(userstatus[socket.id] == "free"){
var st2 = idmap[socket.id];
if(st2) {
io.to(st2).emit('disconnect message',disconnectMessage);
delete idmap[socket.id];
delete idmap[st2];
//Map the stranger to another user if there exists some free user
if(freeUsers.length) {
var st3 = freeUsers.shift();
idmap[st2] = st3;
idmap[st3] = st2;
io.to(st2).emit('welcome message',welcomeMessage);
io.to(st3).emit('welcome message',welcomeMessage);
}
else freeUsers.push(st2);
}
}
});
//When message comes , send it to the id mapped to socket's
/* socket.on('chat message', function(msg){
console.log('message: ' + msg);
if(idmap[socket.id]) io.to(idmap[socket.id]).emit('chat message', msg);
console.log(idmap);
});*/
});
var deployPort = process.env.PORT || 3000;
httpServer.listen(deployPort, function() {
console.log('Listening on *:' + deployPort);
});
//var room_count = Object.keys(io.sockets.adapter.rooms[roomname]).length;