# wx.closeSocket(Object object)

with Promise style call: Supported

Mini Program plugin: Not supported

Stop WebSocket Connect.Recommended use SocketTask The way to manage webSocket Link, the life cycle of each link is more controllable. There are multiple simultaneous webSocket In the case of links to wx Prefixes may bring up some of the situations that are not expected.

# parameter

# Object object

attribute type Default values Required Introductions
code number 1000 (indicates normal shutdown connection) no A numeric value represents the status number for which the connection was closed and indicates why the connection is closed.
reason string no A human-readable string representing the reason the connection was closed. This string must be no longer than 123 Byte UTF-8 Text (not characters).
success function no Interface calls the successful callback function
fail function no Interface calls failed callback functions
complete function no Callback function at the end of an interface call (both successful and unsuccessful calls are executed)

# sample code

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

//Notice there's a timing problem,
//if wx.connectSocket No callback yet. wx.on SocketOpen, and call wx.closeSocket, then can't close WebSocket The purpose of.
//Must be WebSocket Open Period Call wx.closeSocket To shut it down.
wx.onSocketOpen(function() {
  wx.closeSocket()
})

wx.onSocketClose(function(res) {
  console.log('WebSocket Closed! ')
})