http2session.ping([payload, ]callback)
payload<Buffer> | <TypedArray> | <DataView> 可选的 ping 负载。callback<Function>- 返回:<boolean>
向已连接的 HTTP/2 对等方发送 PING 帧。必须提供一个 callback 函数。如果 PING 发送成功,该方法将返回 true,否则返回 false。
【Sends a PING frame to the connected HTTP/2 peer. A callback function must
be provided. The method will return true if the PING was sent, false
otherwise.】
maxOutstandingPings 配置选项决定了未确认 ping 的最大数量。默认最大值为 10。
【The maximum number of outstanding (unacknowledged) pings is determined by the
maxOutstandingPings configuration option. The default maximum is 10.】
如果提供,payload 必须是一个包含 8 字节数据的 Buffer、TypedArray 或 DataView,这些数据将随 PING 发送,并在 ping 确认时返回。
【If provided, the payload must be a Buffer, TypedArray, or DataView
containing 8 bytes of data that will be transmitted with the PING and
returned with the ping acknowledgment.】
回调函数将接收三个参数:一个错误参数,如果 PING 被成功确认,该参数将为 null;一个 duration 参数,报告从发送 ping 到收到确认所经过的毫秒数;以及一个包含 8 字节 PING 数据的 Buffer。
【The callback will be invoked with three arguments: an error argument that will
be null if the PING was successfully acknowledged, a duration argument
that reports the number of milliseconds elapsed since the ping was sent and the
acknowledgment was received, and a Buffer containing the 8-byte PING
payload.】
session.ping(Buffer.from('abcdefgh'), (err, duration, payload) => {
if (!err) {
console.log(`Ping acknowledged in ${duration} milliseconds`);
console.log(`With payload '${payload.toString()}'`);
}
}); 如果未指定 payload 参数,默认的负载将是标记 PING 持续时间开始的 64 位时间戳(小端序)。
【If the payload argument is not specified, the default payload will be the
64-bit timestamp (little endian) marking the start of the PING duration.】