live users

This commit is contained in:
JurajKubrican
2025-08-04 20:05:08 +02:00
parent 70b0b70f65
commit c350501f29
5 changed files with 50 additions and 51 deletions

View File

@@ -10,8 +10,6 @@
appearance: none; appearance: none;
-webkit-appearance: none; -webkit-appearance: none;
-moz-appearance: none; -moz-appearance: none;
width: 20px;
height: 20px;
background: transparent; background: transparent;
border: none; border: none;
position: relative; position: relative;
@@ -50,4 +48,8 @@
display: grid; display: grid;
grid-template-columns: repeat(32, 1fr); /* 32 equal columns */ grid-template-columns: repeat(32, 1fr); /* 32 equal columns */
grid-template-rows: repeat(32, 1fr); /* 32 equal rows */ grid-template-rows: repeat(32, 1fr); /* 32 equal rows */
}
.counter-label {
color: #4a7c59;
} }

View File

@@ -20,34 +20,28 @@
}); });
return; return;
} }
if (instruction.startsWith("u:")) {
const items = instruction.split(":");
const el = document.getElementById("counter");
el.innerText = items[1];
const el2 = document.getElementById("ips");
el2.innerText = items[2].replaceAll(",", "\n");
return;
}
}; };
socket.addMessageListener((data) => { socket.addMessageListener((data) => {
const instructions = data.split("\n"); const instructions = data.split("\n");
instructions.forEach(handleInstruction); instructions.forEach(handleInstruction);
}); });
const handleBoxChange = (event) => {
const target = event.target;
const id = target?.id.split("-")[1];
const value = target.checked ? "+" : "-";
socket.send("b:" + id + ":" + value);
};
document.querySelectorAll(".boxes input").forEach((input) => { document.querySelectorAll(".boxes input").forEach((input) => {
input.addEventListener("change", (event) => { input.addEventListener("change", handleBoxChange);
const target = event.target;
const id = target?.id.split("-")[1];
const value = target.checked ? "+" : "-";
socket.send("b:" + id + ":" + value);
});
}); });
// const autoPlayEl = document.querySelector("#randomize") as CheckboxEl;
// autoPlayEl?.addEventListener("click", (e) => socket.send("r:1000"));
// var golTimer: number | undefined = undefined;
// const handleGol = (el: CheckboxEl) => {
// if (el.checked) {
// golTimer = setInterval(() => {
// socket.send("gol");
// }, 500);
// } else {
// clearInterval(golTimer);
// }
// };
// const golEl = document.querySelector("#game-of-life") as CheckboxEl;
// handleGol(golEl);
// golEl.addEventListener("change", (e) => handleGol(e.target as CheckboxEl));
const container = document.querySelector(".boxes"); const container = document.querySelector(".boxes");
const resizeObserver = new ResizeObserver((entries) => { const resizeObserver = new ResizeObserver((entries) => {
const entry = entries.at(0); const entry = entries.at(0);

View File

@@ -29,6 +29,16 @@ type Box = {
}); });
return; return;
} }
if (instruction.startsWith("u:")) {
const items = instruction.split(":");
const el = document.getElementById("counter") as CheckboxEl;
el.innerText = items[1];
const el2 = document.getElementById("ips") as CheckboxEl;
el2.innerText = items[2].replaceAll(",", "<br/>");
return;
}
}; };
socket.addMessageListener((data) => { socket.addMessageListener((data) => {
@@ -36,34 +46,17 @@ type Box = {
instructions.forEach(handleInstruction); instructions.forEach(handleInstruction);
}); });
const handleBoxChange = (event: Event) => {
const target = event.target as CheckboxEl;
const id = target?.id.split("-")[1];
const value = target.checked ? "+" : "-";
socket.send("b:" + id + ":" + value);
}
document.querySelectorAll(".boxes input").forEach((input) => { document.querySelectorAll(".boxes input").forEach((input) => {
input.addEventListener("change", (event) => { input.addEventListener("change", handleBoxChange);
const target = event.target as CheckboxEl;
const id = target?.id.split("-")[1];
const value = target.checked ? "+" : "-";
socket.send("b:" + id + ":" + value);
});
}); });
// const autoPlayEl = document.querySelector("#randomize") as CheckboxEl;
// autoPlayEl?.addEventListener("click", (e) => socket.send("r:1000"));
// var golTimer: number | undefined = undefined;
// const handleGol = (el: CheckboxEl) => {
// if (el.checked) {
// golTimer = setInterval(() => {
// socket.send("gol");
// }, 500);
// } else {
// clearInterval(golTimer);
// }
// };
// const golEl = document.querySelector("#game-of-life") as CheckboxEl;
// handleGol(golEl);
// golEl.addEventListener("change", (e) => handleGol(e.target as CheckboxEl));
const container = document.querySelector(".boxes") as HTMLDivElement; const container = document.querySelector(".boxes") as HTMLDivElement;
const resizeObserver = new ResizeObserver((entries) => { const resizeObserver = new ResizeObserver((entries) => {
const entry = entries.at(0); const entry = entries.at(0);

View File

@@ -12,7 +12,7 @@ import (
) )
var ( var (
wsConnections = make(map[*websocket.Conn]bool) wsConnections = make(map[*websocket.Conn]string)
wsMutex sync.Mutex wsMutex sync.Mutex
) )
@@ -161,11 +161,16 @@ func handleMessage(msg string, ws *websocket.Conn) error {
func InitWs(c echo.Context) error { func InitWs(c echo.Context) error {
websocket.Handler(func(ws *websocket.Conn) { websocket.Handler(func(ws *websocket.Conn) {
wsMutex.Lock() wsMutex.Lock()
wsConnections[ws] = true wsConnections[ws] = c.RealIP()
wsMutex.Unlock() wsMutex.Unlock()
boxes := GetBoxes() boxes := GetBoxes()
msg := "" msg := "u:" + strconv.Itoa(len(wsConnections)) + ":"
for _, addr := range wsConnections {
msg += addr + ","
}
msg += "\n"
for _, box := range boxes { for _, box := range boxes {
msg += box.serialize() msg += box.serialize()
} }

View File

@@ -1,5 +1,10 @@
{{block "boxes" .}} {{block "boxes" .}}
<link rel="stylesheet" href="/css/boxes.css?v={{.BuildNumber}}" /> <link rel="stylesheet" href="/css/boxes.css?v={{.BuildNumber}}" />
<span class="counter-label">Currently Online: <span id="counter"></span></span>
<div id="ips" class="ips-container"></div>
<div class="boxes-container"> <div class="boxes-container">
<div class="boxes"> <div class="boxes">
{{range .Boxes}}<input {{range .Boxes}}<input