-
Notifications
You must be signed in to change notification settings - Fork 10.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(examples): add example with WebTransport
- Loading branch information
1 parent
2f6cc2f
commit 443e447
Showing
7 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.pem |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
|
||
# Socket.IO WebTransport example | ||
|
||
## How to use | ||
|
||
```shell | ||
# generate a self-signed certificate | ||
$ ./generate_cert.sh | ||
|
||
# install dependencies | ||
$ npm i | ||
|
||
# start the server | ||
$ node index.js | ||
|
||
# open a Chrome browser | ||
$ ./open_chrome.sh | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#!/bin/bash | ||
openssl req -new -x509 -nodes \ | ||
-out cert.pem \ | ||
-keyout key.pem \ | ||
-newkey ec \ | ||
-pkeyopt ec_paramgen_curve:prime256v1 \ | ||
-subj '/CN=127.0.0.1' \ | ||
-days 14 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Socket.IO WebTransport exampleqg</title> | ||
</head> | ||
<body> | ||
<script src="/socket.io/socket.io.js"></script> | ||
<script> | ||
const socket = io({ | ||
transportOptions: { | ||
webtransport: { | ||
hostname: "127.0.0.1" | ||
} | ||
} | ||
}); | ||
|
||
socket.on("connect", () => { | ||
console.log(`connect ${socket.id}`); | ||
|
||
socket.io.engine.on("upgrade", (transport) => { | ||
console.log(`transport upgraded to ${transport.name}`); | ||
}); | ||
}); | ||
|
||
socket.on("connect_error", (err) => { | ||
console.log(`connect_error due to ${err.message}`); | ||
}); | ||
|
||
socket.on("disconnect", (reason) => { | ||
console.log(`disconnect due to ${reason}`); | ||
}); | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { readFileSync } from "node:fs"; | ||
import { createServer } from "node:https"; | ||
import { Server } from "socket.io"; | ||
import { Http3Server } from "@fails-components/webtransport"; | ||
|
||
const key = readFileSync("./key.pem"); | ||
const cert = readFileSync("./cert.pem"); | ||
|
||
const httpsServer = createServer({ | ||
key, | ||
cert | ||
}, (req, res) => { | ||
if (req.method === "GET" && req.url === "/") { | ||
const content = readFileSync("./index.html"); | ||
res.writeHead(200, { | ||
"content-type": "text/html" | ||
}); | ||
res.write(content); | ||
res.end(); | ||
} else { | ||
res.writeHead(404).end(); | ||
} | ||
}); | ||
|
||
const io = new Server(httpsServer, { | ||
transports: ["polling", "websocket", "webtransport"] | ||
}); | ||
|
||
const port = process.env.PORT || 3000; | ||
|
||
io.on("connection", (socket) => { | ||
console.log(`connect ${socket.id}`); | ||
|
||
socket.conn.on("upgrade", (transport) => { | ||
console.log(`transport upgraded to ${transport.name}`); | ||
}); | ||
|
||
socket.on("disconnect", (reason) => { | ||
console.log(`disconnect ${socket.id} due to ${reason}`); | ||
}); | ||
}); | ||
|
||
httpsServer.listen(port, () => { | ||
console.log(`server listening at https://localhost:${port}`); | ||
}); | ||
|
||
const h3Server = new Http3Server({ | ||
port, | ||
host: "0.0.0.0", | ||
secret: "changeit", | ||
cert, | ||
privKey: key, | ||
}); | ||
|
||
(async () => { | ||
const stream = await h3Server.sessionStream("/socket.io/"); | ||
const sessionReader = stream.getReader(); | ||
|
||
while (true) { | ||
const { done, value } = await sessionReader.read(); | ||
if (done) { | ||
break; | ||
} | ||
io.engine.onWebTransportSession(value); | ||
} | ||
})(); | ||
|
||
h3Server.startServer(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#!/bin/bash | ||
HASH=`openssl x509 -pubkey -noout -in cert.pem | | ||
openssl pkey -pubin -outform der | | ||
openssl dgst -sha256 -binary | | ||
base64` | ||
|
||
/opt/google/chrome/chrome \ | ||
--origin-to-force-quic-on=127.0.0.1:3000 \ | ||
--ignore-certificate-errors-spki-list=$HASH \ | ||
https://localhost:3000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"name": "webtransport", | ||
"version": "0.0.1", | ||
"description": "", | ||
"private": true, | ||
"type": "module", | ||
"dependencies": { | ||
"@fails-components/webtransport": "^0.1.7", | ||
"socket.io": "^4.7.1" | ||
} | ||
} |