Skip to content

Commit 443e447

Browse files
docs(examples): add example with WebTransport
1 parent 2f6cc2f commit 443e447

File tree

7 files changed

+151
-0
lines changed

7 files changed

+151
-0
lines changed

examples/webtransport/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.pem

examples/webtransport/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
# Socket.IO WebTransport example
3+
4+
## How to use
5+
6+
```shell
7+
# generate a self-signed certificate
8+
$ ./generate_cert.sh
9+
10+
# install dependencies
11+
$ npm i
12+
13+
# start the server
14+
$ node index.js
15+
16+
# open a Chrome browser
17+
$ ./open_chrome.sh
18+
```
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
openssl req -new -x509 -nodes \
3+
-out cert.pem \
4+
-keyout key.pem \
5+
-newkey ec \
6+
-pkeyopt ec_paramgen_curve:prime256v1 \
7+
-subj '/CN=127.0.0.1' \
8+
-days 14

examples/webtransport/index.html

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Socket.IO WebTransport exampleqg</title>
6+
</head>
7+
<body>
8+
<script src="/socket.io/socket.io.js"></script>
9+
<script>
10+
const socket = io({
11+
transportOptions: {
12+
webtransport: {
13+
hostname: "127.0.0.1"
14+
}
15+
}
16+
});
17+
18+
socket.on("connect", () => {
19+
console.log(`connect ${socket.id}`);
20+
21+
socket.io.engine.on("upgrade", (transport) => {
22+
console.log(`transport upgraded to ${transport.name}`);
23+
});
24+
});
25+
26+
socket.on("connect_error", (err) => {
27+
console.log(`connect_error due to ${err.message}`);
28+
});
29+
30+
socket.on("disconnect", (reason) => {
31+
console.log(`disconnect due to ${reason}`);
32+
});
33+
</script>
34+
</body>
35+
</html>

examples/webtransport/index.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { readFileSync } from "node:fs";
2+
import { createServer } from "node:https";
3+
import { Server } from "socket.io";
4+
import { Http3Server } from "@fails-components/webtransport";
5+
6+
const key = readFileSync("./key.pem");
7+
const cert = readFileSync("./cert.pem");
8+
9+
const httpsServer = createServer({
10+
key,
11+
cert
12+
}, (req, res) => {
13+
if (req.method === "GET" && req.url === "/") {
14+
const content = readFileSync("./index.html");
15+
res.writeHead(200, {
16+
"content-type": "text/html"
17+
});
18+
res.write(content);
19+
res.end();
20+
} else {
21+
res.writeHead(404).end();
22+
}
23+
});
24+
25+
const io = new Server(httpsServer, {
26+
transports: ["polling", "websocket", "webtransport"]
27+
});
28+
29+
const port = process.env.PORT || 3000;
30+
31+
io.on("connection", (socket) => {
32+
console.log(`connect ${socket.id}`);
33+
34+
socket.conn.on("upgrade", (transport) => {
35+
console.log(`transport upgraded to ${transport.name}`);
36+
});
37+
38+
socket.on("disconnect", (reason) => {
39+
console.log(`disconnect ${socket.id} due to ${reason}`);
40+
});
41+
});
42+
43+
httpsServer.listen(port, () => {
44+
console.log(`server listening at https://localhost:${port}`);
45+
});
46+
47+
const h3Server = new Http3Server({
48+
port,
49+
host: "0.0.0.0",
50+
secret: "changeit",
51+
cert,
52+
privKey: key,
53+
});
54+
55+
(async () => {
56+
const stream = await h3Server.sessionStream("/socket.io/");
57+
const sessionReader = stream.getReader();
58+
59+
while (true) {
60+
const { done, value } = await sessionReader.read();
61+
if (done) {
62+
break;
63+
}
64+
io.engine.onWebTransportSession(value);
65+
}
66+
})();
67+
68+
h3Server.startServer();

examples/webtransport/open_chrome.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
HASH=`openssl x509 -pubkey -noout -in cert.pem |
3+
openssl pkey -pubin -outform der |
4+
openssl dgst -sha256 -binary |
5+
base64`
6+
7+
/opt/google/chrome/chrome \
8+
--origin-to-force-quic-on=127.0.0.1:3000 \
9+
--ignore-certificate-errors-spki-list=$HASH \
10+
https://localhost:3000

examples/webtransport/package.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "webtransport",
3+
"version": "0.0.1",
4+
"description": "",
5+
"private": true,
6+
"type": "module",
7+
"dependencies": {
8+
"@fails-components/webtransport": "^0.1.7",
9+
"socket.io": "^4.7.1"
10+
}
11+
}

0 commit comments

Comments
 (0)