actually got my js being called

This commit is contained in:
Nickiel12 2024-08-23 18:35:19 -07:00
parent 39c62db5f5
commit 5ef49cc2c2
3 changed files with 86 additions and 79 deletions

View file

@ -3,7 +3,6 @@
<head>
<link href="/static/main.css" rel="stylesheet"/>
<script src="/static/feather.min.js"></script>
<script src="/static/index.js" type="module"></script>
</head>
<body>
@ -43,8 +42,11 @@
</div>
</div>
<script>
<script type=module>
import { init } from "/static/index.js";
feather.replace();
init();
console.log("Hello worlds");
</script>
</body>

View file

@ -1,5 +1,5 @@
const { invoke } = window.__TAURI__.tauri;
import { videoview } from "./rtc.js";
import { rtc_init } from "./rtc.js";
function call_camera_connect() {
invoke("connect_to_camera", {})
@ -12,3 +12,10 @@ function call_camera_connect() {
})
.catch((e) => console.error(e));
}
async function init() {
console.log("Setting up");
await rtc_init();
}
export { init };

View file

@ -1,92 +1,90 @@
const { emit, listen } = window.__TAURI.tauri.event;
const videoview = document.getElementById("remoteview");
import { event } from "./@tauri-apps/api/index.js";
const config = {
iceServers: [{ urls: "stun:localhost" }]
};
const polite = true;
const pc = new RTCPeerConnection(config);
pc.ontrack = ({ track, streams }) => {
track.onunmute = () => {
if (remoteview.srcObject) {
return;
}
remoteview.srcObject = streams[0];
};
};
let makingOffer = false;
pc.onnegotionationneeded = async () => {
try {
makingOffer = true;
await pc.setLocalDescription();
emit("webrtc-message", { description: pc.localDescription });
} catch (err) {
console.error(err);
} finally {
makingOffer = false;
}
};
pc.onicecandidate = ({ candidate }) => emit("webrtc-message", { candidate });
pc.oniceconnectionstatechange = () => {
if (pc.iceConnectionState === "failed") {
pc.restartIce();
}
};
let ignoreOffer = false;
console.log("registering listner");
const application_message = await listen('frontend_message', async (event) => {
console.log("Event: ");
async function rtc_init() {
console.log("ding ding!");
console.log(event);
const videoview = document.getElementById("remoteview");
const { description, candidate } = event.payload.data;
const config = {
iceServers: [{ urls: "stun:localhost" }]
};
const polite = true;
try {
if (description) {
const offerCollision =
description.type === "offer" &&
(makingOffer || pc.signalingState !== "stable");
const pc = new RTCPeerConnection(config);
ignoreOffer = !polite && offerCollision;
if (ignoreOffer) {
pc.ontrack = ({ track, streams }) => {
track.onunmute = () => {
if (remoteview.srcObject) {
return;
}
remoteview.srcObject = streams[0];
};
};
await pc.setRemoteDescription(description);
if (description.type === "offer") {
await pc.setLocalDescription();
emit( "webrtc-message", { description: pc.localDescription });
}
} else if (candidate) {
try {
await pc.addIceCandidate(candidate);
} catch (err) {
if (!ignoreOffer) {
throw err;
let makingOffer = false;
pc.onnegotionationneeded = async () => {
try {
makingOffer = true;
await pc.setLocalDescription();
event.emit("webrtc-message", { description: pc.localDescription });
} catch (err) {
console.error(err);
} finally {
makingOffer = false;
}
};
pc.onicecandidate = ({ candidate }) => event.emit("webrtc-message", { candidate });
pc.oniceconnectionstatechange = () => {
if (pc.iceConnectionState === "failed") {
pc.restartIce();
}
};
let ignoreOffer = false;
console.log("registering listner");
const application_message = await event.listen('frontend_message', async (event) => {
console.log("Event: ");
console.log(event);
const { description, candidate } = event.payload.data;
try {
if (description) {
const offerCollision =
description.type === "offer" &&
(makingOffer || pc.signalingState !== "stable");
ignoreOffer = !polite && offerCollision;
if (ignoreOffer) {
return;
}
await pc.setRemoteDescription(description);
if (description.type === "offer") {
await pc.setLocalDescription();
event.emit( "webrtc-message", { description: pc.localDescription });
}
} else if (candidate) {
try {
await pc.addIceCandidate(candidate);
} catch (err) {
if (!ignoreOffer) {
throw err;
}
}
}
} catch (err) {
console.error(err);
}
} catch (err) {
console.error(err);
}
});
});
}
// async function start_rtc_connection() {
//try {
// const stream = await navigator.mediaDevices.getUserMedia({ audio: false, video: false });
//}
//}
export { videoview };
export { rtc_init };