forked from KamandPrompt/baat-cheet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
184 lines (151 loc) · 5.01 KB
/
app.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
//setup basic express server
var express = require('express')
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var path = require('path');
var index = require('./serve/index.js');
var port = process.env.PORT || 3000;
http.listen(port,function(){
console.log("Listening",http.address().port);
});
app.use(express.static(__dirname + '/public'));
app.set('view engine', 'ejs');
app.set('view options', {
layout: false
});
//routing
app.use('/',index);
app.use(function(err,req,res,next){
console.log(err.stack);
res.status(500);
});
//users online
var users = [];
//list of rooms and number of users in particular room (default: lobby)
var rooms = [{name: 'lobby', description: 'Central Lobby', num_users: 0}];
io.on('connection', function(socket) {
//When client requests for setting username
socket.on('set username', function(name) {
name = name.trim()
//if name is empty(null), do nothing
if(name == "" || name == null) {
return;
}
//if username is not taken
else if(users.indexOf(name) == -1) {
users.push(name);
//username is valid so user is set
socket.emit('user set', {username: name, online: rooms[0].num_users + 1});
//by default, user joins lobby
socket.join('lobby');
rooms[0].num_users++;
//notify all users (except sender) that user joined
socket.broadcast.emit('user joined', {username: name, online: rooms[0].num_users});
socket.username = name;
for (var i = 1; i < rooms.length; i++) {
socket.emit('room created other', {room_name: rooms[i].name, description: rooms[i].description, online: rooms[i].num_users});
}
}
//if username is taken
else {
socket.emit('user exists', name);
}
});
//When client sends message
socket.on('Message Request', function(data){
//if message is valid
if(data.msg) {
//display message to all clients in room including sender
io.sockets["in"](data.room).emit('Display Message', {msg: data.msg, user: socket.username, room: data.room});
}
});
//When client creates room
socket.on('create room', function(data) {
data.room_name = data.room_name.trim();
//if room name is empty, do nothing
if(data.room_name == null) {
return;
}
var num_rooms = rooms.length
//check if room exists
for(var i = 0; i<num_rooms; ++i) {
//if room name is taken
if(rooms[i].name == data.room_name) {
socket.emit('room exists', data.room_name);
return;
}
}
//room not taken so insert into room array
rooms.push({name: data.room_name, description: data.description, num_users: 1});
socket.join(data.room_name);
socket.emit('room created self', {room_name: data.room_name, description: data.description, online: 1});
socket.broadcast.emit('room created other', {room_name: data.room_name, description: data.description});
});
//When user requests to join the room
socket.on('join room', function(room) {
socket.join(room.name);
var num_rooms = rooms.length;
var num_users;
//update number of users in room
for(var i = 0; i < num_rooms; i++) {
if(room.name == rooms[i].name) {
rooms[i].num_users++;
num_users = rooms[i].num_users;
break;
}
}
//update the user's info
socket.emit('room joined', {name: room.name, online: num_users});
//notify other users in room that someone joined
socket["to"](room.name).broadcast.emit('user join', {username: socket.username, room: room.name, online: num_users});
});
//When user requests to leave the room
socket.on('leave room', function(room) {
socket.leave(room.name);
var num_rooms = rooms.length;
var num_users;
//update number of users in room
for(var i = 0; i < num_rooms; i++) {
if(room.name == rooms[i].name) {
rooms[i].num_users--;
num_users = rooms[i].num_users;
//if users become 0, destroy/delete the room
if(rooms[i].num_users == 0) {
io.sockets.emit('destroy room', room.name);
rooms.splice(i, 1);
return;
}
break;
}
}
//notify other users in room that someone left
socket["to"](room.name).broadcast.emit('user left room', {username: socket.username, room: room.name, online: num_users});
});
//When user disconnets remove user from users
socket.on('disconnecting', function() {
var num_rooms = rooms.length;
//update number of users in rooms
for(var i = 0; i < num_rooms; i++) {
if(socket.rooms[rooms[i].name]) {
rooms[i].num_users--;
//notify other users in room that user left
//socket["to"](rooms[i].name).broadcast.emit('user left room', {username: socket.username, room: rooms[i].name});
//if number of users become 0 in some room, destroy/delete that room
if(rooms[i].num_users == 0 && rooms[i].name != 'lobby') {
io.sockets.emit('destroy room', rooms[i].name);
rooms.splice(i, 1);
i--;
num_rooms--;
}
}
}
io.sockets.emit('user left', {username: socket.username});
io.sockets.emit('update info', rooms);
//remove username from users array
var index = users.indexOf(socket.username);
if(index != -1) {
users.splice(index, 1);
}
});
});