forked from pegnovi/COMP-3203-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsockets.js
More file actions
186 lines (154 loc) · 4.96 KB
/
sockets.js
File metadata and controls
186 lines (154 loc) · 4.96 KB
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
//=====================
//===socket.io stuff===
//=====================
var socketio = require("socket.io");
var uuid = require("node-uuid");
module.exports = function(server) {
var io = socketio.listen(server)
//Represents a group of clients in the same chatroom
var groups = {}; //roomID : list of client ids
io.sockets.on("connection", function(client) {
console.log("client id = " + client.id);
client.name = "Unknown";
client.on("createid", function() {
if(client.room) {
client.emit("createid", JSON.stringify({
id: client.room,
}));
return;
}
var UUID = uuid.v4();
client.room = UUID;
groups[UUID] = [];
groups[UUID].push(client);
client.name = "Host";
client.emit("createid", JSON.stringify({
id: UUID,
}));
});
client.on("setname", function(data) {
data = JSON.parse(data);
var result = false;
var message = "";
if(data.name.length < 3) {
message = "Name must be at least 3 characters long.";
} else if (data.name.length > 20) {
message = "Name cannot be more than 20 characters long."
} else {
client.name = data.name;
result = true;
message = "Name is good."
}
client.emit("setname", JSON.stringify({
result: result,
error: message
}));
});
client.on("requestCanvasData", function() {
io.to(groups[client.room][0].id).emit("getCanvasData", JSON.stringify({
requesterID: client.id
}));
});
client.on("giveCanvasData", function(data) {
console.log("giveCanvasData");
data = JSON.parse(data);
io.to(data.peerID).emit("HereIsCanvasData", JSON.stringify({
image: data.image,
}));
});
client.on("doesroomexist", function(data) {
data = JSON.parse(data);
var result = groups[data.room] ? true: false;
if(result) {
//group exists
//send number of ppl there and their clientIDs
var otherClientsIDs = [];
for(var i=0; i<groups[data.room].length; i++) {
otherClientsIDs.push(groups[data.room][i].id);
}
client.emit("roomExists", JSON.stringify({
groupmatesIDs: otherClientsIDs
}));
//add to group
var found = false;
if(groups[data.room] != undefined) {
for(var i=0; i<groups[data.room].length; i++) {
if(groups[data.room][i].id == client.id) {
found = true;
}
}
if(!found) {
console.log("added " + client.id + " to group");
groups[data.room].push(client);
client.room = data.room;
}
console.log("GROUP SO FAR:");
for(var i=0; i<groups[data.room].length; i++) {
console.log(groups[data.room][i].id);
}
console.log("+++++++++++++++++++++++++++");
}
}
else {
client.emit("roomDoesNotExist");
}
});
client.on("signalOffer", function(data) {
data = JSON.parse(data);
//console.log("!!!IN SIGNAL OFFER with target id = " + data.targetID);
//console.log("the offer is = " + data.clientOffer);
io.to(data.targetID).emit("offerFromClient", JSON.stringify({
offer: data.clientOffer,
offererID: client.id
}));
});
client.on("signalAnswer", function(data) {
data = JSON.parse(data);
//console.log("client room = " + client.room);
io.to(data.targetID).emit("answerToOffer", JSON.stringify({
roomID: client.room,
answer: data.clientAnswer,
answererID: client.id,
answererName: data.clientName
}));
});
client.on("iceCandidate", function(data) {
data = JSON.parse(data);
console.log("ICE candidate from " + client.id);
console.log("For room " + data.room);
if(groups[data.room] != undefined) {
for(var i=0; i<groups[data.room].length; i++) {
if(groups[data.room][i].id != client.id) {
console.log("Sending ICE Candidate to " + groups[data.room][i].id);
io.to(groups[data.room][i].id).emit("iceCandidateUpdate", JSON.stringify({
peerID: client.id,
iceCandidate: data.candidate
}));
}
}
console.log();
}
});
client.on("disconnect", function() {
if(client.room) {
console.log("deleting client " + client.id + " from group");
//delete client from group
var index = groups[client.room].indexOf(client);
groups[client.room].splice(index,1);
//tell all other clients in the group to delete this client
for(var i=0; i<groups[client.room].length; i++) {
console.log("HERE with " + groups[client.room][i].id);
io.to(groups[client.room][i].id).emit("deleteMember", JSON.stringify({
memberToDelete: client.id
}));
}
if(groups[client.room].length == 0) {
console.log("group member count == 0 so deleting entire group");
delete groups[client.room];
}
delete client;
}
});
});
console.log('Socket.io listening on server');
}