Wrote some Websocket funcs

This commit is contained in:
2025-11-17 00:42:24 +03:00
parent be98b998e7
commit 1f73a8909a

View File

@@ -37,6 +37,8 @@ console.log("Echo mock frontend loaded");
var ws = null; var ws = null;
var pc = null; var pc = null;
var rc = null; var rc = null;
var lastSignal = null; // saving the last signal to send it on client connect
var dataChannel = null
function setSessionInfo(text) { function setSessionInfo(text) {
var partyLabel = var partyLabel =
@@ -131,8 +133,13 @@ console.log("Echo mock frontend loaded");
); );
} }
dataChannel.send(text)
appendMessage("me", text); appendMessage("me", text);
if (!wsConnected) { if (!wsConnected) {
appendMessage( appendMessage(
"them", "them",
@@ -215,6 +222,16 @@ console.log("Echo mock frontend loaded");
} }
} }
function sendSignal(payload) {
if (!ws || ws.readyState !== WebSocket.OPEN) {
return
}
try {
ws.send(JSON.stringify(payload));
} catch (err) {
console.error("Failed to send signal", err)
}
}
function handleWebSocketEvent(payload) { function handleWebSocketEvent(payload) {
// this will handle offers and answers // this will handle offers and answers
@@ -227,14 +244,27 @@ console.log("Echo mock frontend loaded");
pc.onicecandidate = e => { pc.onicecandidate = e => {
console.log(" NEW ice candidate!! on pc reprinting SDP ") console.log(" NEW ice candidate!! on pc reprinting SDP ")
console.log(JSON.stringify(pc.localDescription)) console.log(JSON.stringify(pc.localDescription))
sendSignal(JSON.stringify(pc.localDescription))
lastSignal = JSON.stringify(pc.localDescription)
} }
const sendChannel = pc.createDataChannel("sendChannel"); const sendChannel = pc.createDataChannel("sendChannel");
sendChannel.onmessage = e => console.log("messsage received!!!" + e.data) sendChannel.onmessage = e => {
sendChannel.onopen = e => console.log("open!!!!"); console.log("messsage received!!!" + e.data)
sendChannel.onclose = e => console.log("closed!!!!!!"); appendMessage("them", e.data)
}
sendChannel.onopen = e => {
console.log("open!!!!")
setChatInputEnabled(true)
};
sendChannel.onclose = e => {
console.log("closed!!!!!!")
setChatInputEnabled(false)
};
dataChannel = sendChannel
pc.createOffer().then(o => pc.setLocalDescription(o)) pc.createOffer().then(o => pc.setLocalDescription(o))