如何在Node.js中向服务器发送HTTP / 2帧

时间:2016-09-21 07:34:30

标签: node.js http connection ping http2

使用以下代码:

var req = http2.request({
    hostname: 'api.push.apple.com',
    protocol: 'https:',
    port: 443,
    method: 'POST',
    path: '/3/device/' + deviceToken,
    agent: new http2.Agent({log: logger}),
    headers: {
        'apns-topic': 'web.com.example'
    },
    cert: cert,
    key: key,
}, function(res) {
    res.pipe(process.stdout);
});
req.write(JSON.stringify(post));
req.end();

如何发送PING框架以检查我与node-http2模块的连接是否健康?

提前致谢。

1 个答案:

答案 0 :(得分:0)

来自Node.js HTTP/2 Documentation

session.ping(Buffer.from('12345678'), (err, duration, payload) => {
  if (!err) {
    console.log(`Ping acknowledged in ${duration} milliseconds`);
    console.log(`With payload '${payload.toString()}'`);
  }
});