Compare commits
10 Commits
3238623c15
...
c59ac2d7c7
| Author | SHA1 | Date | |
|---|---|---|---|
| c59ac2d7c7 | |||
| df7bf08099 | |||
| e42d492ef2 | |||
| f547e3f525 | |||
| fa05b054b3 | |||
| 965f43a397 | |||
| 1f73a8909a | |||
| be98b998e7 | |||
| 6e63e5c3ff | |||
| b0ec147c2d |
20
Dockerfile
Normal file
20
Dockerfile
Normal 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
19
docker-compose.yml
Normal 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
|
||||
|
||||
@@ -33,15 +33,20 @@ console.log("Echo mock frontend loaded");
|
||||
|
||||
var currentSessionId = null;
|
||||
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) {
|
||||
var partyLabel =
|
||||
currentParty === "A"
|
||||
? "You are party A (offerer side)"
|
||||
: currentParty === "B"
|
||||
? "You are party B (answerer side)"
|
||||
: "";
|
||||
? "You are party B (answerer side)"
|
||||
: "";
|
||||
|
||||
sessionInfoEl.textContent = partyLabel
|
||||
? text + " · " + partyLabel
|
||||
@@ -75,7 +80,7 @@ console.log("Echo mock frontend loaded");
|
||||
function resetState() {
|
||||
currentSessionId = null;
|
||||
currentParty = null;
|
||||
mockConnected = false;
|
||||
wsConnected = false;
|
||||
setChatInputEnabled(false);
|
||||
}
|
||||
|
||||
@@ -100,53 +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 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) {
|
||||
if (!mockConnected) {
|
||||
appendMessage(
|
||||
"system",
|
||||
"Mock mode: sending locally. Wire this up to RTCDataChannel.send()."
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
dataChannel.send(text)
|
||||
|
||||
appendMessage("me", text);
|
||||
|
||||
if (!mockConnected) {
|
||||
appendMessage(
|
||||
"them",
|
||||
"(Simulated peer) Replace with your RTCDataChannel onmessage handler."
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async function createSession() {
|
||||
@@ -176,11 +152,11 @@ console.log("Echo mock frontend loaded");
|
||||
|
||||
appendMessage(
|
||||
"system",
|
||||
"TODO: connect to /api/signal/session/" +
|
||||
currentSessionId +
|
||||
"/party/A via WebSocket."
|
||||
"Created session"
|
||||
);
|
||||
mockConnectP2P();
|
||||
|
||||
await connectWebSocket()
|
||||
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
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();
|
||||
if (!id) {
|
||||
sessionIdInput.focus();
|
||||
@@ -201,15 +177,152 @@ console.log("Echo mock frontend loaded");
|
||||
currentParty = "B";
|
||||
showChat();
|
||||
|
||||
appendMessage(
|
||||
"system",
|
||||
"TODO: connect to /api/signal/session/" +
|
||||
currentSessionId +
|
||||
"/party/B via WebSocket."
|
||||
);
|
||||
mockConnectP2P();
|
||||
await connectWebSocket()
|
||||
}
|
||||
|
||||
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 () {
|
||||
createSession();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user