connect(url: string, callback: (socket: Socket) => void, headers?: Record<string, string>): Response
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. |
Type | Description |
object, containing the response result returned by ws.connect. |
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();});});}
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);}