菜单
开源

Socket.ping()

注意

存在一个 API 更好且更标准的模块。

新的 k6/experimental/websockets API 部分实现了 WebSockets API living standard

如果可能,我们建议使用新的 API。它使用全局事件循环,与其他 k6 API 保持一致,并具有更好的性能。

发送 ping。Ping 消息可用于验证远程端点是否响应正常。

示例

JavaScript
import ws from 'k6/ws';

export default function () {
  const url = 'ws://echo.websocket.org';
  const response = ws.connect(url, null, function (socket) {
    socket.on('open', function () {
      socket.on('pong', function () {
        // As required by the spec, when the ping is received, the recipient must send back a pong.
        console.log('connection is alive');
      });

      socket.ping();
    });
  });
}