-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.html
More file actions
95 lines (84 loc) · 2.59 KB
/
index.html
File metadata and controls
95 lines (84 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>WebSocket Live Test Rig</title>
<style>
body {
font-family: sans-serif;
padding: 20px;
background: #121212;
color: #fff;
}
#log {
background: #000;
padding: 10px;
height: 300px;
overflow-y: scroll;
border: 1px solid #333;
}
.status-on {
color: #00ff00;
}
.status-off {
color: #ff0000;
}
</style>
</head>
<body>
<h1>WebSocket Broadcast Console</h1>
<p id="status" class="status-off">Connecting...</p>
<form id="message-form">
<input
id="message-input"
type="text"
placeholder="Send cargo..."
required
/>
<button type="submit">Deploy Message</button>
</form>
<h3>Live Stream Logs:</h3>
<pre id="log"></pre>
<script>
const statusEl = document.getElementById("status");
const log = document.getElementById("log");
const input = document.getElementById("message-input");
// 1. Initiate the Handshake
const socket = new WebSocket("ws://localhost:8080");
const appendLog = (label, message) => {
const entry = `${new Date().toLocaleTimeString()} ${label} ${message}\n`;
log.textContent = entry + log.textContent; // Prepend for fast reading
};
// 2. Event Listeners (The WebSocket Lifecycle)
socket.addEventListener("open", () => {
statusEl.textContent = "CONNECTED: ws://localhost:8080";
statusEl.className = "status-on";
appendLog("[SYSTEM]", "Tunnel Established. 🚀");
});
socket.addEventListener("message", (event) => {
// This is where the broadcast from OTHER clients appears
appendLog("[RECEIVED]", event.data);
});
socket.addEventListener("close", () => {
statusEl.textContent = "DISCONNECTED";
statusEl.className = "status-off";
appendLog("[SYSTEM]", "Tunnel Collapsed. 👋");
});
// 3. Sending Data
document
.getElementById("message-form")
.addEventListener("submit", (e) => {
e.preventDefault();
// SENIOR CHECK: Don't try to send if the socket is dead
if (socket.readyState !== WebSocket.OPEN) {
appendLog("[ERROR]", "No active tunnel found.");
return;
}
const msg = input.value.trim();
socket.send(msg); // Sending raw text through the tunnel
appendLog("[SENT]", msg);
input.value = "";
});
</script>
</body>
</html>