Skip to main content
Version: Next

Taro.closeSocket(option)

Disables the WebSocket connection

Reference

Type​

(option?: Option) => Promise<CallbackResult>

Parameters​

Option​

PropertyTypeRequiredDescription
codenumberNoA numeric value indicates the status code explaining why the connection has been disabled.
reasonstringNoA readable string explaining why the connection has been disabled. This string must be a UTF-8-encoded text (not characters) with not more than 123 bytes.
complete(res: CallbackResult) => voidNoThe callback function used when the API call completed (always executed whether the call succeeds or fails)
fail(res: CallbackResult) => voidNoThe callback function for a failed API call
success(res: CallbackResult) => voidNoThe callback function for a successful API call

Sample Code​

Taro.connectSocket({
url: 'test.php'
})

//Pay attention to the operation sequence
//If you call wx.closeSocket before the wx.onSocketOpen callback for wx.connectSocket , WebSocket connection cannot be disabled.
//You can only call wx.closeSocket to disable the connection when WebSocket is enabled.
Taro.onSocketOpen(function() {
Taro.closeSocket()
})
Taro.onSocketClose(function(res) {
console.log('WebSocket Disabled!')
})

API Support​

APIWeChat Mini-ProgramBaidu Smart-ProgramAlipay Mini-ProgramH5React Native
Taro.closeSocket✔️✔️✔️