Skip to main content
Version: 3.x

Taro.onSocketClose(callback)

Listens on the event of disabling the WebSocket connection.

Reference

Type

(callback: Callback) => void

Parameters

Callback

The callback function for the event of disabling the WebSocket connection.

(result: CallbackResult) => void
PropertyType
resultCallbackResult

CallbackResult

PropertyTypeDescription
codenumberA numeric value indicates the status code explaining why the connection has been disabled.
reasonstringA readable string explaining why the connection has been disabled.

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.onSocketClose✔️✔️✔️
选择下列对应的群,使用微信扫码添加,会收到入群二维码,再扫群码添加即可。