feat: move call sound to server
This commit is contained in:
6
index.ts
6
index.ts
@@ -94,6 +94,12 @@ Bun.serve({
|
|||||||
}
|
}
|
||||||
} else if (data.type == "rooms") {
|
} else if (data.type == "rooms") {
|
||||||
ws.send(JSON.stringify({ type: "rooms", rooms: ROOMS }));
|
ws.send(JSON.stringify({ type: "rooms", rooms: ROOMS }));
|
||||||
|
} else if (data.type == "call-audio") {
|
||||||
|
// Send the call.wav file as data URI
|
||||||
|
const file = await Bun.file("./call.wav").arrayBuffer();
|
||||||
|
const base64 = Buffer.from(file).toString("base64");
|
||||||
|
const dataUri = `data:audio/wav;base64,${base64}`;
|
||||||
|
ws.send(JSON.stringify({ type: "call-audio", dataUri }));
|
||||||
}
|
}
|
||||||
}, // a message is received
|
}, // a message is received
|
||||||
open(ws) {
|
open(ws) {
|
||||||
|
|||||||
@@ -13,6 +13,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
let withSound = false;
|
let withSound = false;
|
||||||
|
let callAudioURI = "";
|
||||||
let persisted: PersistedState<CallEntry[]> = new PersistedState("calls", []);
|
let persisted: PersistedState<CallEntry[]> = new PersistedState("calls", []);
|
||||||
let calls = $state<CallEntry[]>($state.snapshot(persisted.current));
|
let calls = $state<CallEntry[]>($state.snapshot(persisted.current));
|
||||||
let newCalls = $state<CallEntry[]>([]);
|
let newCalls = $state<CallEntry[]>([]);
|
||||||
@@ -37,8 +38,8 @@
|
|||||||
persisted.current = calls;
|
persisted.current = calls;
|
||||||
connected = true;
|
connected = true;
|
||||||
if (!firstLoad) {
|
if (!firstLoad) {
|
||||||
if (_newCalls.length > 0 && withSound) {
|
if (_newCalls.length > 0 && withSound && callAudioURI) {
|
||||||
const audio = new Audio("/call.wav");
|
const audio = new Audio(callAudioURI);
|
||||||
audio.play();
|
audio.play();
|
||||||
}
|
}
|
||||||
newCalls.push(..._newCalls);
|
newCalls.push(..._newCalls);
|
||||||
@@ -52,6 +53,15 @@
|
|||||||
eventTarget.dispatchEvent(
|
eventTarget.dispatchEvent(
|
||||||
new CustomEvent("send", { detail: { type: "display" } })
|
new CustomEvent("send", { detail: { type: "display" } })
|
||||||
);
|
);
|
||||||
|
if(withSound) {
|
||||||
|
eventTarget.addEventListener("call-audio", (e) => {
|
||||||
|
const detail = (e as CustomEvent).detail;
|
||||||
|
callAudioURI = detail.dataUri;
|
||||||
|
});
|
||||||
|
eventTarget.dispatchEvent(
|
||||||
|
new CustomEvent("send", { detail: { type: "call-audio" } })
|
||||||
|
);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
function updateMarqueeSpeed() {
|
function updateMarqueeSpeed() {
|
||||||
|
|||||||
Reference in New Issue
Block a user