Compare commits

...

10 Commits

4 changed files with 205 additions and 52 deletions

20
Dockerfile Normal file
View File

@@ -0,0 +1,20 @@
# Dockerfile
FROM golang:tip-alpine3.22 AS builder
WORKDIR /app
# RUN apk add --no-cache git ca-certificates
COPY go.mod go.sum ./
RUN go mod download
COPY . .
ENV CGO_ENABLED=0 GOOS=linux GOARCH=amd64
RUN go build -ldflags="-s -w" -o /app/echo .
FROM alpine:3.22
# RUN apk add --no-cache ca-certificates tzdata && update-ca-certificates
RUN addgroup -S app && adduser -S app -G app
USER app
WORKDIR /app
COPY --from=builder /app/echo /app/echo
COPY --from=builder /app/front_files /app/front_files
# EXPOSE is optional with custom networking, we can keep or remove it as we are using custom network with compose
EXPOSE 8900
ENTRYPOINT ["/app/echo"]

19
docker-compose.yml Normal file
View File

@@ -0,0 +1,19 @@
services:
echo:
build:
context: .
dockerfile: Dockerfile
image: echo-image:latest
container_name: echo
restart: unless-stopped
ports:
- "8900:8900"
networks:
- echo-net
environment:
- TZ=UTC
networks:
echo-net:
name: echo-net

View File

@@ -33,15 +33,20 @@ console.log("Echo mock frontend loaded");
var currentSessionId = null; var currentSessionId = null;
var currentParty = null; // "A" (offerer) or "B" (answerer) var currentParty = null; // "A" (offerer) or "B" (answerer)
var mockConnected = false; var wsConnected = false;
var ws = null;
var pc = 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 =
currentParty === "A" currentParty === "A"
? "You are party A (offerer side)" ? "You are party A (offerer side)"
: currentParty === "B" : currentParty === "B"
? "You are party B (answerer side)" ? "You are party B (answerer side)"
: ""; : "";
sessionInfoEl.textContent = partyLabel sessionInfoEl.textContent = partyLabel
? text + " · " + partyLabel ? text + " · " + partyLabel
@@ -75,7 +80,7 @@ console.log("Echo mock frontend loaded");
function resetState() { function resetState() {
currentSessionId = null; currentSessionId = null;
currentParty = null; currentParty = null;
mockConnected = false; wsConnected = false;
setChatInputEnabled(false); setChatInputEnabled(false);
} }
@@ -100,53 +105,24 @@ console.log("Echo mock frontend loaded");
setSessionInfo( setSessionInfo(
"Session ID: " + currentSessionId + " (share this with your peer)" "Session ID: " + currentSessionId + " (share this with your peer)"
); );
appendMessage(
"system",
"Session created. TODO: open a WebSocket for signaling and send an SDP offer."
);
} else { } else {
setSessionInfo("Joined session: " + currentSessionId); 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 mockConnectP2P() {
if (mockConnected) {
return;
}
mockConnected = true;
setChatInputEnabled(true);
appendMessage(
"system",
"Pretend the RTCDataChannel is open now. Replace this with real WebRTC events."
);
}
function sendChatMessage(text) { function sendChatMessage(text) {
if (!mockConnected) {
appendMessage(
"system", dataChannel.send(text)
"Mock mode: sending locally. Wire this up to RTCDataChannel.send()."
);
}
appendMessage("me", text); appendMessage("me", text);
if (!mockConnected) {
appendMessage(
"them",
"(Simulated peer) Replace with your RTCDataChannel onmessage handler."
);
}
} }
async function createSession() { async function createSession() {
@@ -176,11 +152,11 @@ console.log("Echo mock frontend loaded");
appendMessage( appendMessage(
"system", "system",
"TODO: connect to /api/signal/session/" + "Created session"
currentSessionId +
"/party/A via WebSocket."
); );
mockConnectP2P();
await connectWebSocket()
} catch (err) { } catch (err) {
console.error(err); console.error(err);
alert(err && err.message ? err.message : "Could not create session."); alert(err && err.message ? err.message : "Could not create session.");
@@ -190,7 +166,7 @@ console.log("Echo mock frontend loaded");
} }
} }
function joinSession() { async function joinSession() {
var id = sessionIdInput.value.trim(); var id = sessionIdInput.value.trim();
if (!id) { if (!id) {
sessionIdInput.focus(); sessionIdInput.focus();
@@ -201,15 +177,152 @@ console.log("Echo mock frontend loaded");
currentParty = "B"; currentParty = "B";
showChat(); showChat();
appendMessage( await connectWebSocket()
"system",
"TODO: connect to /api/signal/session/" +
currentSessionId +
"/party/B via WebSocket."
);
mockConnectP2P();
} }
async function connectWebSocket() {
// supporting ws for local testing.
var schema = window.location.protocol === "https" ? "wss://" : "ws://"
var wsURL = schema + window.location.host +
"/api/signal/session/" + currentSessionId + "/party/" + currentParty
ws = new WebSocket(wsURL)
ws.onopen = () => {
appendMessage("system", "Connected to WS")
if (currentParty == "A") {
createChannel()
} else {
sendSignal({ "type": "hello" })
}
}
ws.onmessage = async (event) => {
var event_data = event.data
console.log("Received from WS", event_data, typeof (event_data))
var payload = JSON.parse(event_data)
await handleWebSocketEvent(payload)
}
}
function sendSignal(payload) {
if (!payload) {
console.warn("Attempted to send empty signaling payload")
return
}
if (!ws || ws.readyState !== WebSocket.OPEN) {
return
}
try {
ws.send(JSON.stringify(payload));
} catch (err) {
console.error("Failed to send signal", err)
}
}
async function handleWebSocketEvent(payload) {
console.log("Handling WS Event, payload.type", payload.type, payload, typeof (payload))
if (payload.type == "offer") {
await handleOffer(payload)
} else if (payload.type == "hello" && lastSignal) {
sendSignal(lastSignal)
} else if (payload.type == "answer") {
handleAnswer(payload)
}
}
async function handleOffer(offer) {
console.log("New offer", offer)
if (!rc) {
rc = new RTCPeerConnection()
}
rc.onicecandidate = e => {
console.log(" NEW ice candidate!! reprinting SDP ")
console.log(JSON.stringify(rc.localDescription))
}
rc.ondatachannel = e => {
const receiveChannel = e.channel;
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")
var answer = await rc.createAnswer()
await rc.setLocalDescription(answer)
console.log("Answer", JSON.stringify(rc.localDescription))
sendSignal(rc.localDescription)
lastSignal = rc.localDescription
}
function handleAnswer(answer) {
pc.setRemoteDescription(answer).then(a => console.log("Accepted Answer."))
}
function createChannel() {
pc = new RTCPeerConnection()
pc.onicecandidate = e => {
console.log(" NEW ice candidate!! on pc reprinting SDP ")
console.log(JSON.stringify(pc.localDescription))
if (pc.localDescription) {
sendSignal(pc.localDescription)
lastSignal = pc.localDescription
}
}
const sendChannel = pc.createDataChannel("sendChannel");
sendChannel.onmessage = e => {
console.log("messsage received!!!" + e.data)
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))
}
createBtn.addEventListener("click", function () { createBtn.addEventListener("click", function () {
createSession(); createSession();
}); });

View File

@@ -7,6 +7,7 @@ import (
func main() { func main() {
fmt.Println("Hello from echo-echo-cho-o") fmt.Println("Hello from echo-echo-cho-o")
fmt.Println("Listening to port 8900")
api.BuildRouter(":8080") api.BuildRouter(":8900")
} }