SocketTask.close

Close the WebSocket connection.

Prerequisites:

  • Supported SDK versions:
    • Android: 2.67.0
    • iOS: 2.80.0
  • Supported types of mini programs: DSL

Sample code

See the following JavaScript sample code:

copy
const socketTask = my.connectSocket({
  url: 'wss://...',
  multiple: true, 
})
socketTask.onClose(()=>{
  console.log('WebSocket is closed!');
});
socketTask.close();

Parameters

The incoming parameter is of the Object type with the following properties:

Property

Type

Required

Description

code

Number

No

The close status code of the WebSocket connection.

Valid value:

  • 1000:this is the default value. Indicates the connection is closed normally. Or
  • 3000 ~ 4999: customize the close status code.

reason

String

No

The close reason of the WebSocket connection.

success

Function

No

The callback function for a successful API call.

fail

Function

No

The callback function for a failed API call.

complete

Function

No

The callback function used when the API call is completed. This function is always executed no matter the call succeeds or fails.

success/fail/complete callback function

The following table provides the properties in the success/fail/complete callback function:

Property

Data type

Description

error

Number

The error code. 0 indicates success. For more information, see the following Error code section.

errorMessage

String

The error message.

Error codes

Error code

Error message

Further action

2

Code can only be 1000, or between 3000 and 4999

The close status code of the WebSocket connection can only be 1000 or between 3000 and 4999. Check the code parameter carefully.