-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
81 lines (68 loc) · 1.8 KB
/
index.js
File metadata and controls
81 lines (68 loc) · 1.8 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
const express = require("express")
const app = express()
app.use(express.json())
const connections = {}
DELAY = 2500
function waitForReciever(id) {
return new Promise((resolve) => {
function checkForReciever() {
if (connections[id].connect == true) {
resolve(connections[id].answer)
}
else {
console.log(`ID : ${id} Waiting for connection!`)
setTimeout(checkForReciever, DELAY)
}
}
checkForReciever()
})
}
app.get("/", (req, res) => {
res.send("Hello World!")
})
app.post("/offer", async (req, res) => {
res.setHeader("Transfer-Encoding", "chunked")
var id = req.body.ID
connections[id] = (req.body)
connections[id]["connect"] = false;
console.log("Recieved Body = ", connections[id])
try {
const receiver = await waitForReciever(id);
if (receiver) {
delete connections[id]
res.json({ receiver });
} else {
console.log("Receiver not Found!");
res.json({ error: "Receiver not found" });
}
} catch (error) {
console.error("Error while waiting for receiver:", error);
res.json({ error: "Internal server error" });
}
})
app.post("/sendanswer", (req, res) => {
var id = req.body.connectID
if (connections[id] !== null) {
var offerObject = connections[id]
connections[id].connect = true
connections[id].answer = req.body.answer
console.log("Sending offerObject", offerObject)
res.send({ "success": true })
}
else {
res.send({ "success": false })
}
})
app.get("/getoffer", (req, res) => {
var id = req.body.connectID
if (connections[id] !== null) {
var offerObject = connections[id]
res.send({ "success": true, "offer": offerObject })
}
else {
res.send({ "success": false })
}
})
app.listen(9400, () => {
console.log("Server running on port : 9400.")
})