-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-websocket.html
More file actions
53 lines (47 loc) · 1.43 KB
/
test-websocket.html
File metadata and controls
53 lines (47 loc) · 1.43 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
<!DOCTYPE html>
<html>
<head>
<title>WebSocket Test</title>
</head>
<body>
<h1>WebSocket Connection Test</h1>
<div id="status">Connecting...</div>
<div id="messages"></div>
<script>
const ws = new WebSocket(
"ws://localhost:5000/ws?roomId=test&username=testuser"
);
const statusDiv = document.getElementById("status");
const messagesDiv = document.getElementById("messages");
ws.onopen = function () {
statusDiv.textContent = "Connected!";
statusDiv.style.color = "green";
// Test sending a message
setTimeout(() => {
ws.send(
JSON.stringify({
action: "CODE_CHANGE",
payload: { code: 'console.log("Hello World");' },
})
);
}, 1000);
};
ws.onerror = function (error) {
statusDiv.textContent = "Connection Error: " + error;
statusDiv.style.color = "red";
};
ws.onclose = function () {
statusDiv.textContent = "Connection Closed";
statusDiv.style.color = "orange";
};
ws.onmessage = function (event) {
const message = JSON.parse(event.data);
const messageDiv = document.createElement("div");
messageDiv.textContent = `Received: ${
message.action
} - ${JSON.stringify(message.payload)}`;
messagesDiv.appendChild(messageDiv);
};
</script>
</body>
</html>