tencent cloud

All product documents
Tencent Cloud Observability Platform
ws.connect
Last updated: 2025-03-11 20:32:30
ws.connect
Last updated: 2025-03-11 20:32:30
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
Response
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);
}


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

Feedback

Contact Us

Contact our sales team or business advisors to help your business.

Technical Support

Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

7x24 Phone Support