backend for twitch chat widgets
This commit is contained in:
parent
09c11f11a1
commit
28b9492cb3
11 changed files with 725 additions and 94 deletions
38
twitch/public/chat.js
Normal file
38
twitch/public/chat.js
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
const chatContainer = document.getElementById("chat");
|
||||
|
||||
async function sync() {
|
||||
let eventSource;
|
||||
try {
|
||||
eventSource = new EventSource("/twitch/sse/chat");
|
||||
} catch (err) {
|
||||
console.error("Failed to connect to event source", err);
|
||||
return;
|
||||
}
|
||||
|
||||
eventSource.addEventListener("error", () => {
|
||||
eventSource.close();
|
||||
console.error("Connection lost, or an error has occurred.");
|
||||
console.log("Attempting to reconnect...");
|
||||
|
||||
setTimeout(() => {
|
||||
sync();
|
||||
}, 1000);
|
||||
});
|
||||
|
||||
eventSource.addEventListener("chat", event => {
|
||||
console.log(`[${chat}] ${event.data}`);
|
||||
const messageEl = document.createElement("p");
|
||||
messageEl.innerText = event.data;
|
||||
chatContainer.appendChild(messageEl);
|
||||
});
|
||||
|
||||
eventSource.addEventListener("open", () => {
|
||||
console.log("Connected.");
|
||||
});
|
||||
}
|
||||
|
||||
function twitchEmoteURL(emoteID) {
|
||||
return `https://static-cdn.jtvnw.net/emoticons/v2/${emoteID}/default/dark/1.0#e=0`
|
||||
}
|
||||
|
||||
sync();
|
||||
Loading…
Add table
Add a link
Reference in a new issue