tencent cloud

Module Overview
Last updated: 2025-03-11 20:32:30
Module Overview
Last updated: 2025-03-11 20:32:30
The pts/ws module in the JavaScript API is used to implement basic features based on the Websocket protocol.

Methodology

Methodology
Return Type
Description
Response
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
Socket
After successfully establishing the connection with ws.connect, pass the ws.Socket object into the callback function to define the WebSocket request logic.
Response
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);
};

Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback