Skip to content

Network

Initiate request

request

Base library support from 1.3.9, iOS version 2.1.23, Android version 2.1.38

request(Object object)

initiate HTTPS Network request. Please read carefully before usingRelated notes

parameter

Object object

attributetypeDefault valuesRequiredIntroductions
urlstringyesDeveloper Server Interface Address
datastring/object/ArrayBuffernoRequested parameters
headerObjectnoSet the request header,header Can not be set Referer。
content-type Default is application/json
methodstringGETnoHTTP Request method
dataTypestringjsonnoData format returned
responseTypestringtextnoData type of response
successfunctionnoInterface calls the successful callback function
failfunctionnoInterface calls failed callback functions
completefunctionnoCallback function at the end of an interface call (both successful and unsuccessful calls are executed)

object.method Legal value

valueIntroductions
OPTIONSHTTP request OPTIONS
GETHTTP request GET
HEADHTTP request HEAD
POSTHTTP request POST
PUTHTTP request PUT
DELETEHTTP request DELETE
TRACEHTTP request TRACE
CONNECTHTTP request CONNECT

object.dataType Legal value

valueIntroductions
jsonThe data returned is JSON, which is returned once for the returned data JSON.parse
其他Does not perform on the returned content JSON.parse

object.responseType Legal value

valueIntroductions
textThe data for the response is text
arraybufferThe data for the response is ArrayBuffer

object.success callback

parameter

Object res

attributetypeIntroductions
datastring/object/ArrayBufferData returned by the developer server
statusCodenumberReturned by the developer server HTTP Status code
headerObjectReturned by the developer server HTTP Response Header

sample code

javascript
ft.request({
  url: 'test.php', // Just an example, not a real interface address
  data: {
    x: '',
    y: ''
  },
  header: {
    'content-type': 'application/json' // Default values
  },
  success(res) {
    console.log(res.data)
  }
})

RequestTask.abort

Base library 2.4.2 support from iOS version 2.13.59, Android version 2.13.34

RequestTask.abort()

Interrupt request task

sample code

javascript
const task = ft.request({
  // some params
})
// Interrupt requests
task.abort()

RequestTask.onHeadersReceived

Base library 2.4.2 support from iOS version 2.13.59, Android version 2.13.34

RequestTask.onHeadersReceived(function callback)

to monitor HTTP Response Header Events. Will be earlier than the request to complete the event

parameter

function callback

HTTP Response Header Event callback function

callbackparameter

Object res

attributetypeIntroductions
headerObjectReturned by the developer server HTTP Response Header

RequestTask.offHeadersReceived

Base library 2.4.2 support from iOS version 2.13.59, Android version 2.13.34

RequestTask.offHeadersReceived(function callback)

Cancel the wiretap. HTTP Response Header event

parameter

function callback

HTTP Response Header Event callback function

RequestTask.onChunkReceived

Base library 3.2.3 support from iOS version 2.43.7, Android version 2.43.7

RequestTask.onChunkReceived(function callback)

to monitor Transfer-Encoding Chunk Received Events.

parameter

function callback

HTTP Chunk Received Event callback function

callbackparameter

Object res

attributetypeIntroductions
dataArrayBufferchunk buffer

RequestTask.offChunkReceived

Base library 3.2.3 support from iOS version 2.43.7, Android version 2.43.7

RequestTask.offChunkReceived(function callback)

Cancel the wiretap HTTP Response Header Transfer-Encoding Chunk Received Events.

参数

function callback

HTTP Chunk Received Event callback function

to download

downloadFile

Base library support from 1.3.9, iOS version 2.1.23, Android version 2.1.38

downloadFile(Object object)

Download file resources to the local. The client directly initiates a HTTPS GET Request, return the local temporary path to the file (Local path)The maximum file allowed for a single download is 200MB。 Please read carefully before usingRelated notes。

Note: Please click the header Specifies a reasonable Content-Type Field to ensure that the client handles file types correctly ​ parameter

Object object

attributetypeDefault valuesRequiredIntroductions
urlstringyesDownload resource url
headerObjectnoHTTP Requested Header,Header Can not be set Refer
successfunctionnoInterface calls the successful callback function
failfunctionnoInterface calls failed callback functions
completefunctionnoCallback function at the end of an interface call (both successful and unsuccessful calls are executed)

object.success callback

parameter

Object res

attributetypeIntroductions
tempFilePathstringTemporary file path (Local path)No incoming filePath When you specify a file storage path, the downloaded file is stored in a temporary file
statusCodenumberReturned by the developer server HTTP Status code

sample code

javascript
ft.downloadFile({
  url: 'https://example.com/audio/123', // Just an example, not a real resource.
  success(res) {
    // As long as the server has the response data, the response will be written to a file and entered success Callback, the business needs to determine whether to download the desired content
    if (res.statusCode === 200) {
      ft.playVoice({
        filePath: res.tempFilePath
      })
    }
  }
})

DownloadTask.abort

Base library 2.4.2 support from iOS version 2.13.59, Android version 2.13.34

DownloadTask.abort()

Interrupt Download Task

sample code

javascript
const task = ft.request({
  // some params
})
// Interrupt requests
task.abort()

DownloadTask.onHeadersReceived

Base library 2.4.2 support from iOS version 2.13.59, Android version 2.13.34

DownloadTask.onHeadersReceived(function callback)

to monitor HTTP Response Header Events. Will be earlier than the request to complete the event

parameter

function callback

HTTP Response Header Event callback function​

callbackparameter

Object res

attributetypeIntroductions
headerObjectReturned by the developer server HTTP Response Header

DownloadTask.offHeadersReceived

Base library 2.4.2 support from iOS version 2.13.59, Android version 2.13.34

DownloadTask.offHeadersReceived(function callback)

Cancel the wiretap. HTTP Response Header event

parameter

function callback

HTTP Response Header Event callback function

DownloadTask.onProgressUpdate

Base library 2.4.2 support from iOS version 2.13.59, Android version 2.13.34

DownloadTask.onProgressUpdate(function callback)

Listen for changes in download progress

parameter

function callback

Download callback functions for progress change events

callbackparameter

Object res

attributetypeIntroductions
progressnumberDownload progress percentage
totalBytesWrittennumberLength of data downloaded in units Bytes
totalBytesExpectedToWritenumberTotal length of data expected to be downloaded, in units Bytes

sample code

javascript
const task = ft.downloadFile({
  // some params
})
// Listening for progress changes
task.onProgressUpdate((res) => {
  this.setData({
    progress: res.progress
  })
})

DownloadTask.offProgressUpdate

Base library 2.4.2 support from iOS version 2.13.59, Android version 2.13.34

DownloadTask.offProgressUpdate(function callback)

Cancel listening for download schedule change events

parameter

function callback

Download callback functions for progress change events

upload

uploadFile

Base library support from 1.3.9, iOS version 2.1.23, Android version 2.1.38

uploadFile(Object object)

Uploads local resources to the server. The client initiates a HTTPS POST Request, wherein content-type for multipart/form-data. Please read theRelated notes

parameter

Object object

attributetypeDefault valuesRequiredIntroductions
urlstringyesDeveloper Server Address
filePathstringyesPath to upload file resource (Local path)
namestringyesFile corresponding to Key, developers on the server side can use this key Get the binary content of the file
headerObjectnoHTTP Requested Header,Header Can not be set Refer
formDataObjectnoHTTP Additional requests form data
timeoutnumbernoTimeout in milliseconds
successfunctionnoInterface calls the successful callback function
failfunctionnoInterface calls failed callback functions
completefunctionnoCallback function at the end of an interface call (both successful and unsuccessful calls are executed)

object.success callback

parameter

Object res

attributetypeIntroductions
datastringData returned by the developer server
statusCodenumberReturned by the developer server HTTP Status code

sample code

javascript
ft.chooseImage({
  success(res) {
    const tempFilePaths = res.tempFilePaths
    ft.uploadFile({
      url: 'https://example.weixin.qq.com/upload', // Example only, not real interface address
      filePath: tempFilePaths[0],
      name: 'file',
      formData: {
        user: 'test'
      },
      success(res) {
        const data = res.data
        //of the something
      }
    })
  }
})

UploadTask.abort

Base library 2.4.2 support from iOS version 2.13.59, Android version 2.13.34

UploadTask.abort()

Interrupt upload task

sample code

javascript
const task = ft.request({
  // some params
})
// Interrupt requests
task.abort()

UploadTask.onHeadersReceived

Base library 2.4.2 support from iOS version 2.13.59, Android version 2.13.34

UploadTask.onHeadersReceived(function callback)

to monitor HTTP Response Header Events. Will be earlier than the request to complete the event

parameter

function callback

HTTP Response Header Event callback function

callbackparameter

Object res

attributetypeIntroductions
headerObjectReturned by the developer server HTTP Response Header

UploadTask.offHeadersReceived

Base library 2.4.2 support from iOS version 2.13.59, Android version 2.13.34

UploadTask.offHeadersReceived(function callback)

Cancel the wiretap. HTTP Response Header event

parameter

function callback

HTTP Response Header Event callback function

UploadTask.onProgressUpdate

Base library 2.4.2 support from iOS version 2.13.59, Android version 2.13.34

UploadTask.onProgressUpdate(function callback)

Monitor upload progress changes

parameter

function callback

A callback function for uploading schedule change events

callbackparameter

Object res

attributetypeIntroductions
progressnumberPercentage of upload progress
totalBytesSentnumberThe length of data that has been uploaded, in units Bytes
totalBytesExpectedToSendnumberTotal length of data expected to be uploaded, in units Bytes

sample code

javascript
const task = ft.uploadFile({
  // some params
})
// Listening for progress changes
task.onProgressUpdate((res) => {
  this.setData({
    progress: res.progress
  })
})

UploadTask.offProgressUpdate

Base library 2.4.2 support from iOS version 2.13.59, Android version 2.13.34

UploadTask.offProgressUpdate(function callback)

Cancel listening upload progress change event

parameter

function callback A callback function for uploading schedule change events

Websocket

sendSocketMessage

ft.sendSocketMessage(Object object)

adopt WebSocket Connection sends data. Need first ft.connectSocket, and the ft.onSocketOpen After the callback can be sent.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

attributetypeDefault valuesRequiredIntroductions
datastring/ArrayBufferyesWhat to send
successfunctionnoInterface calls the successful callback function
failfunctionnoInterface calls failed callback functions
completefunctionnoCallback function at the end of an interface call (both successful and unsuccessful calls are executed)

sample code

javascript
let socketOpen = false
let socketMsgQueue = []
ft.connectSocket({
  url: 'test.php'
})

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

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

onSocketOpen

ft.onSocketOpen(function callback)

to monitor WebSocket Connect to open events.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

function callback

WebSocket Connect callback functions for open events

parameter

attributetypeIntroductions
headerobjectHTTP response header for a successful connection

onSocketMessage

ft.onSocketMessage(function callback)

to monitor WebSocket Receive a message event to the server.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

function callback

WebSocket Callback function to receive message events from the server

parameter

Object res

attributetypeIntroductions
datastring/ArrayBufferMessage returned by the server

onSocketError

ft.onSocketError(function callback)

to monitor WebSocket Error event.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

function callback

WebSocket Callback function for error events

parameter

Object res
attributetypeIntroductions
errMsgstringError message

onSocketClose

ft.onSocketClose(function callback)

to monitor WebSocket Connection closure event.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

function callback

WebSocket Callback function for connection closure event

parameter

Object res
attributetypeIntroductions
codenumberA numeric value represents the status number for which the connection was closed and indicates why the connection is closed
reasonstringA human-readable string representing the reason the connection was closed.

connectSocket

SocketTask ft.connectSocket(Object object)

Create a WebSocket Connect. Please read carefully before usingRelated notes。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

attributetypeDefault valuesRequiredIntroductionsMinimum version
urlstringyesDeveloper Server wss Interface address
headerObjectnoHTTP Header,Header Can not be set Refer
protocolsArray.<string>noSubprotocol array1.4.0
tcpNoDelaybooleanfalsenoestablish TCP At the time of the connection. TCP_NODELAY Set up2.4.0
perMessageDeflatebooleanfalsenoEnable compression expansion2.8.0
timeoutnumbernoTimeout in milliseconds2.10.0
successfunctionnoInterface calls the successful callback function
failfunctionnoInterface calls failed callback functions
completefunctionnoCallback function at the end of an interface call (both successful and unsuccessful calls are executed)

Return value

SocketTask

WebSocket quest

sample code

javascript
ft.connectSocket({
  url: 'wss://example.qq.com',
  header:{
    'content-type': 'application/json'
  },
  protocols: ['protocol1']
})

closeSocket

ft.closeSocket(Object object)

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

attributetypeDefault valuesRequiredIntroductions
codenumber1000(indicates normal shutdown connection)noA numeric value represents the status number for which the connection was closed and indicates why the connection is closed.
reasonstringnoA human-readable string representing the reason the connection was closed. This string must be no longer than 123 Byte UTF-8 Text (not characters).
successfunctionnoInterface calls the successful callback function
failfunctionnoInterface calls failed callback functions
completefunctionnoCallback function at the end of an interface call (both successful and unsuccessful calls are executed)

sample code

javascript
ft.connectSocket({
  url: 'test.php'
})

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

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

SocketTask

WebSocket Task, available through ft.connectSocket() Interface creation return

method

  • SocketTask.send(Object object)
    adopt WebSocket Connect to send data

  • SocketTask.close(Object object)
    Stop WebSocket Connect

  • SocketTask.onOpen(function callback)
    to monitor WebSocket Connection Open Event

  • SocketTask.onClose(function callback)
    to monitor WebSocket Connection closure event.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

  • SocketTask.onError(function callback)
    to monitor WebSocket Error event

  • SocketTask.onMessage(function callback)
    to monitor WebSocket Message event received from server

TCP Signal communication

ft.createTCPSocket

ft.createTCPSocket() Create a TCP Socket Instance. Please read carefully before usingRelated notes

Return value

TCPSocket

One TCP Socket Example

Connection limit

  • Allowed with non - local computers in local area networks IP Signal communication
  • Allows communication with configured server domains, seeRelated notes
  • Do not connect to the following port numbers:1024 以下 1099 1433 1521 1719 1720 1723 2049 2375 3128 3306 3389 3659 4045 5060 5061 5432 5984 6379 6000 6566 7001 7002 8000-8100 8443 8888 9200 9300 10051 10080 11211 27017 27018 27019
  • each 5 Maximum creation in minutes 20 individual TCPSocket

TCPSocket

One TCP Socket Instance, by default IPv4 agreement

method

TCPSocket.connect(Object options)

Start a connection on the given socket

TCPSocket.write(string|ArrayBuffer data)

in socket Send data on

TCPSocket.close()

Close the connection

TCPSocket.onClose(function callback)

Once the wire is tapped, socket Full shutdown sends out the event

TCPSocket.offClose(function callback)

Once we cancel the wiretap socket Full shutdown sends out the event

TCPSocket.onConnect(function callback)

Listen in when a socket The event is triggered when the connection is successfully established

TCPSocket.offConnect(function callback)

Cancel the wiretap when a socket The event is triggered when the connection is successfully established

TCPSocket.onError(function callback)

Listen is triggered when an error occurs

TCPSocket.offError(function callback)

Cancel Listen Triggered when an error occurs

TCPSocket.onMessage(function callback)

Monitor triggers the event when data is received

TCPSocket.offMessage(function callback)

Cancels listening to trigger this event when data is received

error

Error codeError messageIntroductions
-1System error
-2Socket interface error, refer to the system socket error code
-3Send failed, no interface permissions
-4Link failed
1Send failed, parameter error, address illegal
2Send failed, parameter error, port illegal

UDP Signal communication

ft.createUDPSocket

ft.createUDPSocket() Create a UDP Socket Instance. Please read carefully before usingRelated notes

Return value

UDPSocket

One UDP Socket Example

UDPSocket

One UDP Socket Instance, by default IPv4 Protocol

method

number UDPSocket.bind(number port)

Bind a system randomly assigned available port or bind a specified port number

UDPSocket.setTTL(number ttl)

Set up IP_TTL Socket option to set a IP Maximum number of hops allowed for packet transmission

UDPSocket.send(Object object)

To the specified IP and port Send a message. Base library 2.9.0 Support Broadcast (The specified address is 255.255.255.255)

attributetypeDefault valuesRequiredIntroductions
addressstringyes
portnumberyes
messagestring/ArrayBufferyes
offsetnumber0nothe offset for sending data, valid only when the message is of type ArrayBuffer.
lengthnumbermessage.byteLengthnothe length of the sent data, valid only when the message is of type ArrayBuffer
setBroadcastbooleanfalsenonot supported

UDPSocket.connect(Object object)

Preconnect to the specified IP and Port, you need to cooperate. write Methods used together

UDPSocket.write()

Usage and send Method is the same if the connect With send No difference (note that even if the connect You also need to fill in the address and port parameters for this interface

UDPSocket.close()

Stop UDP Socket Instance, equivalent to destruction. After the shutdown, UDP Socket Instance can no longer send messages, and each time the UDPSocket.send Will trigger an error event, and message The event callback function also does not execute again. in UDPSocket Instance will be created by the Native Strong references, ensuring that they are not GC。 in UDPSocket.close Removes a strong reference to it, and lets the UDPSocket Instance compliance GC

UDPSocket.onClose(function callback)

Listen Close Event

UDPSocket.offClose(function callback)

Unlisten Close Event

UDPSocket.onError(function callback)

Listen for error events

UDPSocket.offError(function callback)

Unlisten Error Event

UDPSocket.onListening(function callback)

Listen to start listening for packet messages

UDPSocket.offListening(function callback)

The event that starts listening for packet messages

UDPSocket.onMessage(function callback)

Listen to events that receive messages

UDPSocket.offMessage(function callback)

Unlisten for events that receive messages

error

Error codeError messageIntroductions
-1System error
-2Socket interface error
-3Send failed, no interface permissions
1Send failed, parameter error, address illegal
2Send failed, parameter error, port illegal