tencent cloud

Performance Testing Service

ws.connect

PDF
Focus Mode
Font Size
Last updated: 2025-03-10 17:28:33
ws.connect establishes a WebSocket connection based on specified parameters, executes user-defined logic, and returns a Response object.
connect(url: string, callback: (socket: Socket) => void, headers?: Record<string, string>): Response

Parameters

Parameter
Type
Description
url
string
The address of the requested connection.
callback
function
The callback function, which is called after the connection is established, passes the ws.Socket object into this function. Users can define WebSocket request logic in this function.
headers (optional)
Record<string, string>
The headers configuration when initiating a connection request.

Return

Type
Description
object, containing the response result returned by ws.connect.

Samples

Establish a connection:
import ws from 'pts/ws';

export default function () {
const res = ws.connect("ws://localhost:8080/echo", function (socket) {
socket.on('open', () => {
console.log('connected');
socket.close();
});
});
}
Establish a connection and specify the headers parameter:
import ws from 'pts/ws';

export default function () {
const headers = {
'X-MyApplication': 'PTS',
'X-MyScript': 'Websocket',
}
const res = ws.connect("ws://localhost:8080/echo", function (socket) {
socket.on('open', () => {
console.log('connected');
socket.close();
});
}, headers);
}



Help and Support

Was this page helpful?

Help us improve! Rate your documentation experience in 5 mins.

Feedback