-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
113 lines (86 loc) · 3.01 KB
/
server.js
File metadata and controls
113 lines (86 loc) · 3.01 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
const express = require('express');
const app = express();
const http = require('http');
const path = require('path');
const server = http.createServer(app);
if(process.env.NODE_ENV === 'production'){
app.use(express.static('./client/build'));
app.use((req, res, next) => {
res.sendFile(path.join(__dirname, './client/build', 'index.html'));
});
}
const io=require('socket.io')(server,{
cors:{
origin:'*',
methods:["GET","POST"]
}
})
const Document=require('./Document')
const mongoose=require('mongoose');
const db=`mongodb://jishu:6wXF9e49qHMBbSB@ac-uapmlma-shard-00-00.jpmoif5.mongodb.net:27017,ac-uapmlma-shard-00-01.jpmoif5.mongodb.net:27017,ac-uapmlma-shard-00-02.jpmoif5.mongodb.net:27017/?ssl=true&replicaSet=atlas-6ciqwn-shard-0&authSource=admin&retryWrites=true&w=majority`;
mongoose.connect(db).then(()=>console.log("DB Connected!")).catch((err)=>console.log(err));
// what happens when user joins
//get all connected clients of room
const userSocketMap = {};
function getAllConnectedClients(fileId) {
// Map
return Array.from(io.sockets.adapter.rooms.get(fileId) || []).map(
(socketId) => {
return {
socketId,
username: userSocketMap[socketId],
};
}
);
}
// when io is connected
io.on("connection", socket =>{
// if socket receives join signal (someone joined)
socket.on('join', ({ fileId, username }) => {
userSocketMap[socket.id] = username;
socket.join(fileId);
const clients = getAllConnectedClients(fileId);
clients.forEach(({ socketId }) => {
io.to(socketId).emit('joined', {
clients,
username,
socketId: socket.id,
});
});
});
console.log('connected!')
socket.on('get-file',async fileId=>{
const document= await findOrCreateFile(fileId);
socket.join(fileId);
socket.emit('load-file',document.data);
socket.on('send-changes',text=>{
//console.log(text);
socket.broadcast.to(fileId).emit('receive-changes',text);
})
socket.on('save-file',async data=>{
await Document.findByIdAndUpdate(fileId,{data})
})
})
socket.on('disconnecting', () => {
const rooms = [...socket.rooms];
rooms.forEach((fileId) => {
socket.in(fileId).emit('disconnected', {
socketId: socket.id,
username: userSocketMap[socket.id],
});
});
delete userSocketMap[socket.id];
socket.leave();
console.log('left!!')
});
})
const defaultValue="";
async function findOrCreateFile(id){
if(id==null)return;
//console.log(id);
const document=await Document.findById(id);
if(document) return document;
return await Document.create({_id:id,data:defaultValue})
}
const PORT = process.env.PORT || 5000;
server.listen(PORT, () => console.log(`Listening on port ${PORT}`));