tencent cloud

문서Performance Testing Service

Module Overview

다운로드
포커스 모드
폰트 크기
마지막 업데이트 시간: 2025-03-10 17:28:32
The pts/ws module in the JavaScript API is used to implement basic features based on the Websocket protocol.

Methodology

Methodology
Return Type
Description
Establish a WebSocket connection and define the business logic in the callback function. After the callback function has been executed, ws.connect returns a ws.Response object.

Object

Object
Description
After successfully establishing the connection with ws.connect, pass the ws.Socket object into the callback function to define the WebSocket request logic.
The ws.Response object returned by ws.connect after executing the callback function.

Samples

import ws from 'pts/ws';
import { check, sleep } from 'pts';

export default function () {
const res = ws.connect('ws://localhost:8080/echo', function (socket) {
socket.on('open', () => console.log('connected'));
socket.on('message', (data) => console.log('message received: ', data));
socket.on('close', () => console.log('disconnected'));
socket.on('ping', () => console.log('ping'));
socket.on('pong', () => console.log('pong'));
socket.on('error', (e) => console.log('error happened', e.error()));
socket.send('message');
socket.setTimeout(function () {
console.log('3 seconds passed, closing the socket');
socket.close();
}, 3000);
socket.setInterval(function () {
socket.ping();
}, 500);
socket.setLoop(function () {
sleep(0.1)
socket.send('loop message')
});
});
check('status is 101', () => res.status === 101);
};


도움말 및 지원

문제 해결에 도움이 되었나요?

피드백