The **socket** module implements data transfer over TCPSocket, UDPSocket, WebSocket, and TLSSocket connections.
The **socket** module implements data transfer over TCP, UDP, Web, and TLS socket connections.
> **NOTE**
> **NOTE**
>
>
...
@@ -34,7 +34,7 @@ let udp = socket.constructUDPSocketInstance();
...
@@ -34,7 +34,7 @@ let udp = socket.constructUDPSocketInstance();
## UDPSocket<sup>7+</sup>
## UDPSocket<sup>7+</sup>
Defines a UDPSocket connection. Before calling UDPSocket APIs, you need to call [socket.constructUDPSocketInstance](#socketconstructudpsocketinstance) to create a **UDPSocket** object.
Defines a UDP socket connection. Before calling UDPSocket APIs, you need to call [socket.constructUDPSocketInstance](#socketconstructudpsocketinstance) to create a **UDPSocket** object.
| options | [UDPSendOptions](#udpsendoptions) | Yes | Parameters for sending data over the UDPSocket connection. For details, see [UDPSendOptions](#udpsendoptions).|
| options | [UDPSendOptions](#udpsendoptions) | Yes | Parameters for sending data over the UDP socket connection. For details, see [UDPSendOptions](#udpsendoptions).|
| callback | AsyncCallback\<void\> | Yes | Callback used to return the result. |
| callback | AsyncCallback\<void\> | Yes | Callback used to return the result. |
**Error codes**
**Error codes**
...
@@ -164,7 +164,7 @@ udp.send({
...
@@ -164,7 +164,7 @@ udp.send({
send(options: UDPSendOptions): Promise\<void\>
send(options: UDPSendOptions): Promise\<void\>
Sends data over a UDPSocket connection. This API uses a promise to return the result.
Sends data over a UDP socket connection. This API uses a promise to return the result.
Before sending data, call [UDPSocket.bind()](#bind) to bind the IP address and port.
Before sending data, call [UDPSocket.bind()](#bind) to bind the IP address and port.
...
@@ -176,7 +176,7 @@ Before sending data, call [UDPSocket.bind()](#bind) to bind the IP address and p
...
@@ -176,7 +176,7 @@ Before sending data, call [UDPSocket.bind()](#bind) to bind the IP address and p
| options | [UDPSendOptions](#udpsendoptions) | Yes | Parameters for sending data over the UDPSocket connection. For details, see [UDPSendOptions](#udpsendoptions).|
| options | [UDPSendOptions](#udpsendoptions) | Yes | Parameters for sending data over the UDP socket connection. For details, see [UDPSendOptions](#udpsendoptions).|
**Error codes**
**Error codes**
...
@@ -214,7 +214,7 @@ promise.then(() => {
...
@@ -214,7 +214,7 @@ promise.then(() => {
close(callback: AsyncCallback\<void\>): void
close(callback: AsyncCallback\<void\>): void
Closes a UDPSocket connection. This API uses an asynchronous callback to return the result.
Closes a UDP socket connection. This API uses an asynchronous callback to return the result.
| options | [UDPExtraOptions](#udpextraoptions) | Yes | Other properties of the UDPSocket connection. For details, see [UDPExtraOptions](#udpextraoptions).|
| options | [UDPExtraOptions](#udpextraoptions) | Yes | Other properties of the UDP socket connection. For details, see [UDPExtraOptions](#udpextraoptions).|
| callback | AsyncCallback\<void\> | Yes | Callback used to return the result. |
| callback | AsyncCallback\<void\> | Yes | Callback used to return the result. |
| options | [UDPExtraOptions](#udpextraoptions) | Yes | Other properties of the UDPSocket connection. For details, see [UDPExtraOptions](#udpextraoptions).|
| options | [UDPExtraOptions](#udpextraoptions) | Yes | Other properties of the UDP socket connection. For details, see [UDPExtraOptions](#udpextraoptions).|
Disables listening for message receiving events of the UDPSocket connection. This API uses an asynchronous callback to return the result.
Unsubscribes from **message** events of the UDP socket connection. This API uses an asynchronous callback to return the result.
> **NOTE**
> **NOTE**
> You can pass the callback of the **on** function if you want to cancel listening for a certain type of event. If you do not pass the callback, you will cancel listening for all events.
> You can pass the callback of the **on** function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
// You can pass the callback of the on method to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
// You can pass the callback of the on function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
Enables listening for data packet message events or close events of the UDPSocket connection. This API uses an asynchronous callback to return the result.
Subscribes to **listening** events or **close** events of the UDP socket connection. This API uses an asynchronous callback to return the result.
Disables listening for data packet message events or close events of the UDPSocket connection. This API uses an asynchronous callback to return the result.
Unsubscribes from **listening** events or **close** events of the UDP socket connection. This API uses an asynchronous callback to return the result.
> **NOTE**
> **NOTE**
> You can pass the callback of the **on** function if you want to cancel listening for a certain type of event. If you do not pass the callback, you will cancel listening for all events.
> You can pass the callback of the **on** function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
// You can pass the callback of the on method to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
// You can pass the callback of the on function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
udp.off('listening',callback1);
udp.off('listening',callback1);
udp.off('listening');
udp.off('listening');
letcallback2=()=>{
letcallback2=()=>{
console.log("on close, success");
console.log("on close, success");
}
}
udp.on('close',callback2);
udp.on('close',callback2);
// You can pass the callback of the on method to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
// You can pass the callback of the on function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
udp.off('close',callback2);
udp.off('close',callback2);
udp.off('close');
udp.off('close');
```
```
...
@@ -598,7 +598,7 @@ udp.off('close');
...
@@ -598,7 +598,7 @@ udp.off('close');
on(type: 'error', callback: ErrorCallback): void
on(type: 'error', callback: ErrorCallback): void
Enables listening for error events of the UDPSocket connection. This API uses an asynchronous callback to return the result.
Subscribes to **error** events of the UDP socket connection. This API uses an asynchronous callback to return the result.
Disables listening for error events of the UDPSocket connection. This API uses an asynchronous callback to return the result.
Unsubscribes from **error** events of the UDP socket connection. This API uses an asynchronous callback to return the result.
> **NOTE**
> **NOTE**
> You can pass the callback of the **on** function if you want to cancel listening for a certain type of event. If you do not pass the callback, you will cancel listening for all events.
> You can pass the callback of the **on** function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
// You can pass the callback of the on method to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
// You can pass the callback of the on function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
udp.off('error',callback);
udp.off('error',callback);
udp.off('error');
udp.off('error');
```
```
...
@@ -663,7 +663,7 @@ Defines the destination address.
...
@@ -663,7 +663,7 @@ Defines the destination address.
## UDPSendOptions<sup>7+</sup>
## UDPSendOptions<sup>7+</sup>
Defines the parameters for sending data over the UDPSocket connection.
Defines the parameters for sending data over the UDP socket connection.
@@ -684,7 +684,7 @@ Defines other properties of the UDPSocket connection.
...
@@ -684,7 +684,7 @@ Defines other properties of the UDPSocket connection.
| receiveBufferSize | number | No | Size of the receive buffer, in bytes. The default value is **0**. |
| receiveBufferSize | number | No | Size of the receive buffer, in bytes. The default value is **0**. |
| sendBufferSize | number | No | Size of the send buffer, in bytes. The default value is **0**. |
| sendBufferSize | number | No | Size of the send buffer, in bytes. The default value is **0**. |
| reuseAddress | boolean | No | Whether to reuse addresses. The default value is **false**. |
| reuseAddress | boolean | No | Whether to reuse addresses. The default value is **false**. |
| socketTimeout | number | No | Timeout duration of the UDPSocket connection, in ms. The default value is **0**.|
| socketTimeout | number | No | Timeout duration of the UDP socket connection, in ms. The default value is **0**.|
## SocketStateBase<sup>7+</sup>
## SocketStateBase<sup>7+</sup>
...
@@ -739,7 +739,7 @@ let tcp = socket.constructTCPSocketInstance();
...
@@ -739,7 +739,7 @@ let tcp = socket.constructTCPSocketInstance();
## TCPSocket<sup>7+</sup>
## TCPSocket<sup>7+</sup>
Defines a TCPSocket connection. Before calling TCPSocket APIs, you need to call [socket.constructTCPSocketInstance](#socketconstructtcpsocketinstance) to create a **TCPSocket** object.
Defines a TCP socket connection. Before calling TCPSocket APIs, you need to call [socket.constructTCPSocketInstance](#socketconstructtcpsocketinstance) to create a **TCPSocket** object.
Binds the IP address and port number. The port number can be specified or randomly allocated by the system. This API uses an asynchronous callback to return the result.
Binds the IP address and port number. The port number can be specified or randomly allocated by the system. This API uses an asynchronous callback to return the result.
> **NOTE**
> If the operation fails, the system randomly allocates a port number.
Binds the IP address and port number. The port number can be specified or randomly allocated by the system. This API uses a promise to return the result.
Binds the IP address and port number. The port number can be specified or randomly allocated by the system. This API uses a promise to return the result.
> **NOTE**
> If the operation fails, the system randomly allocates a port number.
| options | [TCPSendOptions](#tcpsendoptions) | Yes | Parameters for sending data over the TCPSocket connection. For details, see [TCPSendOptions](#tcpsendoptions).|
| options | [TCPSendOptions](#tcpsendoptions) | Yes | Parameters for sending data over the TCP socket connection. For details, see [TCPSendOptions](#tcpsendoptions).|
| callback | AsyncCallback\<void\> | Yes | Callback used to return the result. |
| callback | AsyncCallback\<void\> | Yes | Callback used to return the result. |
| options | [TCPSendOptions](#tcpsendoptions) | Yes | Parameters for sending data over the TCPSocket connection. For details, see [TCPSendOptions](#tcpsendoptions).|
| options | [TCPSendOptions](#tcpsendoptions) | Yes | Parameters for sending data over the TCP socket connection. For details, see [TCPSendOptions](#tcpsendoptions).|
**Return value**
**Return value**
...
@@ -1002,7 +1008,7 @@ promise1.then(() => {
...
@@ -1002,7 +1008,7 @@ promise1.then(() => {
close(callback: AsyncCallback\<void\>): void
close(callback: AsyncCallback\<void\>): void
Closes a TCPSocket connection. This API uses an asynchronous callback to return the result.
Closes a TCP socket connection. This API uses an asynchronous callback to return the result.
| callback | AsyncCallback\<number\> | Yes | Callback used to return the result. If the operation is successful, the file descriptor of the socket is returned. Otherwise, **undefined** is returned.|
**Example**
```js
importsocketfrom"@ohos.net.socket";
vartcp=socket.constructTCPSocketInstance();
lettunnelfd=0
tcp.bind({
address:"0.0.0.0",
family:1,
})
letconnectAddress={
address:"192.168.1.11",
port:8888,
family:1
};
tcp.connect({
address:connectAddress,timeout:6000
})
tcp.getSocketFd((data)=>{
console.info("tunenlfd: "+data);
tunnelfd=data
})
```
### getSocketFd<sup>10+</sup>
getSocketFd(): Promise\<number\>
Obtains the file descriptor of the **TCPSocket** object. This API uses a promise to return the result.
> **NOTE**
> This API can be called only after **bind** or **connect** is successfully called.
| options | [TCPExtraOptions](#tcpextraoptions) | Yes | Other properties of the TCPSocket connection. For details, see [TCPExtraOptions](#tcpextraoptions).|
| options | [TCPExtraOptions](#tcpextraoptions) | Yes | Other properties of the TCP socket connection. For details, see [TCPExtraOptions](#tcpextraoptions).|
| callback | AsyncCallback\<void\> | Yes | Callback used to return the result. |
| callback | AsyncCallback\<void\> | Yes | Callback used to return the result. |
**Error codes**
**Error codes**
...
@@ -1291,7 +1378,7 @@ let promise = tcp.connect({ address: { address: '192.168.xx.xxx', port: xxxx, fa
...
@@ -1291,7 +1378,7 @@ let promise = tcp.connect({ address: { address: '192.168.xx.xxx', port: xxxx, fa
| options | [TCPExtraOptions](#tcpextraoptions) | Yes | Other properties of the TCPSocket connection. For details, see [TCPExtraOptions](#tcpextraoptions).|
| options | [TCPExtraOptions](#tcpextraoptions) | Yes | Other properties of the TCP socket connection. For details, see [TCPExtraOptions](#tcpextraoptions).|
Disables listening for message receiving events of the TCPSocket connection. This API uses an asynchronous callback to return the result.
Unsubscribes from **message** events of the TCP socket connection. This API uses an asynchronous callback to return the result.
> **NOTE**
> **NOTE**
> You can pass the callback of the **on** function if you want to cancel listening for a certain type of event. If you do not pass the callback, you will cancel listening for all events.
> You can pass the callback of the **on** function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
// You can pass the callback of the on method to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
// You can pass the callback of the on function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
Disables listening for connection or close events of the TCPSocket connection. This API uses an asynchronous callback to return the result.
Unsubscribes from connection or close events of the TCP socket connection. This API uses an asynchronous callback to return the result.
> **NOTE**
> **NOTE**
> You can pass the callback of the **on** function if you want to cancel listening for a certain type of event. If you do not pass the callback, you will cancel listening for all events.
> You can pass the callback of the **on** function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
// You can pass the callback of the on method to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
// You can pass the callback of the on function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
tcp.off('connect',callback1);
tcp.off('connect',callback1);
tcp.off('connect');
tcp.off('connect');
letcallback2=()=>{
letcallback2=()=>{
console.log("on close success");
console.log("on close success");
}
}
tcp.on('close',callback2);
tcp.on('close',callback2);
// You can pass the callback of the on method to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
// You can pass the callback of the on function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
tcp.off('close',callback2);
tcp.off('close',callback2);
tcp.off('close');
tcp.off('close');
```
```
...
@@ -1484,7 +1571,7 @@ tcp.off('close');
...
@@ -1484,7 +1571,7 @@ tcp.off('close');
on(type: 'error', callback: ErrorCallback): void
on(type: 'error', callback: ErrorCallback): void
Enables listening for error events of the TCPSocket connection. This API uses an asynchronous callback to return the result.
Subscribes to **error** events of the TCP socket connection. This API uses an asynchronous callback to return the result.
Disables listening for error events of the TCPSocket connection. This API uses an asynchronous callback to return the result.
Unsubscribes from **error** events of the TCP socket connection. This API uses an asynchronous callback to return the result.
> **NOTE**
> **NOTE**
> You can pass the callback of the **on** function if you want to cancel listening for a certain type of event. If you do not pass the callback, you will cancel listening for all events.
> You can pass the callback of the **on** function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
// You can pass the callback of the on method to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
// You can pass the callback of the on function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
| keepAlive | boolean | No | Whether to keep the connection alive. The default value is **false**. |
| keepAlive | boolean | No | Whether to keep the connection alive. The default value is **false**. |
| OOBInline | boolean | No | Whether to enable OOBInline. The default value is **false**. |
| OOBInline | boolean | No | Whether to enable OOBInline. The default value is **false**. |
| TCPNoDelay | boolean | No | Whether to enable no-delay on the TCPSocket connection. The default value is **false**. |
| TCPNoDelay | boolean | No | Whether to enable no-delay on the TCP socket connection. The default value is **false**. |
| socketLinger | Object | Yes | Socket linger.<br>- **on**: whether to enable socket linger. The value true means to enable socket linger and false means the opposite.<br>- **linger**: linger time, in ms. The value ranges from **0** to **65535**.<br>Specify this parameter only when **on** is set to **true**.|
| socketLinger | Object | Yes | Socket linger.<br>- **on**: whether to enable socket linger. The value true means to enable socket linger and false means the opposite.<br>- **linger**: linger time, in ms. The value ranges from **0** to **65535**.<br>Specify this parameter only when **on** is set to **true**.|
| receiveBufferSize | number | No | Size of the receive buffer, in bytes. The default value is **0**. |
| receiveBufferSize | number | No | Size of the receive buffer, in bytes. The default value is **0**. |
| sendBufferSize | number | No | Size of the send buffer, in bytes. The default value is **0**. |
| sendBufferSize | number | No | Size of the send buffer, in bytes. The default value is **0**. |
| reuseAddress | boolean | No | Whether to reuse addresses. The default value is **false**. |
| reuseAddress | boolean | No | Whether to reuse addresses. The default value is **false**. |
| socketTimeout | number | No | Timeout duration of the UDPSocket connection, in ms. The default value is **0**. |
| socketTimeout | number | No | Timeout duration of the UDP socket connection, in ms. The default value is **0**. |
Binds the IP address and port number. The port number can be specified or randomly allocated by the system. The server listens to and accepts TCPSocket connections established over the socket. Multiple threads are used to process client data concurrently. This API uses an asynchronous callback to return the result.
Binds the IP address and port number. The port number can be specified or randomly allocated by the system. The server listens to and accepts TCP socket connections established over the socket. Multiple threads are used to process client data concurrently. This API uses an asynchronous callback to return the result.
> **NOTE**
> **NOTE**
> The server uses this API to perform the bind, listen, and accept operations.
> The server uses this API to perform the **bind**, **listen**, and **accept** operations. If the **bind** operation fails, the system randomly allocates a port number.
Binds the IP address and port number. The port number can be specified or randomly allocated by the system. The server listens to and accepts TCPSocket connections established over the socket. Multiple threads are used to process client data concurrently. This API uses a promise to return the result.
Binds the IP address and port number. The port number can be specified or randomly allocated by the system. The server listens to and accepts TCP socket connections established over the socket. Multiple threads are used to process client data concurrently. This API uses a promise to return the result.
> **NOTE**
> **NOTE**
> The server uses this API to perform the bind, listen, and accept operations.
> The server uses this API to perform the **bind**, **listen**, and **accept** operations. If the **bind** operation fails, the system randomly allocates a port number.
Disables listening for TCPSocketServer connection events. This API uses an asynchronous callback to return the result.
Unsubscribes from TCPSocketServer connection events. This API uses an asynchronous callback to return the result.
> **NOTE**
> **NOTE**
> You can pass the callback of the **on** function if you want to cancel listening for a certain type of event. If you do not pass the callback, you will cancel listening for all events.
> You can pass the callback of the **on** function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
// You can pass the callback of the on method to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
// You can pass the callback of the on function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
tcpServer.off('connect',callback);
tcpServer.off('connect',callback);
tcpServer.off('connect');
tcpServer.off('connect');
```
```
...
@@ -1975,7 +2065,10 @@ tcpServer.off('connect');
...
@@ -1975,7 +2065,10 @@ tcpServer.off('connect');
on(type: 'error', callback: ErrorCallback): void
on(type: 'error', callback: ErrorCallback): void
Enables listening for error events of the TCPSocketServer connection. This API uses an asynchronous callback to return the result.
Subscribes to **error** events of the TCPSocketServer connection. This API uses an asynchronous callback to return the result.
> **NOTE**
> This API can be called only after **listen** is successfully called.
Disables listening for error events of the TCPSocketServer connection. This API uses an asynchronous callback to return the result.
Unsubscribes from **error** events of the TCPSocketServer connection. This API uses an asynchronous callback to return the result.
> **NOTE**
> **NOTE**
> You can pass the callback of the **on** function if you want to cancel listening for a certain type of event. If you do not pass the callback, you will cancel listening for all events.
> You can pass the callback of the **on** function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
// You can pass the callback of the on method to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
// You can pass the callback of the on function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
Disables listening for**message** events of a **TCPSocketConnection** object. This API uses an asynchronous callback to return the result.
Unsubscribes from**message** events of a **TCPSocketConnection** object. This API uses an asynchronous callback to return the result.
> **NOTE**
> **NOTE**
> You can pass the callback of the **on** function if you want to cancel listening for a certain type of event. If you do not pass the callback, you will cancel listening for all events.
> You can pass the callback of the **on** function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
// You can pass the callback of the on method to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
// You can pass the callback of the on function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
Disables listening for**close** events of a **TCPSocketConnection** object. This API uses an asynchronous callback to return the result.
Unsubscribes from**close** events of a **TCPSocketConnection** object. This API uses an asynchronous callback to return the result.
> **NOTE**
> **NOTE**
> You can pass the callback of the **on** function if you want to cancel listening for a certain type of event. If you do not pass the callback, you will cancel listening for all events.
> You can pass the callback of the **on** function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
// You can pass the callback of the on method to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
// You can pass the callback of the on function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
Disables listening for**error** events of a **TCPSocketConnection** object. This API uses an asynchronous callback to return the result.
Unsubscribes from**error** events of a **TCPSocketConnection** object. This API uses an asynchronous callback to return the result.
> **NOTE**
> **NOTE**
> You can pass the callback of the **on** function if you want to cancel listening for a certain type of event. If you do not pass the callback, you will cancel listening for all events.
> You can pass the callback of the **on** function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
// You can pass the callback of the on method to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
// You can pass the callback of the on function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
client.off('error',callback);
client.off('error',callback);
client.off('error');
client.off('error');
});
});
...
@@ -2558,7 +2651,7 @@ let tls = socket.constructTLSSocketInstance();
...
@@ -2558,7 +2651,7 @@ let tls = socket.constructTLSSocketInstance();
## TLSSocket<sup>9+</sup>
## TLSSocket<sup>9+</sup>
Defines a TLSSocket connection. Before calling TLSSocket APIs, you need to call [socket.constructTLSSocketInstance](#socketconstructtlssocketinstance9) to create a **TLSSocket** object.
Defines a TLS socket connection. Before calling TLSSocket APIs, you need to call [socket.constructTLSSocketInstance](#socketconstructtlssocketinstance9) to create a **TLSSocket** object.
| callback | AsyncCallback\<[SocketStateBase](#socketstatebase)> | Yes | Callback used to return the result. If the operation is successful, the status of the TLSSocket connection is returned. If the operation fails, an error message is returned.|
| callback | AsyncCallback\<[SocketStateBase](#socketstatebase)> | Yes | Callback used to return the result. If the operation is successful, the status of the TLS socket connection is returned. If the operation fails, an error message is returned.|
| options | [TCPExtraOptions](#tcpextraoptions) | Yes | Other properties of the TCPSocket connection. For details, see [TCPExtraOptions](#tcpextraoptions).|
| options | [TCPExtraOptions](#tcpextraoptions) | Yes | Other properties of the TCP socket connection. For details, see [TCPExtraOptions](#tcpextraoptions).|
| callback | AsyncCallback\<void\> | Yes | Callback used to return the result. If the operation is successful, the result of setting other properties of the TCPSocket connection is returned. If the operation fails, an error message is returned.|
| callback | AsyncCallback\<void\> | Yes | Callback used to return the result. If the operation is successful, the result of setting other properties of the TCP socket connection is returned. If the operation fails, an error message is returned.|
| options | [TCPExtraOptions](#tcpextraoptions) | Yes | Other properties of the TCPSocket connection. For details, see [TCPExtraOptions](#tcpextraoptions).|
| options | [TCPExtraOptions](#tcpextraoptions) | Yes | Other properties of the TCP socket connection. For details, see [TCPExtraOptions](#tcpextraoptions).|
Disables listening for **message** events of the TLSSocket connection. This API uses an asynchronous callback to return the result.
Unsubscribes from **message** events of the TLS socket connection. This API uses an asynchronous callback to return the result.
> **NOTE**
> **NOTE**
> You can pass the callback of the **on** function if you want to cancel listening for a certain type of event. If you do not pass the callback, you will cancel listening for all events.
> You can pass the callback of the **on** function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
| type | string | Yes | Type of the event to subscribe to.<br/>**message**: message receiving event.|
| type | string | Yes | Type of the event to subscribe to.<br/>**message**: message receiving event.|
| callback | Callback<{message:ArrayBuffer,remoteInfo:[SocketRemoteInfo](#socketremoteinfo)}> | No | Callback used to return the result.<br/>**message**: received message.<br>**remoteInfo**: socket connection information.|
| callback | Callback<{message:ArrayBuffer,remoteInfo:[SocketRemoteInfo](#socketremoteinfo)}> | No | Callback used to return the result.<br>**message**: received message.<br>**remoteInfo**: socket connection information.|
// You can pass the callback of the on method to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
// You can pass the callback of the on function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
Disables listening for **connect** or **close** events of the TLSSocket connection. This API uses an asynchronous callback to return the result.
Unsubscribes from **connect** or **close** events of the TLS socket connection. This API uses an asynchronous callback to return the result.
> **NOTE**
> **NOTE**
> You can pass the callback of the **on** function if you want to cancel listening for a certain type of event. If you do not pass the callback, you will cancel listening for all events.
> You can pass the callback of the **on** function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
// You can pass the callback of the on method to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
// You can pass the callback of the on function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
tls.off('connect',callback1);
tls.off('connect',callback1);
tls.off('connect');
tls.off('connect');
letcallback2=()=>{
letcallback2=()=>{
console.log("on close success");
console.log("on close success");
}
}
tls.on('close',callback2);
tls.on('close',callback2);
// You can pass the callback of the on method to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
// You can pass the callback of the on function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
Disables listening for error events of the TLSSocket connection. This API uses an asynchronous callback to return the result.
Unsubscribes from **error** events of the TLS socket connection. This API uses an asynchronous callback to return the result.
> **NOTE**
> **NOTE**
> You can pass the callback of the **on** function if you want to cancel listening for a certain type of event. If you do not pass the callback, you will cancel listening for all events.
> You can pass the callback of the **on** function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
// You can pass the callback of the on method to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
// You can pass the callback of the on function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
Sets up a TLSSocket connection, and creates and initializes a TLS session after **bind** is successfully called. During this process, a TLS/SSL handshake is performed between the application and the server to implement data transmission. This API uses an asynchronous callback to return the result.
Sets up a TLS socket connection, and creates and initializes a TLS session after **bind** is successfully called. During this process, a TLS/SSL handshake is performed between the application and the server to implement data transmission. This API uses an asynchronous callback to return the result.
| options | [TLSConnectOptions](#tlsconnectoptions9) | Yes | Parameters required for the TLSSocket connection.|
| options | [TLSConnectOptions](#tlsconnectoptions9) | Yes | Parameters required for the TLS socket connection.|
| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.|
| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.|
Sets up a TLSSocket connection, and creates and initializes a TLS session after **bind** is successfully called. During this process, a TLS/SSL handshake is performed between the application and the server to implement data transmission. Both two-way and one-way authentication modes are supported. This API uses a promise to return the result.
Sets up a TLS socket connection, and creates and initializes a TLS session after **bind** is successfully called. During this process, a TLS/SSL handshake is performed between the application and the server to implement data transmission. Both two-way and one-way authentication modes are supported. This API uses a promise to return the result.
Obtains the local digital certificate after a TLSSocket connection is established. This API is applicable to two-way authentication. It uses an asynchronous callback to return the result.
Obtains the local digital certificate after a TLS socket connection is established. This API is applicable to two-way authentication. It uses an asynchronous callback to return the result.
Obtains the local digital certificate after a TLSSocket connection is established. This API is applicable to two-way authentication. It uses a promise to return the result.
Obtains the local digital certificate after a TLS socket connection is established. This API is applicable to two-way authentication. It uses a promise to return the result.
Obtains the digital certificate of the server after a TLSSocket connection is established. This API uses an asynchronous callback to return the result.
Obtains the digital certificate of the server after a TLS socket connection is established. This API uses an asynchronous callback to return the result.
Obtains the cipher suite negotiated by both communication parties after a TLSSocket connection is established. This API uses an asynchronous callback to return the result.
Obtains the cipher suite negotiated by both communication parties after a TLS socket connection is established. This API uses an asynchronous callback to return the result.
Obtains the cipher suite negotiated by both communication parties after a TLSSocket connection is established. This API uses a promise to return the result.
Obtains the cipher suite negotiated by both communication parties after a TLS socket connection is established. This API uses a promise to return the result.
Obtains the signing algorithm negotiated by both communication parties after a TLSSocket connection is established. This API is applicable to two-way authentication. It uses an asynchronous callback to return the result.
Obtains the signing algorithm negotiated by both communication parties after a TLS socket connection is established. This API is applicable to two-way authentication. It uses an asynchronous callback to return the result.
Obtains the signing algorithm negotiated by both communication parties after a TLSSocket connection is established. This API is applicable to two-way authentication. It uses a promise to return the result.
Obtains the signing algorithm negotiated by both communication parties after a TLS socket connection is established. This API is applicable to two-way authentication. It uses a promise to return the result.
@@ -3815,7 +3908,7 @@ let tlsServer = socket.constructTLSSocketServerInstance();
...
@@ -3815,7 +3908,7 @@ let tlsServer = socket.constructTLSSocketServerInstance();
## TLSSocketServer<sup>10+</sup>
## TLSSocketServer<sup>10+</sup>
Defines a TLSSocketServer connection. Before calling TLSSocketServer APIs, you need to call [socket.constructTLSSocketServerInstance](#socketconstructtlssocketserverinstance10) to create a **TLSSocketServer** object.
Defines a TLS socket server connection. Before calling TLSSocketServer APIs, you need to call [socket.constructTLSSocketServerInstance](#socketconstructtlssocketserverinstance10) to create a **TLSSocketServer** object.
| callback | AsyncCallback\<[SocketStateBase](#socketstatebase7)> | Yes | Callback used to return the result. If the operation is successful, the status of the TLSSocketServer connection is returned. If the operation fails, an error message is returned.|
| callback | AsyncCallback\<[SocketStateBase](#socketstatebase7)> | Yes | Callback used to return the result. If the operation is successful, the status of the TLS socket server connection is returned. If the operation fails, an error message is returned.|
| options | [TCPExtraOptions](#tcpextraoptions7) | Yes | Other properties of the TLSSocketServer connection. |
| options | [TCPExtraOptions](#tcpextraoptions7) | Yes | Other properties of the TLS socket server connection. |
| callback | AsyncCallback\<void\> | Yes | Callback used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.|
| callback | AsyncCallback\<void\> | Yes | Callback used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.|
Obtains the local digital certificate after a TLSSocketServer connection is established. This API uses an asynchronous callback to return the result.
Obtains the local digital certificate after a TLS socket server connection is established. This API uses an asynchronous callback to return the result.
> **NOTE**
> **NOTE**
> This API can be called only after **listen** is successfully called.
> This API can be called only after **listen** is successfully called.
Obtains the communication protocol version after a TLSSocketServer connection is established. This API uses an asynchronous callback to return the result.
Obtains the communication protocol version after a TLS socket server connection is established. This API uses an asynchronous callback to return the result.
> **NOTE**
> **NOTE**
> This API can be called only after **listen** is successfully called.
> This API can be called only after **listen** is successfully called.
Disables listening for TLSSocketServer connection events. This API uses an asynchronous callback to return the result.
Unsubscribes from TLS socket server connection events. This API uses an asynchronous callback to return the result.
> **NOTE**
> **NOTE**
> You can pass the callback of the **on** function if you want to cancel listening for a certain type of event. If you do not pass the callback, you will cancel listening for all events.
> This API can be called only after **listen** is successfully called.
> You can pass the callback of the **on** function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
// You can pass the callback of the on method to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
// You can pass the callback of the on function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
tlsServer.off('connect',callback);
tlsServer.off('connect',callback);
tlsServer.off('connect');
tlsServer.off('connect');
```
```
### on('error')
### on('error')<sup>10+</sup>
on(type: 'error', callback: ErrorCallback): void
on(type: 'error', callback: ErrorCallback): void
Enables listening for**error** events of a **TLSSocketServer** object. This API uses an asynchronous callback to return the result.
Subscribes to**error** events of a **TLSSocketServer** object. This API uses an asynchronous callback to return the result.
> **NOTE**
> **NOTE**
> This API can be called only after **listen** is successfully called.
> This API can be called only after **listen** is successfully called.
Disables listening for**error** events of a **TLSSocketServer** object. This API uses an asynchronous callback to return the result.
Unsubscribes from**error** events of a **TLSSocketServer** object. This API uses an asynchronous callback to return the result.
> **NOTE**
> **NOTE**
> You can pass the callback of the **on** function if you want to cancel listening for a certain type of event. If you do not pass the callback, you will cancel listening for all events.
> This API can be called only after **listen** is successfully called.
> You can pass the callback of the **on** function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
// You can pass the callback of the on method to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
// You can pass the callback of the on function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
tlsServer.off('error',callback);
tlsServer.off('error',callback);
tlsServer.off('error');
tlsServer.off('error');
```
```
...
@@ -4704,7 +4799,7 @@ Defines a **TLSSocketConnection** object, that is, the connection between the TL
...
@@ -4704,7 +4799,7 @@ Defines a **TLSSocketConnection** object, that is, the connection between the TL
| data | string | Yes | Parameters for sending data over the TLSSocketServer connection. |
| data | string | Yes | Parameters for sending data over the TLS socket server connection. |
| callback | AsyncCallback\<void\> | Yes | Callback used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.|
| callback | AsyncCallback\<void\> | Yes | Callback used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.|
Obtains the digital certificate of the peer end after a TLSSocketServer connection is established. This API uses an asynchronous callback to return the result. It applies only to the scenario where the client sends a certificate to the server.
Obtains the digital certificate of the peer end after a TLS socket server connection is established. This API uses an asynchronous callback to return the result. It applies only to the scenario where the client sends a certificate to the server.
Obtains the digital certificate of the peer end after a TLSSocketServer connection is established. This API uses a promise to return the result. It applies only to the scenario where the client sends a certificate to the server.
Obtains the digital certificate of the peer end after a TLS socket server connection is established. This API uses a promise to return the result. It applies only to the scenario where the client sends a certificate to the server.
Obtains the cipher suite negotiated by both communication parties after a TLSSocketServer connection is established. This API uses an asynchronous callback to return the result.
Obtains the cipher suite negotiated by both communication parties after a TLS socket server connection is established. This API uses an asynchronous callback to return the result.
Obtains the cipher suite negotiated by both communication parties after a TLSSocketServer connection is established. This API uses a promise to return the result.
Obtains the cipher suite negotiated by both communication parties after a TLS socket server connection is established. This API uses a promise to return the result.
Obtains the signing algorithm negotiated by both communication parties after a TLSSocketServer connection is established. This API uses an asynchronous callback to return the result.
Obtains the signing algorithm negotiated by both communication parties after a TLS socket server connection is established. This API uses an asynchronous callback to return the result.
Obtains the signing algorithm negotiated by both communication parties after a TLSSocketServer connection is established. This API uses a promise to return the result.
Obtains the signing algorithm negotiated by both communication parties after a TLS socket server connection is established. This API uses a promise to return the result.
Disables listening for**message** events of a **TLSSocketConnection** object. This API uses an asynchronous callback to return the result.
Unsubscribes from**message** events of a **TLSSocketConnection** object. This API uses an asynchronous callback to return the result.
> **NOTE**
> **NOTE**
> You can pass the callback of the **on** function if you want to cancel listening for a certain type of event. If you do not pass the callback, you will cancel listening for all events.
> You can pass the callback of the **on** function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
// You can pass the callback of the on method to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
// You can pass the callback of the on function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
Disables listening for**close** events of a **TLSSocketConnection** object. This API uses an asynchronous callback to return the result.
Unsubscribes from**close** events of a **TLSSocketConnection** object. This API uses an asynchronous callback to return the result.
> **NOTE**
> **NOTE**
> You can pass the callback of the **on** function if you want to cancel listening for a certain type of event. If you do not pass the callback, you will cancel listening for all events.
> You can pass the callback of the **on** function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
// You can pass the callback of the on method to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
// You can pass the callback of the on function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
Disables listening for**error** events of a **TLSSocketConnection** object. This API uses an asynchronous callback to return the result.
Unsubscribes from**error** events of a **TLSSocketConnection** object. This API uses an asynchronous callback to return the result.
> **NOTE**
> **NOTE**
> You can pass the callback of the **on** function if you want to cancel listening for a certain type of event. If you do not pass the callback, you will cancel listening for all events.
> You can pass the callback of the **on** function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
// You can pass the callback of the on method to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
// You can pass the callback of the on function if you want to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.