From f02e2720592d42c36589cc727a2f6d91f88b24d5 Mon Sep 17 00:00:00 2001 From: shawn_he Date: Fri, 27 May 2022 17:03:03 +0800 Subject: [PATCH] update docs Signed-off-by: shawn_he --- .../reference/apis/js-apis-socket.md | 86 ++++++++++--------- 1 file changed, 45 insertions(+), 41 deletions(-) diff --git a/en/application-dev/reference/apis/js-apis-socket.md b/en/application-dev/reference/apis/js-apis-socket.md index 2c83bd2e48..b590efb7ca 100644 --- a/en/application-dev/reference/apis/js-apis-socket.md +++ b/en/application-dev/reference/apis/js-apis-socket.md @@ -7,7 +7,7 @@ ## Modules to Import -```js +``` import socket from '@ohos.net.socket'; ``` @@ -28,7 +28,7 @@ Creates a **UDPSocket** object. **Example** -```js +``` let udp = socket.constructUDPSocketInstance(); ``` @@ -56,7 +56,7 @@ Binds the IP address and port number. The port number can be specified or random **Example** -```js +``` let udp = socket.constructUDPSocketInstance(); udp.bind({address: '192.168.xx.xxx', port: xxxx, family: 1}, err => { if (err) { @@ -93,7 +93,7 @@ Binds the IP address and port number. The port number can be specified or random **Example** -```js +``` let udp = socket.constructUDPSocketInstance(); let promise = udp.bind({address: '192.168.xx.xxx', port: 8080, family: 1}); promise .then(() => { @@ -110,6 +110,8 @@ send\(options: UDPSendOptions, callback: AsyncCallback\): void Sends data over a UDPSocket connection. This API uses an asynchronous callback to return the result. +Before sending data, call [UDPSocket.bind()](#bind) to bind the IP address and port. + **Required permission**: ohos.permission.INTERNET **System capability**: SystemCapability.Communication.NetStack @@ -123,7 +125,7 @@ Sends data over a UDPSocket connection. This API uses an asynchronous callback t **Example** -```js +``` let udp = socket.constructUDPSocketInstance(); udp.send({ data:'Hello, server!', @@ -148,6 +150,8 @@ send\(options: UDPSendOptions\): Promise Sends data over a UDPSocket connection. This API uses a promise to return the result. +Before sending data, call [UDPSocket.bind()](#bind) to bind the IP address and port. + **Required permission**: ohos.permission.INTERNET **System capability**: SystemCapability.Communication.NetStack @@ -166,7 +170,7 @@ Sends data over a UDPSocket connection. This API uses a promise to return the re **Example** -```js +``` let udp = socket.constructUDPSocketInstance(); let promise = udp.send({ data:'Hello, server!', @@ -202,7 +206,7 @@ Closes a UDPSocket connection. This API uses an asynchronous callback to return **Example** -```js +``` let udp = socket.constructUDPSocketInstance(); udp.close(err => { if (err) { @@ -232,7 +236,7 @@ Closes a UDPSocket connection. This API uses a promise to return the result. **Example** -```js +``` let udp = socket.constructUDPSocketInstance(); let promise = udp.close(); promise.then(() => { @@ -264,7 +268,7 @@ Obtains the status of the UDPSocket connection. This API uses an asynchronous ca **Example** -```js +``` let udp = socket.constructUDPSocketInstance(); udp.bind({address: '192.168.xx.xxx', port: xxxx, family: 1}, err => { if (err) { @@ -304,7 +308,7 @@ Obtains the status of the UDPSocket connection. This API uses a promise to retur **Example** -```js +``` let udp = socket.constructUDPSocketInstance(); udp.bind({address: '192.168.xx.xxx', port: xxxx, family: 1}, err => { if (err) { @@ -345,7 +349,7 @@ Sets other properties of the UDPSocket connection. This API uses an asynchronous **Example** -```js +``` let udp = socket.constructUDPSocketInstance(); udp.bind({address:'192.168.xx.xxx', port:xxxx, family:1}, err=> { if (err) { @@ -397,7 +401,7 @@ Sets other properties of the UDPSocket connection. This API uses a promise to re **Example** -```js +``` let udp = socket.constructUDPSocketInstance(); let promise = udp.bind({address:'192.168.xx.xxx', port:xxxx, family:1}); promise.then(() => { @@ -437,7 +441,7 @@ Enables listening for message receiving events of the UDPSocket connection. This **Example** -```js +``` let udp = socket.constructUDPSocketInstance(); udp.on('message', value => { console.log("on message, message:" + value.message + ", remoteInfo:" + value.remoteInfo); @@ -465,7 +469,7 @@ Disables listening for message receiving events of the UDPSocket connection. Thi **Example** -```js +``` let udp = socket.constructUDPSocketInstance(); let callback = value =>{ console.log("on message, message:" + value.message + ", remoteInfo:" + value.remoteInfo); @@ -494,7 +498,7 @@ Enables listening for data packet message events or close events of the UDPSocke **Example** -```js +``` let udp = socket.constructUDPSocketInstance(); udp.on('listening', () => { console.log("on listening success"); @@ -525,7 +529,7 @@ Disables listening for data packet message events or close events of the UDPSock **Example** -```js +``` let udp = socket.constructUDPSocketInstance(); let callback1 = () =>{ console.log("on listening, success"); @@ -562,7 +566,7 @@ Enables listening for error events of the UDPSocket connection. This API uses an **Example** -```js +``` let udp = socket.constructUDPSocketInstance(); udp.on('error', err => { console.log("on error, err:" + JSON.stringify(err)) @@ -590,7 +594,7 @@ Disables listening for error events of the UDPSocket connection. This API uses a **Example** -```js +``` let udp = socket.constructUDPSocketInstance(); let callback = err =>{ console.log("on error, err:" + JSON.stringify(err)); @@ -622,7 +626,7 @@ Defines the parameters for sending data over the UDPSocket connection. | Name | Type | Mandatory| Description | | ------- | ---------------------------------- | ---- | -------------- | -| data | string \| ArrayBuffer8+ | Yes | Data to send. | +| data | string | Yes | Data to send. | | address | [NetAddress](#netaddress) | Yes | Destination address.| ## UDPExtraOptions @@ -680,7 +684,7 @@ Creates a **TCPSocket** object. **Example** -```js +``` let tcp = socket.constructTCPSocketInstance(); ``` @@ -709,7 +713,7 @@ Binds the IP address and port number. The port number can be specified or random **Example** -```js +``` let tcp = socket.constructTCPSocketInstance(); tcp.bind({address: '192.168.xx.xxx', port: xxxx, family: 1}, err => { if (err) { @@ -745,7 +749,7 @@ Binds the IP address and port number. The port number can be specified or random **Example** -```js +``` let tcp = socket.constructTCPSocketInstance(); let promise = tcp.bind({address: '192.168.xx.xxx', port: xxxx, family: 1}); promise.then(() => { @@ -775,7 +779,7 @@ Sets up a connection to the specified IP address and port number. This API uses **Example** -```js +``` let tcp = socket.constructTCPSocketInstance(); tcp.connect({ address: {address: '192.168.xx.xxx', port: xxxx, family: 1} , timeout: 6000}, err => { if (err) { @@ -811,7 +815,7 @@ Sets up a connection to the specified IP address and port number. This API uses **Example** -```js +``` let tcp = socket.constructTCPSocketInstance(); let promise = tcp.connect({ address: {address: '192.168.xx.xxx', port: xxxx, family: 1} , timeout: 6000}); promise.then(() => { @@ -844,7 +848,7 @@ Sends data over a TCPSocket connection. This API uses an asynchronous callback t **Example** -```js +``` let tcp = socket.constructTCPSocketInstance(); let promise = tcp.connect({ address: {address: '192.168.xx.xxx', port: xxxx, family: 1} , timeout: 6000}); promise.then(() => { @@ -891,7 +895,7 @@ Sends data over a TCPSocket connection. This API uses a promise to return the re **Example** -```js +``` let tcp = socket.constructTCPSocketInstance(); let promise1 = tcp.connect({ address: {address: '192.168.xx.xxx', port: xxxx, family: 1} , timeout: 6000}); promise1.then(() => { @@ -929,7 +933,7 @@ Closes a TCPSocket connection. This API uses an asynchronous callback to return **Example** -```js +``` let tcp = socket.constructTCPSocketInstance(); tcp.close(err => { if (err) { @@ -959,7 +963,7 @@ Closes a TCPSocket connection. This API uses a promise to return the result. **Example** -```js +``` let tcp = socket.constructTCPSocketInstance(); let promise = tcp.close(); promise.then(() => { @@ -991,7 +995,7 @@ Obtains the remote address of a TCPSocket connection. This API uses an asynchron **Example** -```js +``` let tcp = socket.constructTCPSocketInstance(); let promise = tcp.connect({ address: {address: '192.168.xx.xxx', port: xxxx, family: 1} , timeout: 6000}); promise.then(() => { @@ -1030,7 +1034,7 @@ Obtains the remote address of a TCPSocket connection. This API uses a promise to **Example** -```js +``` let tcp = socket.constructTCPSocketInstance(); let promise1 = tcp.connect({ address: {address: '192.168.xx.xxx', port: xxxx, family: 1} , timeout: 6000}); promise1.then(() => { @@ -1069,7 +1073,7 @@ Obtains the status of the TCPSocket connection. This API uses an asynchronous ca **Example** -```js +``` let tcp = socket.constructTCPSocketInstance(); let promise = tcp.connect({ address: {address: '192.168.xx.xxx', port: xxxx, family: 1} , timeout: 6000}); promise.then(() => { @@ -1109,7 +1113,7 @@ Obtains the status of the TCPSocket connection. This API uses a promise to retur **Example** -```js +``` let tcp = socket.constructTCPSocketInstance(); let promise = tcp.connect({ address: {address: '192.168.xx.xxx', port: xxxx, family: 1} , timeout: 6000}); promise.then(() => { @@ -1148,7 +1152,7 @@ Sets other properties of the TCPSocket connection. This API uses an asynchronous **Example** -```js +``` let tcp = socket.constructTCPSocketInstance(); let promise = tcp.connect({ address: {address: '192.168.xx.xxx', port: xxxx, family: 1} , timeout: 6000}); promise.then(() => { @@ -1203,7 +1207,7 @@ Sets other properties of the TCPSocket connection. This API uses a promise to re **Example** -```js +``` let tcp = socket.constructTCPSocketInstance(); let promise = tcp.connect({ address: {address: '192.168.xx.xxx', port: xxxx, family: 1} , timeout: 6000}); promise.then(() => { @@ -1246,7 +1250,7 @@ Enables listening for message receiving events of the TCPSocket connection. This **Example** -```js +``` let tcp = socket.constructTCPSocketInstance(); tcp.on('message', value => { console.log("on message, message:" + value.message + ", remoteInfo:" + value.remoteInfo) @@ -1274,7 +1278,7 @@ Disables listening for message receiving events of the TCPSocket connection. Thi **Example** -```js +``` let tcp = socket.constructTCPSocketInstance(); let callback = value =>{ console.log("on message, message:" + value.message + ", remoteInfo:" + value.remoteInfo); @@ -1304,7 +1308,7 @@ Enables listening for connection or close events of the TCPSocket connection. Th **Example** -```js +``` let tcp = socket.constructTCPSocketInstance(); tcp.on('connect', () => { console.log("on connect success") @@ -1335,7 +1339,7 @@ Disables listening for connection or close events of the TCPSocket connection. T **Example** -```js +``` let tcp = socket.constructTCPSocketInstance(); let callback1 = () =>{ console.log("on connect success"); @@ -1371,7 +1375,7 @@ Enables listening for error events of the TCPSocket connection. This API uses an **Example** -```js +``` let tcp = socket.constructTCPSocketInstance(); tcp.on('error', err => { console.log("on error, err:" + JSON.stringify(err)) @@ -1399,7 +1403,7 @@ Disables listening for error events of the TCPSocket connection. This API uses a **Example** -```js +``` let tcp = socket.constructTCPSocketInstance(); let callback = err =>{ console.log("on error, err:" + JSON.stringify(err)); @@ -1430,7 +1434,7 @@ Defines the parameters for sending data over the TCPSocket connection. | Name | Type | Mandatory| Description | | -------- | ------ | ---- | ------------------------------------------------------------ | -| data | string\| ArrayBuffer8+ | Yes | Data to send. | +| data | string | Yes | Data to send. | | encoding | string | No | Character encoding format. The options are as follows: **UTF-8**, **UTF-16BE**, **UTF-16LE**, **UTF-16**, **US-AECII**, and **ISO-8859-1**. The default value is **UTF-8**.| ## TCPExtraOptions -- GitLab