From fa05b054b3922dcd87e637c3c87da26adf3952aa Mon Sep 17 00:00:00 2001 From: Abdulhade Date: Mon, 17 Nov 2025 01:28:24 +0300 Subject: [PATCH] Wired answers and now it is working! --- front_files/index.js | 52 ++++++++++++++++++++------------------------ 1 file changed, 23 insertions(+), 29 deletions(-) diff --git a/front_files/index.js b/front_files/index.js index f19bd98..9539842 100644 --- a/front_files/index.js +++ b/front_files/index.js @@ -105,47 +105,24 @@ console.log("Echo mock frontend loaded"); setSessionInfo( "Session ID: " + currentSessionId + " (share this with your peer)" ); - appendMessage( - "system", - "Session created. TODO: open a WebSocket for signaling and send an SDP offer." - ); + } else { setSessionInfo("Joined session: " + currentSessionId); - appendMessage( - "system", - "Joined session. TODO: connect to the signaling WebSocket and respond with an SDP answer." - ); + } - appendMessage( - "system", - "Mock mode: no real signaling or WebRTC yet. Everything stays local." - ); + } function sendChatMessage(text) { - if (!wsConnected) { - appendMessage( - "system", - "Mock mode: sending locally. Wire this up to RTCDataChannel.send()." - ); - } dataChannel.send(text) appendMessage("me", text); - - - if (!wsConnected) { - appendMessage( - "them", - "(Simulated peer) Replace with your RTCDataChannel onmessage handler." - ); - } } async function createSession() { @@ -256,6 +233,8 @@ console.log("Echo mock frontend loaded"); await handleOffer(payload) } else if (payload.type == "hello" && lastSignal) { sendSignal(lastSignal) + } else if (payload.type == "answer") { + handleAnswer(payload) } } @@ -275,14 +254,25 @@ console.log("Echo mock frontend loaded"); rc.ondatachannel = e => { const receiveChannel = e.channel; - receiveChannel.onmessage = e => console.log("message received!!!" + e.data) - receiveChannel.onopen = e => console.log("open!!!!"); - receiveChannel.onclose = e => console.log("closed!!!!!!"); + receiveChannel.onmessage = e => { + console.log("messsage received!!!" + e.data) + appendMessage("them", e.data) + } + receiveChannel.onopen = e => { + console.log("open!!!!") + setChatInputEnabled(true) + }; + receiveChannel.onclose = e => { + console.log("closed!!!!!!") + setChatInputEnabled(false) + }; rc.channel = receiveChannel; + dataChannel = receiveChannel } + await rc.setRemoteDescription(offer) console.log("Remote description applied") @@ -293,6 +283,10 @@ console.log("Echo mock frontend loaded"); lastSignal = rc.localDescription } + function handleAnswer(answer) { + pc.setRemoteDescription(answer).then(a => console.log("Accepted Answer.")) + } + function createChannel() { pc = new RTCPeerConnection()