title: Taro.closeSocket(option)

sidebar_label: closeSocket

Disables the WebSocket connection

Reference

Type

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

Parameters

Option

Property Type Required Description
code number No A numeric value indicates the status code explaining why the connection has been disabled.
reason string No A 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) => void No The callback function used when the API call completed (always executed whether the call succeeds or fails)
fail (res: CallbackResult) => void No The callback function for a failed API call
success (res: CallbackResult) => void No The callback function for a successful API call

Sample Code

  1. Taro.connectSocket({
  2. url: 'test.php'
  3. })
  4. //Pay attention to the operation sequence
  5. //If you call wx.closeSocket before the wx.onSocketOpen callback for wx.connectSocket , WebSocket connection cannot be disabled.
  6. //You can only call wx.closeSocket to disable the connection when WebSocket is enabled.
  7. Taro.onSocketOpen(function() {
  8. Taro.closeSocket()
  9. })
  10. Taro.onSocketClose(function(res) {
  11. console.log('WebSocket Disabled!')
  12. })

API Support

API WeChat Mini-Program Baidu Smart-Program Alipay Mini-Program H5 React Native
Taro.closeSocket ✔️ ✔️ ✔️