wx.connectSocket(OBJECT)

Create a WebSocket link. Please read instructions before using.

Before base library 1.7.0, one Mini Program can only have one "WebSocket" link at one time, if a "WebSocket" link already exists, that link will be automatically closed and a "WebSocket" link will be re-created. For base library 1.7.0 and later versions, multiple "WebSokcet" links are supported, a new SocketTask will be returned whenever "wx.connectSocket" is called successfully.

OBJECT parameter description:

Parameter Type Required Description Minimum Version
url String Yes The developer server interface address must be in "wss" protocol, and a valid domain name must be configured by the backend
header Object No HTTP Header , "Referer" cannot be set within "header"
method String No The default is "GET", Valid values: OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
protocols StringArray No Sub-protocol array 1.4.0
success Function No Callback function when interface call succeeded
fail Function No Callback function when interface call failed
complete Function No Callback function when interface call completed (always executed whether call succeeds or fails)

Sample code:

wx.connectSocket({
  url: 'wss://example.qq.com',
  data:{
    x: '',
    y: ''
  },
  header:{ 
    'content-type': 'application/json'
  },
  protocols: ['protocol1'],
  method:"GET"
})

wx.onSocketOpen(CALLBACK)

Monitor "WebSocket" link opening events.

callback function

Parameter
res
Attribute Type Description Supported versions
header object HTTP response "Header" linked successfully 2.0.0

Sample code:

wx.connectSocket({
  url: 'test.php'
})
wx.onSocketOpen(function(res) {
  console.log("WebSocket link has been opened!")
})

wx.onSocketError(CALLBACK)

Monitor "WebSocket" errors.

Sample code:

wx.connectSocket({
  url: 'test.php'
})
wx.onSocketOpen(function(res){
  console.log("WebSocket link has been opened!")
})
wx.onSocketError(function(res){
  console.log(Open "WebSocket link failed, please check!")
})

wx.sendSocketMessage(OBJECT)

To send data through a "WebSocket" link first requires wx.connectSocket, and can only send after a wx.onSocketOpen callback.

OBJECT parameter description:

Parameter Type Required Description
data String/ArrayBuffer Yes Content to be sent
success Function No Callback function when interface call succeeded
fail Function No Callback function when interface call failed
complete Function No Callback function when interface call completed (always executed whether call succeeds or fails)

Sample code:

var socketOpen = false
var socketMsgQueue = []
wx.connectSocket({
  url: 'test.php'
})

wx.onSocketOpen(function(res) {
  socketOpen = true
  for (var i = 0; i < socketMsgQueue.length; i++){
     sendSocketMessage(socketMsgQueue[i])
  }
  socketMsgQueue = []
})

function sendSocketMessage(msg) {
  if (socketOpen) {
    wx.sendSocketMessage({
      data:msg
    })
  } else {
     socketMsgQueue.push(msg)
  }
}

wx.onSocketMessage(CALLBACK)

Monitor message events received by "WebSocket" from the server.

CALLBACK return parameter:

Parameter Type Description
data String/ArrayBuffer Messages returned by the server

Sample code:

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

wx.onSocketMessage(function(res) {
  console.log(Received server content:" + res.data)
})

wx.closeSocket(OBJECT)

Close "WebSocket" link.

OBJECT parameter description:

Parameter Type Required Description Minimum Version
code Number No A numerical value, representing the status code for closing the connection, which indicates the reason for closing the connection. If this parameter is not assigned, the default value is 1000 (indicating that the connection is closed normally) 1.4.0
reason String No A readable string, representing the reason for closing the connection. This string must be in UTF-8 text no longer than 123 bytes (non-characters) 1.4.0
success Function No Callback function when interface call succeeded
fail Function No Callback function when interface call failed
complete Function No Callback function when interface call completed (always executed whether call succeeds or fails)

wx.onSocketClose(CALLBACK)

Monitor closing of "WebSocket".

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

//Take note that there are time sequence problems here,
//If "wx.connectSocket" has not callback "wx.onSocketOpen", and calls "wx.closeSocket" first, then the objective of closing "WebSocket" cannot be met.
//It can be closed only if "wx.closeSocketduring" is called when "WebSocket" is open.
wx.onSocketOpen(function() {
  wx.closeSocket()
})

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

Return value:

Start from base library version 1.7.0. Please remaining backward compatible.

Return a SocketTask.

Bug & Tip

  1. tip: Starting from base library 1.7.0 , 2 "WebSocket" links can exist at the same time

个结果 ""

    没有找到相关内容 ""