WIP
This commit is contained in:
@@ -25,13 +25,15 @@
|
||||
let soundBuffer = null;
|
||||
let isAudioUnlocked = false;
|
||||
|
||||
const audioFallback = new Audio(SOUND_URL);
|
||||
audioFallback.preload = "auto";
|
||||
// HTML5 Audio Fallback element
|
||||
const fallbackAudio = document.getElementById("mm-chat-sound") || new Audio(SOUND_URL);
|
||||
fallbackAudio.src = SOUND_URL;
|
||||
fallbackAudio.preload = "auto";
|
||||
|
||||
const soundedIds = new Set();
|
||||
let inboxReady = false;
|
||||
|
||||
// Initialize and unlock audio context on explicit user gesture
|
||||
// Unlocks audio context on user interaction to bypass browser Autoplay policies
|
||||
function unlockAudio() {
|
||||
if (isAudioUnlocked) return;
|
||||
|
||||
@@ -43,26 +45,38 @@
|
||||
audioCtx.resume();
|
||||
}
|
||||
|
||||
// Warm up HTML Audio element for browsers blocking Web Audio
|
||||
audioFallback.play().then(() => {
|
||||
audioFallback.pause();
|
||||
audioFallback.currentTime = 0;
|
||||
isAudioUnlocked = true;
|
||||
}).catch(() => {
|
||||
// User interaction required
|
||||
});
|
||||
// Warm up HTML Audio tag
|
||||
const playPromise = fallbackAudio.play();
|
||||
if (playPromise !== undefined) {
|
||||
playPromise
|
||||
.then(() => {
|
||||
fallbackAudio.pause();
|
||||
fallbackAudio.currentTime = 0;
|
||||
isAudioUnlocked = true;
|
||||
})
|
||||
.catch(() => { });
|
||||
}
|
||||
|
||||
// Fetch and decode Audio Buffer for Web Audio API
|
||||
if (audioCtx && !soundBuffer) {
|
||||
fetch(SOUND_URL, { credentials: "same-origin", cache: "force-cache" })
|
||||
.then((res) => res.arrayBuffer())
|
||||
.then((response) => {
|
||||
if (!response.ok) throw new Error("Audio file error");
|
||||
return response.arrayBuffer();
|
||||
})
|
||||
.then((bytes) => audioCtx.decodeAudioData(bytes))
|
||||
.then((buf) => { soundBuffer = buf; })
|
||||
.catch(() => {});
|
||||
.then((buffer) => {
|
||||
soundBuffer = buffer;
|
||||
isAudioUnlocked = true;
|
||||
})
|
||||
.catch(() => { });
|
||||
}
|
||||
}
|
||||
|
||||
function playNotifySound() {
|
||||
// Attempt 1: Web Audio API
|
||||
unlockAudio();
|
||||
|
||||
// 1. Try Web Audio API
|
||||
if (audioCtx && audioCtx.state === "running" && soundBuffer) {
|
||||
try {
|
||||
const source = audioCtx.createBufferSource();
|
||||
@@ -70,12 +84,18 @@
|
||||
source.connect(audioCtx.destination);
|
||||
source.start(0);
|
||||
return;
|
||||
} catch (e) {}
|
||||
} catch (e) {
|
||||
// Fallback below
|
||||
}
|
||||
}
|
||||
|
||||
// Attempt 2: HTML5 Audio Fallback
|
||||
audioFallback.currentTime = 0;
|
||||
audioFallback.play().catch(() => {});
|
||||
// 2. Fallback to standard HTML5 Audio
|
||||
try {
|
||||
fallbackAudio.currentTime = 0;
|
||||
fallbackAudio.play().catch((err) => {
|
||||
console.warn("Autoplay blocked sound execution:", err);
|
||||
});
|
||||
} catch (e) { }
|
||||
}
|
||||
|
||||
function playIncomingSound(message) {
|
||||
@@ -206,9 +226,8 @@
|
||||
|
||||
state.messages.forEach((message) => {
|
||||
const bubble = document.createElement("div");
|
||||
bubble.className = `mm-chat__bubble ${
|
||||
isOutgoing(message, conversation) ? "mm-chat__bubble--out" : "mm-chat__bubble--in"
|
||||
}`;
|
||||
bubble.className = `mm-chat__bubble ${isOutgoing(message, conversation) ? "mm-chat__bubble--out" : "mm-chat__bubble--in"
|
||||
}`;
|
||||
const body = document.createElement("div");
|
||||
body.textContent = message.body;
|
||||
const meta = document.createElement("span");
|
||||
@@ -332,14 +351,14 @@
|
||||
sendEl.disabled = true;
|
||||
inputEl.value = "";
|
||||
|
||||
// 1. OPTIMISTIC UPDATE: Render local message immediately
|
||||
// Render message locally first (Instant UI update)
|
||||
const tempId = "temp-" + Date.now();
|
||||
const optimisticMsg = {
|
||||
id: tempId,
|
||||
conversation_id: state.activeId,
|
||||
body: body,
|
||||
created_at: new Date().toISOString(),
|
||||
user: { id: config.userId || "current" }
|
||||
user: { id: "operator" }
|
||||
};
|
||||
|
||||
state.messages.push(optimisticMsg);
|
||||
@@ -357,21 +376,16 @@
|
||||
const serverMsg = payload.data;
|
||||
if (serverMsg && serverMsg.id) {
|
||||
soundedIds.add(messageKey(serverMsg));
|
||||
// Replace temp message with server message
|
||||
const idx = state.messages.findIndex(m => m.id === tempId);
|
||||
const idx = state.messages.findIndex((m) => m.id === tempId);
|
||||
if (idx !== -1) {
|
||||
state.messages[idx] = serverMsg;
|
||||
} else if (!state.messages.some(m => String(m.id) === String(serverMsg.id))) {
|
||||
state.messages.push(serverMsg);
|
||||
}
|
||||
renderMessages();
|
||||
}
|
||||
await loadInbox();
|
||||
} catch (err) {
|
||||
// Remove temp message if request failed
|
||||
state.messages = state.messages.filter(m => m.id !== tempId);
|
||||
state.messages = state.messages.filter((m) => m.id !== tempId);
|
||||
renderMessages();
|
||||
alert("Failed to send message");
|
||||
} finally {
|
||||
sendEl.disabled = false;
|
||||
inputEl.focus();
|
||||
@@ -428,23 +442,18 @@
|
||||
function bootEcho() {
|
||||
const pusher = config.pusher || {};
|
||||
const EchoClass = window.Echo;
|
||||
if (typeof EchoClass !== "function" || !window.Pusher || !pusher.key) {
|
||||
console.warn("Echo/Pusher assets missing.");
|
||||
return;
|
||||
}
|
||||
if (typeof EchoClass !== "function" || !window.Pusher || !pusher.key) return;
|
||||
|
||||
const isHttps = window.location.protocol === "https:";
|
||||
const host = window.location.hostname;
|
||||
const endpoint = echoEndpoint(pusher);
|
||||
|
||||
try {
|
||||
state.echo = new EchoClass({
|
||||
broadcaster: "reverb",
|
||||
key: pusher.key,
|
||||
wsHost: host,
|
||||
// Match standard HTTPS (443) or custom port passed from backend
|
||||
wsPort: pusher.port || (isHttps ? 443 : 80),
|
||||
wssPort: pusher.port || (isHttps ? 443 : 80),
|
||||
forceTLS: isHttps,
|
||||
wsHost: endpoint.host,
|
||||
wsPort: endpoint.port,
|
||||
wssPort: endpoint.port,
|
||||
forceTLS: endpoint.forceTLS,
|
||||
enabledTransports: ["ws", "wss"],
|
||||
disableStats: true,
|
||||
cluster: "mt1",
|
||||
@@ -459,19 +468,8 @@
|
||||
},
|
||||
});
|
||||
|
||||
// Debugging connection states in console
|
||||
if (state.echo.connector && state.echo.connector.pusher) {
|
||||
state.echo.connector.pusher.connection.bind("state_change", (states) => {
|
||||
console.log("[Reverb WS State]:", states.current);
|
||||
});
|
||||
state.echo.connector.pusher.connection.bind("error", (err) => {
|
||||
console.error("[Reverb WS Error]:", err);
|
||||
});
|
||||
}
|
||||
|
||||
state.echo.private(config.inboxChannel || "chat.support").listen(".message.sent", upsertIncoming);
|
||||
} catch (err) {
|
||||
console.error("Failed to initialize Echo:", err);
|
||||
state.echo = null;
|
||||
}
|
||||
}
|
||||
@@ -525,9 +523,9 @@
|
||||
}
|
||||
});
|
||||
|
||||
// Global user interaction handlers to unlock autoplay block
|
||||
["click", "keydown", "touchstart"].forEach((eventName) => {
|
||||
document.addEventListener(eventName, unlockAudio, { once: false, capture: true });
|
||||
// Attach global unlock listeners to capture any user interaction instantly
|
||||
["pointerdown", "keydown", "touchstart", "click"].forEach((eventName) => {
|
||||
document.addEventListener(eventName, unlockAudio, { capture: true, once: false });
|
||||
});
|
||||
|
||||
loadInbox();
|
||||
|
||||
Reference in New Issue
Block a user