clientside websocket connect, increase volume

This commit is contained in:
2024-12-23 12:43:59 -06:00
parent c0e99b5f94
commit 4eeb9ddcef

View File

@@ -2,6 +2,7 @@ import Badge from "@/components/Badge";
import Emboldened from "@/components/Emboldened"; import Emboldened from "@/components/Emboldened";
import { cn, plural, type ClassValue } from "@/util"; import { cn, plural, type ClassValue } from "@/util";
import { useRef, useState } from "preact/hooks"; import { useRef, useState } from "preact/hooks";
import { useEffect } from "preact/hooks";
type StatefulDemoProps = { type StatefulDemoProps = {
class?: ClassValue; class?: ClassValue;
@@ -13,6 +14,36 @@ type SessionData = {
}; };
const StatefulDemo = ({ class: className }: StatefulDemoProps) => { const StatefulDemo = ({ class: className }: StatefulDemoProps) => {
useEffect(() => {
const socket = new WebSocket(
(window.location.protocol === "https:" ? "wss://" : "ws://") +
window.location.host +
"/ws"
);
socket.onmessage = (event) => {
const data = JSON.parse(event.data);
if (data.type == undefined)
throw new Error("Received message without type");
switch (data.type) {
case "session":
setSession(data.session);
break;
default:
console.warn("Received unknown message type", data.type);
}
};
socket.onclose = () => {
console.log("WebSocket connection closed");
};
return () => {
socket.close();
};
}, []);
const randomBits = (bits: number) => const randomBits = (bits: number) =>
Math.floor(Math.random() * 2 ** bits) Math.floor(Math.random() * 2 ** bits)
.toString(16) .toString(16)
@@ -70,7 +101,7 @@ const StatefulDemo = ({ class: className }: StatefulDemoProps) => {
onClick={function onClick() { onClick={function onClick() {
highlight(i); highlight(i);
const audio = new Audio("/notify.wav"); const audio = new Audio("/notify.wav");
audio.volume = 0.3; audio.volume = 0.5;
audio.play(); audio.play();
}} }}
> >