在客户端推流


¥Push streams on the client

要在客户端接收推送流,则在 ClientHttp2Session 上为 'stream' 事件设置监听器:

¥To receive pushed streams on the client, set a listener for the 'stream' event on the ClientHttp2Session:

const http2 = require('node:http2');

const client = http2.connect('http://localhost');

client.on('stream', (pushedStream, requestHeaders) => {
  pushedStream.on('push', (responseHeaders) => {
    // Process response headers
  });
  pushedStream.on('data', (chunk) => { /* handle pushed data */ });
});

const req = client.request({ ':path': '/' });