-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (41 loc) · 1.15 KB
/
index.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
const app = require("express")();
const server = require("http").createServer(app);
const cors = require("cors");
const io = require("socket.io")(server, {
cors: {
origin: "*",
method: ["GET", "POST"],
},
});
const corsOpts = {
origin: '*',
methods: ["GET", "POST"]
};
app.use(cors(corsOpts));
const PORT = process.env.PORT || 5000;
app.get("/", (req, res) => {
res.send("server is running!");
});
io.on("connection", (socket) => {
// give us our own id on frontend
socket.emit("me", socket.id);
// bread cast a message to end the handshake
socket.on("disconnect", () => {
socket.broadcast.emit("callended");
});
// calling the user data
socket.on("calluser", ({ userToCall, signalData, from, name }) => {
io.to(userToCall).emit("calluser", { signal: signalData, from, name });
});
//call recived by user and answered
socket.on("answercall", (data) => {
io.to(data.to).emit("callaccepted", data.signal);
});
//Message the connected user
socket.on("sendmessage", (body) => {
io.emit("message", body);
});
});
server.listen(PORT, (req, res) => {
console.log(`Server Listing on Port : ${PORT}`);
});