How to get the frames sent by the client (HTTP2)

23 Views Asked by At

i have a piece of code to create a Http2 sever website Now I was able to get the customer header but not the frames I have tried many ways but don't know how I have tried many ways but don't know how to get the frames how i can get frame sent by the client to my site?

const cluster = require('cluster');

const http2 = require('http2');

const fs = require('fs');

const numCPUs = require('os').cpus().length;

if (cluster.isMaster) {

  console.log(`Master process is running on pid ${process.pid}`);

  // Fork workers

  for (let i = 0; i < numCPUs; i++) {

    cluster.fork();

  }

  cluster.on('exit', (worker, code, signal) => {

    console.log(`Worker process ${worker.process.pid} died`);

    // Optionally restart the worker

    cluster.fork();

  });

} else {

  // Worker process

  const server = http2.createSecureServer({

    key: fs.readFileSync('key.pem'),

    cert: fs.readFileSync('cert.pem'),

  });

  let lineCount = 0; // Variable to count the lines

  server.on('stream', (stream, headers) => {

    // 

    if (lineCount < 1000) {

      const headersString = JSON.stringify(headers) + '\n';

      fs.appendFile('h2.txt', headersString, 'utf8', (err) => {

        if (err) {

          console.error('Error writing to h2.txt:', err);

        } else {

          lineCount++;

        }

      });

    }

    // 

    stream.on('data', (chunk) => {

      console.log(`Worker ${process.pid} received data frame: ${chunk.toString()}`);

    });

    // 

    stream.respond({

      'content-type': 'text/html',

      ':status': 200,

    });

    stream.end('<h1>Hello, this is an HTTP/2 server with a self-signed certificate!</h1>');

  });

  const PORT = 443;

  server.listen(PORT, () => {

    console.log(`Worker ${process.pid} is running on port ${PORT}`);

  });

}

Can anyone help me do it? or show what the problem is here

0

There are 0 best solutions below