-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
93 lines (76 loc) · 2.42 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
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
import express from "express";
import bodyParser from "body-parser";
import cors from "cors";
import router from "./routes/routes.js";
import { Connection } from "./database/db.js";
import cookieParser from "cookie-parser";
import {Server} from "socket.io"
const port = process.env.PORT || 8000;
const app = express();
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use(
cors({
origin: ["http://localhost:3000"],
credentials: true,
})
);
app.use("/",router);
Connection();
app.get("/" , (req , res) => {
return res.send("Hello!");
});
const server = app.listen(port , () => {
console.log(`Server is running at ${port}`);
})
///////////////////////////////////////////////////////////////////////////////////////
// socket
const io = new Server(server,{
cors:{
origin: ["http://localhost:3000"],
// credentials: true,
}
})
io.on("connection",(socket)=>{
console.log("Connected");
socket.on("setup" , ({sender}) => {
// console.log(sender);
socket.join(sender._id);
console.log(`${sender.name} joined room : ${sender._id}`);
})
socket.on("getappointment" , ({id,userData}) => {
// console.log(userData.name);
socket.in(id).emit("appointmentreq" , {patient : userData});
})
socket.on("acceptreq" , ({info , pid , did , name}) => {
// console.log(info , `Request Accepted for ${pid} by ${did}`);
socket.in(pid).emit("reqaccept" , {info , pid , did , name});
})
socket.on("deletereq", ({id}) => {
// console.log(`Request Declined for ${id}`);
socket.in(id).emit("reqdelete" , {});
})
// socket.on("callUser" , ({pid , signalData ,did}) => {
// // console.log(`${name} is calling`,userToCall);
// socket.in(pid).emit("userCall" , {pid , signalData , did});
// })
// socket.on("joinPatient" , ({data, did}) => {
// // console.log(data);
// socket.in(did).emit("patientJoin" , {data});
// })
socket.emit("me", socket.id);
socket.on("callUser", ({ userToCall, signalData, from, name }) => {
console.log("server")
io.to(userToCall).emit("callUser", { signal: signalData, from, name });
});
socket.on("answerCall", (data) => {
io.to(data.to).emit("callAccepted", data.signal)
});
socket.on("cameraOff",({isOff,data})=>{
io.to(data).emit("cameraOff",{isOff,data});
})
socket.on("disconnect",() => {
console.log("Disconnected");
socket.broadcast.emit("callEnded");
})
});