-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
33 lines (28 loc) · 837 Bytes
/
server.js
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
const http2 = require("http2");
const { HTTP2_HEADER_PATH } = http2.constants;
const server = http2.createServer();
server.on("error", console.error);
server.on("stream", (stream, headers) => {
(async () => {
let disconnected = false;
stream.on("close", () => {
disconnected = true;
});
for (let i = 1; i <= 10; i++) {
if (disconnected || !stream.pushAllowed) {
break;
}
stream.pushStream(
{ [HTTP2_HEADER_PATH]: "/random" + i },
(err, pushStream) => {
console.log(`push ${i}`);
pushStream.respond({ ":status": 200 });
pushStream.end(`push ${i}`);
}
);
await new Promise((resolve) => setTimeout(resolve, Math.random() * 2000));
}
if (!disconnected) stream.end("end stream");
})();
});
server.listen(3000);