Spaces:
Sleeping
Sleeping
Create main.js
Browse files- static/main.js +33 -0
static/main.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Create a new RTCPeerConnection instance
|
| 2 |
+
let pc = new RTCPeerConnection();
|
| 3 |
+
|
| 4 |
+
// Function to send an offer request to the server
|
| 5 |
+
async function createOffer() {
|
| 6 |
+
console.log("Sending offer request");
|
| 7 |
+
|
| 8 |
+
// Fetch the offer from the server
|
| 9 |
+
const offerResponse = await fetch("/offer", {
|
| 10 |
+
method: "POST",
|
| 11 |
+
headers: {
|
| 12 |
+
"Content-Type": "application/json",
|
| 13 |
+
},
|
| 14 |
+
body: JSON.stringify({
|
| 15 |
+
sdp: "",
|
| 16 |
+
type: "offer",
|
| 17 |
+
}),
|
| 18 |
+
});
|
| 19 |
+
|
| 20 |
+
// Parse the offer response
|
| 21 |
+
const offer = await offerResponse.json();
|
| 22 |
+
console.log("Received offer response:", offer);
|
| 23 |
+
|
| 24 |
+
// Set the remote description based on the received offer
|
| 25 |
+
await pc.setRemoteDescription(new RTCSessionDescription(offer));
|
| 26 |
+
|
| 27 |
+
// Create an answer and set it as the local description
|
| 28 |
+
const answer = await pc.createAnswer();
|
| 29 |
+
await pc.setLocalDescription(answer);
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
// Trigger the process by creating and sending an offer
|
| 33 |
+
createOffer();
|