forked from Akhu/RemoteDrawingSocket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
243 lines (208 loc) · 7.3 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
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
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var colors = ["#424242","#E53935","#8E24AA","#D81B60","#00897B","#FDD835","#039BE5","#E91E63","#2196F3","#3F51B5","#4CAF50","#FFC107","#FF9800","#FFEB3B","#A282B5","#81E7F9","#E0EF53"];
var peoples = new Array();
var roomName = new String();
var numberOfUsers = 0;
var numberOfRooms = 0;
var userExists = false;
var MongoClient = require('mongodb').MongoClient
, assert = require('assert');
// Connection URL
var url = 'mongodb://localhost:27017/data';
var myDb;
//Connect to Mongo database
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected successfully to database server");
myDb = db;
//ExpressJS
//app.use(express.static('public'));
app.get('/', function(req, res){
res.sendFile('/home/olga/Documents/dev/java_android/LearningProject/RemoteSocket/webinterface.html');
});
//Socket.io
http.listen(3000, function(){
console.log('listening on *:3000');
});
io.on('connection', function(socket){
console.log('A user '+socket.id+' is connected');
numberOfUsers++;
console.log(numberOfUsers);
io.emit("dataUsers", numberOfUsers);
io.emit("dataRooms", numberOfRooms);
// Attributes the color, joins the room
socket.on("joinRoom",function(data){
var id = socket.id;
var color = colors[Math.floor(Math.random()*17)];
var roomName = data.room;
var pseudo = data.name;
var thisUser = {
"id" : id,
"pseudo" : pseudo,
"color" : color,
"room": roomName
};
console.log(thisUser);
socket.join(roomName);
var l = arrayObjectIndexOf(peoples, id, "id");
peoples[l].color = color;
for(var i = 0, len = peoples.length; i < len ; i++) {
if (roomName === peoples[i].room) {
console.log("Room exists already");
} else {
numberOfRooms++;
}
peoples[l].room = roomName;
}
console.log("Number of Rooms " + numberOfRooms);
socket.emit("me", thisUser);
io.emit("dataRooms", numberOfRooms);
myDb.collection('Users').drop (function(err, result) {
console.log("Collection removed");
});
return peoples;
});
// Leave room
socket.on ("leave", function(){
console.log(peoples);
// Index of the current Socket Id
var l = arrayObjectIndexOf(peoples, socket.id, "id");
var roomName = peoples[l].room;
socket.leave(roomName);
peoples[l].room = null;
console.log ('User ' + socket.id + ' left the room ' + roomName);
// Index of User(s) from the same room as the leaving User
var n = arrayObjectIndexOf(peoples, roomName, "room");
// No more Users left in this Room
if (n == -1) {
numberOfRooms--;
}
console.log("Number of Rooms " + numberOfRooms);
io.emit("dataRooms", numberOfRooms);
return peoples;
});
//Log in
socket.on("login", function(data){
var id = socket.id;
var pseudo = data.pseudo;
// Checking if pseudo already exists
var n = arrayObjectIndexOf(peoples, data.pseudo, "pseudo");
if (n == -1) {
//Commit new user
userExists = false;
peoples.push({ "id" : id, "pseudo" : pseudo, "color" : null, "room" : null });
console.log(peoples);
if (peoples.length === 1) {
console.log(peoples.length + ' user connected');
} else {
console.log(peoples.length + ' users connected');
}
} else {userExists = true}
// Pseudo exists, have to choose another pseudo
var exists = {
userExists: userExists };
io.emit("userExists", exists);
return peoples;
});
//Clear all drawings in particular room
socket.on ("clearRoom", function(data){
var roomName = data.room;
io.in(roomName).emit("clearRoom");
});
//Clear all drawings
socket.on("clear", function(){
io.emit("clear");
})
//Transfer coordinates
socket.on('drawing', function(coordinates){
var n = arrayObjectIndexOf(peoples, socket.id, "id");
var pseudo = peoples[n].pseudo;
var room = peoples[n].room;
var color = peoples[n].color;
var objectToSend = {
"coordinates" : coordinates,
"drawer" : pseudo,
"color" : color
};
io.in(room).emit("receiveDrawing", objectToSend);
console.log(objectToSend);
//Send the drawing to the database
myDb.collection('Users').insert(
{ userId : peoples[n].id, pseudo : peoples[n].pseudo, color: peoples[n].color, room: peoples[n].room,
coordinates_old : coordinates.old, coordinates_new : coordinates.new}, function(err, result) {
assert.equal(err, null);
//console.log("Inserted coordinates information for " + peoples[n].id);
});
});
//Change the color, check if it the background color, send the msg "You are Eraser"
socket.on('colorChanging', function(backgrouncolor){
var newColor = colors[Math.floor(Math.random()*17)];
var msg = false;
var n = arrayObjectIndexOf(peoples, socket.id, "id");
console.log(newColor);
if (newColor === backgrouncolor.color) {
// Check if there is Eraser in the room, if yes - attribute new color
var l = arrayObjectIndexOf(peoples, newColor, "color");
console.log(l);
msg = true;
while (l>=0 && newColor === backgrouncolor.color) {
newColor = colors[Math.floor(Math.random()*17)];
l = arrayObjectIndexOf(peoples, newColor, "color");
msg = false;
}
}
var newColorObject = {
"color" : newColor,
"eraser" : msg
};
peoples[n].color = newColor;
console.log(newColorObject);
socket.emit("newColor", newColorObject);
return peoples;
});
// Change the background color
socket.on('backgroundChange', function(backgrouncolor){
var newColor = colors[Math.floor(Math.random()*17)];
var roomName = backgrouncolor.room;
while (newColor === backgrouncolor.color) {
newColor = colors[Math.floor(Math.random()*17)];
}
var newColorObject = {
"color" : newColor,
};
io.in(roomName).emit("newBackground", newColorObject);
});
// Disconnect
socket.on("disconnect", function(){
console.log("bye bye "+socket.id);
var l = arrayObjectIndexOf(peoples, socket.id, "id");
var room = peoples[l].room;
peoples.splice(l, 1);
var n = arrayObjectIndexOf(peoples, room, "room");
if (n == -1) {
numberOfRooms--;
console.log("number Of Rooms " + numberOfRooms);
}
numberOfUsers--;
io.emit("dataUsers", numberOfUsers);
io.emit("dataRooms", numberOfRooms);
myDb.collection('Users').find({}).toArray(function(err, docs) {
assert.equal(err, null);
console.log("Found the following records");
console.log(docs);
});
return peoples;
})
});
});
function arrayObjectIndexOf(myArray, searchTerm, property) {
for(var i = 0, len = myArray.length; i < len; i++) {
//console.log(myArray[i]);
//console.log(myArray[i]);
if (myArray[i][property] === searchTerm) return i;
}
return -1;
}