Network
# Initiate a request
# request
Base Library 1.3.9 is supported, iOS version 2.1.23, Android version 2.1.38
request(Object object)
Make an HTTP network request using native APIs and return the RequestTask object after execution.
Parameters
Object object
| Properties | Type | The default value is | Required | Description |
|---|---|---|---|---|
| url | string | Yes | Developer server interface address | |
| data | string/object/ArrayBuffer | No | The parameter of the request | |
| header | Object | No | Set the requested header, in which the Referer cannot be set. content-type defaults to application/json | |
| method | string | GET | No | HTTP request method |
| dataType | string | json | No | The returned data format |
| responseType | string | text | No | The data type of the response |
| success | function | No | The interface calls the callback function successfully | |
| fail | function | No | The callback function that failed the interface callback function | |
| complete | function | No | The callback function at the end of the interface call (the call succeeds or fails) |
object.method' legal value
| The value | Description |
|---|---|
| OPTIONS | HTTP requests options |
| GET | HTTP request GET |
| HEAD | HTTP requests THE HEAD |
| POST | HTTP requests POST |
| PUT | HTTP requests PUT |
| DELETE | HTTP requests a DELETE |
| TRACE | THE HTTP request TRACE |
| CONNECT | HTTP requests CONNECT |
Legal value for object.dataType
| The value | Description |
|---|---|
| json | The returned data is JSON, after which a JSON.parse |
| Other | Json.parse |
Legal value for object.responseType
| The value | Description |
|---|---|
| text | The data for the response is text |
| arraybuffer | The data for the response is arrayBuffer |
object.success callback function
Parameters
Object res
| Properties | Type | Description |
|---|---|---|
| data | string/object/ArrayBuffer | The developer server returns data |
| statusCode | number | The developer server returns the HTTP status code |
| header | Object | The HTTP Response Header returned by the developer server |
Sample Code
# RequestTask.abort
Base Library 2.4.2 is supported, iOS version 2.13.59, Android version 2.13.34
RequestTask.abort()
Interrupt the request task
Sample Code
Base Library 2.4.2 is supported, iOS version 2.13.59, Android version 2.13.34
RequestTask.onHeadersReceived(function callback)
Listen for HTTP Response Header events earlier than the request completion event.
Parameters
function callback
The callback function for the Header Received event
Callback function parameters
Object res
| Properties | Type | Description |
|---|---|---|
| header | Object | The HTTP Response Header returned by the developer server |
Base Library 2.4.2 is supported, iOS version 2.13.59, Android version 2.13.34
RequestTask.offHeadersReceived(function callback)
Cancels listening for HTTP Response Header events.
Parameters
function callback
The callback function for the Header Received event
# Download
# downloadFile
Base Library 1.3.9 is supported, iOS version 2.1.23, Android version 2.1.38
downloadFile(Object object)
Download the file resource to your local location. The client directly initiates an HTTPS GET request that returns the local temporary path to the file.
Returns a DownloadTask after the call.
note
Specify a reasonable Content-Type field in the header of the server-side response to ensure that the client handles the file type correctly.
Parameters
Object object
| Properties | Type | The default value is | Required | Description |
|---|---|---|---|---|
| url | string | Yes | The url of the download resource | |
| header | Object | No | Set the header of the request, in which the Referer | |
| success | function | No | The interface calls the callback function successfully | |
| fail | function | No | The callback function that failed the interface callback function | |
| complete | function | No | The callback function at the end of the interface call (the call succeeds or fails) |
object.success callback function
Parameters
Object res
| Properties | Type | Description |
|---|---|---|
| tempFilePath | string | Temporary file path. |
| statusCode | number | The developer server returns the HTTP status code |
Sample Code
# DownloadTask.abort
Base Library 2.4.2 is supported, iOS version 2.13.59, Android version 2.13.34
DownloadTask.abort()
Interrupt the request task
Sample Code
Base Library 2.4.2 is supported, iOS version 2.13.59, Android version 2.13.34
DownloadTask.onHeadersReceived(function callback)
Listen for HTTP Response Header events earlier than the request completion event.
Parameters
function callback
The callback function for the Header Received event
Callback function parameters
Object res
| Properties | Type | Description |
|---|---|---|
| header | Object | The HTTP Response Header returned by the developer server |
Base Library 2.4.2 is supported, iOS version 2.13.59, Android version 2.13.34
DownloadTask.offHeadersReceived(function callback)
Cancels listening for HTTP Response Header events.
Parameters
function callback
The callback function for the Header Received event
# DownloadTask.onProgressUpdate
Base Library 2.4.2 is supported, iOS version 2.13.59, Android version 2.13.34
DownloadTask.onProgressUpdate(function callback)
Listen for download progress change events.
Parameters
function callback
The callback function for the Progress Update event
Callback function parameters
Object res
| Properties | Type | Description |
|---|---|---|
| progress | number | Download progress percentage |
| totalBytesWritten | number | The length of the data that has been downloaded, in bytes |
| totalBytesExpectedToWrite | number | The total length of data expected to be downloaded, in Bytes |
Sample Code
# DownloadTask.offProgressUpdate
Base Library 2.4.2 is supported, iOS version 2.13.59, Android version 2.13.34
DownloadTask.offProgressUpdate(function callback)
Cancels listening for download progress changes.
Parameters
function callback
The callback function for the Progress Update event
# Upload
# uploadFile
Base Library 1.3.9 is supported, iOS version 2.1.23, Android version 2.1.38
uploadFile(Object object)
Upload local resources to the server. The client initiates an HTTPS POST request with content-type multipart/form-data.
Returns UploadTask after the call.
Parameters
Object object
| Properties | Type | The default value is | Required | Description |
|---|---|---|---|---|
| url | string | Yes | Developer server address | |
| filePath | string | Yes | The path to upload the file resource | |
| name | string | Yes | The key corresponding to the file, the developer can obtain the binary content of the file through this key on the server side | |
| header | Object | No | Set the header of the request, in which the Referer | |
| formData | Object | No | Other additional form data | |
| success | function | No | The interface calls the callback function successfully | |
| fail | function | No | The callback function that failed the interface callback function | |
| complete | function | No | The callback function at the end of the interface call (the call succeeds or fails) |
object.success callback function
Parameters
Object res
| Properties | Type | Description |
|---|---|---|
| data | string | The developer server returns data |
| statusCode | number | The developer server returns the HTTP status code |
Sample Code
# UploadTask.abort
Base Library 2.4.2 is supported, iOS version 2.13.59, Android version 2.13.34
UploadTask.abort()
Interrupt the request task
Sample Code
Base Library 2.4.2 is supported, iOS version 2.13.59, Android version 2.13.34
UploadTask.onHeadersReceived(function callback)
Listen for HTTP Response Header events earlier than the request completion event.
Parameters
function callback
The callback function for the Header Received event
Callback function parameters
Object res
| Properties | Type | Description |
|---|---|---|
| header | Object | The HTTP Response Header returned by the developer server |
Base Library 2.4.2 is supported, iOS version 2.13.59, Android version 2.13.34
UploadTask.offHeadersReceived(function callback)
Cancels listening for HTTP Response Header events.
Parameters
function callback
The callback function for the Header Received event
# UploadTask.onProgressUpdate
Base Library 2.4.2 is supported, iOS version 2.13.59, Android version 2.13.34
UploadTask.onProgressUpdate(function callback)
Listen for download progress change events.
Parameters
function callback
The callback function for the Progress Update event
Callback function parameters
Object res
| Properties | Type | Description |
|---|---|---|
| progress | number | Download progress percentage |
| totalBytesWritten | number | The length of the data that has been downloaded, in bytes |
| totalBytesExpectedToWrite | number | The total length of data expected to be downloaded, in Bytes |
Sample Code
# UploadTask.offProgressUpdate
Base Library 2.4.2 is supported, iOS version 2.13.59, Android version 2.13.34
UploadTask.offProgressUpdate(function callback)
Cancels listening for download progress changes.
Parameters
The callback function for the function callbackProgress Update event
# Websocket
# sendSocketMessage
ft.sendSocketMessage(Object object)
Send data over a WebSocket connection. You need to ft.connectSocket first and send it after the ft.onSocketOpen callback.
Parameters
Object object
| Properties | Type | The default value is | Required | Description |
|---|---|---|---|---|
| data | string/ArrayBuffer | Yes | What needs to be sent | |
| success | function | No | The interface calls the callback function successfully | |
| fail | function | No | The callback function that failed the interface callback function | |
| complete | function | No | The callback function at the end of the interface call (the call succeeds or fails) |
Sample Code
# onSocketOpen
ft.onSocketOpen(function callback)
Listen for WebSocket connection open events
Parameters
function callback
A callback function for a WebSocket connection open event
Parameters
| Properties | Type | Description |
|---|---|---|
| header | object | The HTTP response header |
# onSocketMessage
ft.onSocketMessage(function callback)
Listens for webSocket message events that are accepted to the server
Parameters
function callback
WebSocket accepts a callback function for a message event to the server
Parameters
Object res
| Properties | Type | Description |
|---|---|---|
| data | string/ArrayBuffer | The server returned the message |
# onSocketError
ft.onSocketError(function callback)
Listen for WebSocket error events
Parameters
function callback
A callback function for webSocket error events
Parameters
# Object res
| Properties | Type | Description |
|---|---|---|
| errMsg | string | Error message |
# onSocketClose
ft.onSocketClose(function callback)
Listen for WebSocket connection closure events
Parameters
function callback
A callback function for the WebSocket connection close event
Parameters
# Object res
| Properties | Type | Description |
|---|---|---|
| code | number | A numeric value represents the status number of the closed connection and indicates why the connection was closed. |
| reason | string | A readable string that represents why the connection was closed. |
# connectSocket
SocketTask ft.connectSocket(Object object)
Create a WebSocket connection. Please read the instructions before use.
Parameters
Object object
| Properties | Type | The default value is | Required | Description | Minimum version |
|---|---|---|---|---|---|
| url | string | Yes | Developer Server wss interface address | ||
| header | Object | No | HTTP Header, the Referer | cannot be set in the Header | |
| protocols | Array.<string> | No | Subprotocol array | 1.4.0 | |
| tcpNoDelay | boolean | false | No | The TCP_NODELAY set | when a TCP connection is established 2.4.0 |
| perMessageDeflate | boolean | false | No | Whether to turn on the Scale-Out | 2.8.0 |
| timeout | number | No | The timeout period, in milliseconds, | 2.10.0 | |
| success | function | No | The interface calls the callback function successfully | ||
| fail | function | No | The callback function that failed the interface callback function | ||
| complete | function | No | The callback function at the end of the interface call (the call succeeds or fails) |
Return Value
SocketTask
WebSocket tasks
Sample Code
# closeSocket
ft.closeSocket(Object object)
Close the WebSocket connection
Parameters
Object object
| Properties | Type | The default value is | Required | Description |
|---|---|---|---|---|
| code | number | 1000 (indicates graceful closure of the connection) | No | A numeric value represents the status number of the closed connection and indicates why the connection was closed. |
| reason | string | No | A readable string that represents why the connection was closed. This string must be UTF-8 text (not characters | |
| success | function | No | The interface calls the callback function successfully | |
| fail | function | No | The callback function that failed the interface callback function | |
| complete | function | No | The callback function at the end of the interface call (the call succeeds or fails) |
Sample Code
# SocketTask
WebSocket task, which can be created through the ft.connectSocket() interface
Method
- SocketTask.send(Object object)
Send data over a WebSocket connection - SocketTask.close(Object object)
Close the WebSocket connection - SocketTask.onOpen(function callback)
Listen for WebSocket connection open events - SocketTask.onClose(function callback)
Listen for WebSocket connection closure events - SocketTask.onError(function callback)
Listen for WebSocket error events - SocketTask.onMessage(function callback)
Listens for webSocket message events that are accepted to the server
# TCP communication
# ft.createTCPSocket
ft.createTCPSocket() Creates a TCP Socket instance
Return Value
TCPSocket
A TCP Socket instance
Connection Limit
- Allow communication with non-native IPs within the LAN
- Connections to the following port numbers are prohibited: '1024 or less' '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
888892009300100511008011211270172701827019` - Create up to 20 TCPSockets every 5 minutes
# TCPSocket
A TCP Socket instance that uses the IPv4 protocol by default
Method
TCPSocket.connect(Object options)
Initiates a connection on the given socket
TCPSocket.write(string| ArrayBuffer data)
Sends data on the socket
TCPSocket.close()
Close the connection
TCPSocket.onClose(function callback)
The event is emitted by the listener as soon as the socket is completely closed
TCPSocket.offClose(function callback)
Cancel the listening issues the event as soon as the socket is completely closed
TCPSocket.onConnect(function callback)
The listener triggers the event when a socket connection is successfully established
TCPSocket.offConnect(function callback)
Cancels the listening event when a socket connection is successfully established
TCPSocket.onError(function callback)
The listener is triggered when an error occurs
TCPSocket.offError(function callback)
Cancels the listening triggered when an error occurs
TCPSocket.onMessage(function callback)
The listener triggers the event when data is received
TCPSocket.offMessage(function callback)
Cancels the listening event that fires when data is received
Error
| Error code | Error message | Description |
|---|---|---|
| -1 | System error | |
| -2 | Socket interface error, you can refer to the system's socket error code | |
| -3 | Send failed without interface permission | |
| -4 | Link failed | |
| 1 | Send failed, parameter error, address invalid | |
| 2 | Failed to send, parameter error, port invalid |
# UDP communication
# ft.createUDPSocket
ft.createUDPSocket() Creates a UDP Socket instance
Return Value
UDPSocket
An instance of a UDP Socket
# UDPSocket
An instance of a UDP Socket that uses the IPv4 protocol by default.
Method
number UDPSocket.bind(number port)
Binds a system-randomly assigned available port, or binds a specified port number
UDPSocket.setTTL(number ttl)
Set the IP_TTL socket option to set the maximum number of hops allowed when an IP packet is transmitted
UDPSocket.send(Object object)
Sends a message to the specified IP and port
UDPSocket.connect(Object object)
Pre-connect to the specified IP and port, which needs to be used in conjunction with the write method
UDPSocket.write()
The usage is the same as the send method, and there is no difference from send if connect is not called beforehand (note that even if connect is called, you need to fill in the address and port parameters on this interface)
UDPSocket.close()
Shutting down the UDP Socket instance is equivalent to destroying it. After shutdown, the UDP Socket instance can no longer send messages, each call to UDPSocket.send will trigger an error event, and the message event callback function will no longer execute. After a UDPSocket instance is created, it will be strongly referenced by Native, guaranteeing that it will not be GC-free. Strong references to UDPSocket.close are removed, leaving the UDPSocket instance to comply with the GC.
UDPSocket.onClose(function callback)
Listen for shutdown events
UDPSocket.offClose(function callback)
Cancels listening for shutdown events
UDPSocket.onError(function callback)
Listen for error events
UDPSocket.offError(function callback)
Cancels listening for error events
UDPSocket.onListening(function callback)
Listen for events that start listening for packet messages
UDPSocket.offListening(function callback)
Cancel listening for events that start listening for packet messages
UDPSocket.onMessage(function callback)
Listen for events that receive messages
UDPSocket.offMessage(function callback)
Cancels listening for events that receive messages
Error
| Error code | Error message | Description |
|---|---|---|
| -1 | System error | |
| -2 | socket interface error | |
| -3 | Send failed without interface permission | |
| 1 | Send failed, parameter error, address invalid | |
| 2 | Failed to send, parameter error, port invalid |