-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocket.js
More file actions
40 lines (32 loc) · 1.18 KB
/
Copy pathsocket.js
File metadata and controls
40 lines (32 loc) · 1.18 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
const { Server } = require('socket.io');
const { saveClientProjectUpdateAndEmit, clientEnterProject, clientOffline } = require('./modules/project');
const socketExport = {};
socketExport.getSocketIO = (server) => {
const io = new Server(server, {
cors: {
origin: '*', // Only for debug
methods: ['GET', 'POST'],
},
});
io.on('connection', (socket) => {
console.log(`User connected, socketId: ${socket.id}`);
socket.on('disconnect', () => {
console.log(`User disconnected, socketId: ${socket.id}`);
io.emit('userDisconnected');
clientOffline(socket.id);
});
socket.on('clientUpdateProjectInfo', async (socketRes) => {
socket.join(socketRes.projectId);
const emitObject = await saveClientProjectUpdateAndEmit(socketRes);
if (emitObject) {
io.to(socketRes.projectId).emit('serverProjectInfoSync', emitObject);
}
});
socket.on('clientEnterProject', async (projectId, userId) => {
socket.join(projectId);
const emitObject = await clientEnterProject(projectId, userId, socket.id);
io.to(projectId).emit('serverProjectInfoSync', emitObject);
});
});
};
module.exports = socketExport;