WeChat Open Platform

Mini Programs Documentation

WebSocket

wx.connectSocket(OBJECT)

Creates a WebSocket connnection. A Mini Program can only have one WebSocket connection at a time. If a WebSocket connection already exists, this connection will close automatically and a new WebSocket connection will be created.

OBJECT parameter descriptions:

Parameter Type Required Description
url String Yes Developer servers must be WSS protocol and domain names must be legitimate domain names for background configuration
data Object No Request protocol
header Object No HTTP header, referers cannot be set in the header
method String No Default is GET, valid values: OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
success Function No Callback function for successful interface call
fail Function No Callback function for failed interface call
complete Function No Callback function for interface call results (will be executed if call succeeds or fails)

Sample code:

wx.connectSocket({
  url: 'test.php',
  data:{
    x: '',
    y: ''
  },
  header:{ 
    'content-type': 'application/json'
  },
  method:"GET"
})

wx.onSocketOpen(CALLBACK)

Monitors WebSocket connection open events.

Sample code:

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

wx.onSocketError(CALLBACK)

Monitors WebSocket errors.

Sample code:

wx.connectSocket({
  url: 'test.php'
})
wx.onSocketOpen(function(res){
  console.log('WebSocket connection has been opened!')
})
wx.onSocketError(function(res){
  console.log('Failed to open WebSocket connection, please check!')
})

wx.sendSocketMessage(OBJECT)

Sends data via a WebSocket connection. wx.connectSocket is required first, and data can only be sent after the wx.onSocketOpen callback.

OBJECT parameter descriptions:

Parameter Type Required Description
data String/ArrayBuffer Yes The content that needs to be sent
success Function No Callback function for successful interface call
fail Function No Callback function for failed interface call
complete Function No Callback function for interface call results (will be executed if 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)

Monitors server message events received by WebSocket.

CALLBACK return parameters:

Parameter Type Description
data String/ArrayBuffer Message returned by server

Sample code:

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

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

wx.closeSocket()

Closes WebSocket connection.

wx.onSocketClose(CALLBACK)

Monitors closing of WebSocket.

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

//Note that there are timing problems here,
//if wx.connectSocket still hasn't called back wx.onSocketOpen, and wx.closeSocket is called first, then WebSocket cannot be closed.
//WebSocket can only be closed if wx.closeSocket is called while WebSocket is open.
wx.onSocketOpen(function() {
  wx.closeSocket()
})

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

Bugs & Tips

  1. tip: createSocket connection default timeout length and maximum timeout length are both 60s.
  2. tip: The network referer cannot be set, its format is fixed as https://servicewechat.com/{appid}/{version}/page-frame.html, wherein {appid} is the Mini Program's appid and {version} is the Mini Program's version number. If the version number is 0 this indicates that it is the development version.