Skip to content

Commit 1717075

Browse files
websocket demo
1 parent 69b6663 commit 1717075

File tree

4 files changed

+160
-0
lines changed

4 files changed

+160
-0
lines changed

devtools-websocket-demo/index.html

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>WebSocket test page</title>
8+
<link rel="icon" type="image/png" href="https://edgestatic.azureedge.net/welcome/static/favicon.png">
9+
10+
<style>
11+
body {
12+
font-family: system-ui;
13+
margin: 2rem;
14+
}
15+
16+
h1 {
17+
margin: 2rem 0;
18+
}
19+
20+
button {
21+
font-family: inherit;
22+
font-size: inherit;
23+
}
24+
25+
#output {
26+
padding: 0;
27+
margin: 2rem 0;
28+
border: 1px solid;
29+
border-block-end: none;
30+
border-radius: 1rem;
31+
overflow: hidden;
32+
}
33+
34+
#output:empty {
35+
display: none;
36+
}
37+
38+
#output li {
39+
padding: 1rem;
40+
border-block-end: 1px solid;
41+
list-style: none;
42+
overflow: hidden;
43+
text-overflow: ellipsis;
44+
}
45+
46+
#output li.odd {
47+
background: #eee;
48+
}
49+
</style>
50+
</head>
51+
52+
<body>
53+
<h1>WebSocket test page</h1>
54+
55+
<button id="connect">Connect to the server</button>
56+
<button id="send" hidden>Send a message to the server</button>
57+
<ul id="output"></ul>
58+
59+
<script>
60+
const connectBtn = document.getElementById("connect");
61+
const sendBtn = document.getElementById("send");
62+
const output = document.getElementById("output");
63+
64+
let msgIndex = 0;
65+
function logActivity(message) {
66+
const li = document.createElement("li");
67+
li.textContent = message;
68+
li.classList.add(msgIndex % 2 === 0 ? "even" : "odd");
69+
msgIndex++;
70+
output.prepend(li);
71+
}
72+
73+
connectBtn.addEventListener("click", () => {
74+
logActivity("Connecting...");
75+
76+
connectBtn.disabled = true;
77+
// TODO: change URL to match the ws server location.
78+
const socket = new WebSocket("ws://localhost:8080");
79+
80+
socket.addEventListener("open", (event) => {
81+
sendBtn.hidden = false;
82+
logActivity("Connected to the server");
83+
});
84+
85+
sendBtn.addEventListener("click", () => {
86+
const data = {};
87+
for (let i = 0; i < 3000; i++) {
88+
data["key" + i] = "z".repeat(10);
89+
}
90+
socket.send(JSON.stringify(data));
91+
logActivity("Message sent to the server");
92+
});
93+
94+
socket.addEventListener("message", (event) => {
95+
console.log("Message from server ", event.data);
96+
logActivity("Message received from the server: " + event.data.substring(0, 500));
97+
});
98+
}, { once: true });
99+
</script>
100+
</body>
101+
102+
</html>

devtools-websocket-demo/package-lock.json

Lines changed: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "devtools-websocket-demo",
3+
"type": "module",
4+
"main": "server.js",
5+
"dependencies": {
6+
"ws": "^8.18.3"
7+
}
8+
}

devtools-websocket-demo/server.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { WebSocketServer } from 'ws';
2+
3+
const wss = new WebSocketServer({ port: 8080 });
4+
5+
wss.on('connection', function connection(ws, req) {
6+
ws.on('error', console.error);
7+
8+
const ip = req.socket.remoteAddress;
9+
console.log(`New connection from ${ip}`);
10+
11+
ws.on('message', function message(data) {
12+
// Message received from a client.
13+
// Sending the same message back to the client.
14+
ws.send("" + data);
15+
});
16+
});

0 commit comments

Comments
 (0)