-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetupSocket.js
More file actions
129 lines (115 loc) · 3.31 KB
/
Copy pathsetupSocket.js
File metadata and controls
129 lines (115 loc) · 3.31 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
const socketio = require("socket.io");
const jwt = require("jsonwebtoken");
const Board = require("./models/boardModel");
const User = require("./models/userModel");
const roomAuth = async (userHandle, boardID) => {
if (userHandle) {
try {
const board = await Board.findById(boardID)
.select("owner isPublic")
.populate({
path: "collaborators",
select: "handle",
});
if (board.isPublic) return true;
let found = false;
if (board.owner === userHandle) found = true;
else {
for (var i = 0; i < board.collaborators.length; i++) {
if (board.collaborators[i].handle === userHandle) {
found = true;
break;
}
}
}
if (found) return true;
else return false;
} catch (err) {
return false;
}
} else {
try {
const board = await Board.findById(boardID).select("isPublic");
if (board.isPublic) return true;
else return false;
} catch (err) {
return false;
}
}
};
module.exports.setupSocket = (server) => {
const io = socketio(server, {
cors: {
origin: "http://localhost:3000",
methods: ["GET", "POST"],
credentials: true,
},
});
io.use((socket, next) => {
if (socket.handshake.query.token) {
try {
socket.decoded = jwt.verify(
socket.handshake.query.token,
process.env.JWT_SECRET
);
next();
} catch (err) {
console.log("Could not authorize for this board");
next(new Error("Could not authorize for this board"));
}
} else {
socket.decoded = { id: null };
next();
}
}).on("connection", async (socket) => {
let currUser;
if (socket.decoded.id)
currUser = await User.findById(socket.decoded.id).select("handle");
// console.log(currUser, socket.decoded.id)
const userHandle = socket.decoded.id ? currUser.handle : null;
console.log("socket connected");
io.emit("connected");
let roomID = null;
socket.on("disconnect", () => console.log("socket disconnected"));
socket.on("join-room", async (boardID) => {
try {
const canJoin = await roomAuth(userHandle, boardID);
if (canJoin) {
roomID = boardID;
socket.join(roomID);
console.log("Room joined");
io.emit("room-joined");
} else {
console.log("unauthorized");
io.emit("auth-error");
}
} catch (err) {
console.log("unauthorized");
io.emit("auth-error");
}
});
socket.on("leave-room", async (boardID) => {
try {
const canLeave = await roomAuth(userHandle, boardID);
if (canLeave) {
socket.leave(roomID);
roomID = null;
console.log("Room left");
} else {
console.log("Could not leave");
}
} catch (err) {
console.log("Could not leave");
}
});
socket.on("update-canvas", async (data) => {
socket.broadcast.to(roomID).emit("update-canvas", data);
});
socket.on('joinAudioRoom', (roomId, userId) => {
socket.broadcast.to(roomId).emit('userJoinedAudio', userId);
socket.on('leaveAudioRoom', () => {
socket.broadcast.to(roomId).emit('userLeftAudio', userId);
});
});
});
};