-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
66 lines (58 loc) · 1.89 KB
/
server.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
const express=require('express');
require('dotenv').config({path:'./config.env'});
const bodyParser=require('body-parser');
const cors=require('cors');
const morgan=require('morgan');
const path=require('path');
const Code=require('./models/Code');
require('./db/conn');
const app=express();
const port=process.env.PORT || 8000;
app.use(cors());
app.use(morgan("dev"));
app.use(bodyParser.json({limit:'50mb'}));
app.use(bodyParser.urlencoded({limit:"50mb",extended:false}));
app.use('/api',require('./routes/code'));
if(process.env.NODE_ENV === 'production'){
app.use(express.static('client/build'))
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client/build", "index.html"));
});
}
const server=app.listen(port,()=>{
console.log(`Server is running at http://localhost:${port}`);
})
const io=require('socket.io')(server);
io.on("connection",(socket)=>{
console.log("Connected to socket!");
socket.on('joined',async({roomDbId})=>{
// console.log(roomDbId);
socket.join(roomDbId);
})
socket.on("enteringCode",async({code,roomDbId})=>{
// console.log(code);
await Code.findOneAndUpdate({_id:roomDbId},{
$set:{
code
}
});
socket.to(roomDbId).emit("receivingCode",{recCode:code});
})
socket.on('enteringInp',({inp,roomDbId})=>{
// console.log(inp);
socket.to(roomDbId).emit("receivingInp",{inp})
})
socket.on("sendOp",({op,roomDbId})=>{
// console.log("op ",op);
// socket.emit("receiveOp",{op})
socket.to(roomDbId).emit("receiveOp",{op})
})
socket.on("changeExtLangSn",async({lng,cd,ex,roomDbId})=>{
await Code.findOneAndUpdate({_id:roomDbId},{
$set:{
code:cd,language:lng,extension:ex
}
})
io.to(roomDbId).emit("setExtLangSn",{lng,cd,ex});
})
})