From bad3a374e38541160a925a62e321f82621b607e0 Mon Sep 17 00:00:00 2001 From: "haisong.li" Date: Fri, 21 Apr 2023 19:39:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9js=E6=A0=BC=E5=BC=8F=E3=80=81?= =?UTF-8?q?=E5=AF=B9=E8=B1=A1=E7=9A=84=E6=89=93=E5=8D=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../connectivity/net-sharing.md | 6 +- .../connectivity/socket-connection.md | 151 +++++++++--------- .../reference/apis/js-apis-net-ethernet.md | 2 +- .../reference/apis/js-apis-net-policy.md | 74 ++++----- .../reference/apis/js-apis-socket.md | 25 ++- 5 files changed, 121 insertions(+), 137 deletions(-) diff --git a/zh-cn/application-dev/connectivity/net-sharing.md b/zh-cn/application-dev/connectivity/net-sharing.md index c4e0186d8da..2face74e3b8 100644 --- a/zh-cn/application-dev/connectivity/net-sharing.md +++ b/zh-cn/application-dev/connectivity/net-sharing.md @@ -60,7 +60,7 @@ 4. 接收到共享状态开启的回调,开启共享成功。 ```js - // 从@ohos.net.sharing中导入sharing命名空间 +// 从@ohos.net.sharing中导入sharing命名空间 import sharing from '@ohos.net.sharing' // 注册监听共享状态的改变 @@ -85,7 +85,7 @@ sharing.startSharing(sharing.SharingIfaceType.SHARING_WIFI, (error) => { 4. 接收到共享状态关闭的回调,停止共享成功。 ```js - // 从@ohos.net.sharing中导入sharing命名空间 +// 从@ohos.net.sharing中导入sharing命名空间 import sharing from '@ohos.net.sharing' // 注册监听共享状态的改变 @@ -110,7 +110,7 @@ sharing.stopSharing(sharing.SharingIfaceType.SHARING_WIFI, (error) => { 4. 调用stopSharing方法,来停止指定类型共享,共享网络数据量清零。 ```js - // 从@ohos.net.sharing中导入sharing命名空间 +// 从@ohos.net.sharing中导入sharing命名空间 import sharing from '@ohos.net.sharing' // 调用startSharing方法,来开启指定类型共享 diff --git a/zh-cn/application-dev/connectivity/socket-connection.md b/zh-cn/application-dev/connectivity/socket-connection.md index 63b86a61270..99958e526c7 100644 --- a/zh-cn/application-dev/connectivity/socket-connection.md +++ b/zh-cn/application-dev/connectivity/socket-connection.md @@ -86,82 +86,79 @@ UDP与TCP流程大体类似,下面以TCP为例: 6. 发送数据。 7. Socket连接使用完毕后,主动关闭。 + +```js +import socket from '@ohos.net.socket' + +// 创建一个TCPSocket连接,返回一个TCPSocket对象。 +let tcp = socket.constructTCPSocketInstance(); + +// 订阅TCPSocket相关的订阅事件 +tcp.on('message', value => { + console.log("on message") + let buffer = value.message + let dataView = new DataView(buffer) + let str = "" + for (let i = 0; i < dataView.byteLength; ++i) { + str += String.fromCharCode(dataView.getUint8(i)) + } + console.log("on connect received:" + str) +}); +tcp.on('connect', () => { + console.log("on connect") +}); +tcp.on('close', () => { + console.log("on close") +}); - ```js - import socket from '@ohos.net.socket' - - // 创建一个TCPSocket连接,返回一个TCPSocket对象。 - let tcp = socket.constructTCPSocketInstance(); - - // 订阅TCPSocket相关的订阅事件 - tcp.on('message', value => { - console.log("on message") - let buffer = value.message - let dataView = new DataView(buffer) - let str = "" - for (let i = 0;i < dataView.byteLength; ++i) { - str += String.fromCharCode(dataView.getUint8(i)) - } - console.log("on connect received:" + str) - }); - tcp.on('connect', () => { - console.log("on connect") - }); - tcp.on('close', () => { - console.log("on close") - }); - - // 绑定本地IP地址和端口。 - let bindAddress = { - address: '192.168.xx.xx', - port: 1234, // 绑定端口,如1234 - family: 1 - }; - tcp.bind(bindAddress, err => { - if (err) { - console.log('bind fail'); - return; - } - console.log('bind success'); - - // 连接到指定的IP地址和端口。 - let connectAddress = { - address: '192.168.xx.xx', - port: 5678, // 连接端口,如5678 - family: 1 - }; - tcp.connect({ - address: connectAddress, timeout: 6000 - }, err => { - if (err) { - console.log('connect fail'); - return; - } - console.log('connect success'); - - // 发送数据 - tcp.send({ - data: 'Hello, server!' - }, err => { - if (err) { - console.log('send fail'); - return; - } - console.log('send success'); - }) - }); - }); - - // 连接使用完毕后,主动关闭。取消相关事件的订阅。 - setTimeout(() => { - tcp.close((err) => { - console.log('close socket.') - }); - tcp.off('message'); - tcp.off('connect'); - tcp.off('close'); - }, 30 * 1000); - ``` +// 绑定IP地址和端口。 +let bindAddress = { + address: '192.168.xx.xx', + port: 1234, // 绑定端口,如1234 + family: 1 +}; +tcp.bind(bindAddress, err => { + if (err) { + console.log('bind fail'); + return; + } + console.log('bind success'); + // 连接到指定的IP地址和端口。 + let connectAddress = { + address: '192.168.xx.xx', + port: 5678, // 连接端口,如5678 + family: 1 + }; + tcp.connect({ + address: connectAddress, timeout: 6000 + }, err => { + if (err) { + console.log('connect fail'); + return; + } + console.log('connect success'); + // 发送数据 + tcp.send({ + data: 'Hello, server!' + }, err => { + if (err) { + console.log('send fail'); + return; + } + console.log('send success'); + }) + }); +}); +// 连接使用完毕后,主动关闭。取消相关事件的订阅。 +setTimeout(() => { + tcp.close((err) => { + console.log('close socket.') + }); + tcp.off('message'); + tcp.off('connect'); + tcp.off('close'); +}, 30 * 1000); +``` ## 应用通过TLS Socket进行加密数据传输 @@ -206,7 +203,7 @@ tlsTwoWay.on('close', () => { }); // 绑定本地IP地址和端口。 -tlsTwoWay.bind({address: '192.168.xxx.xxx', port: xxxx, family: 1}, err => { +tlsTwoWay.bind({ address: '192.168.xxx.xxx', port: xxxx, family: 1 }, err => { if (err) { console.log('bind fail'); return; @@ -278,7 +275,7 @@ tlsTwoWay.on('close', () => { }); // 绑定本地IP地址和端口。 -tlsOneWay.bind({address: '192.168.xxx.xxx', port: xxxx, family: 1}, err => { +tlsOneWay.bind({ address: '192.168.xxx.xxx', port: xxxx, family: 1 }, err => { if (err) { console.log('bind fail'); return; diff --git a/zh-cn/application-dev/reference/apis/js-apis-net-ethernet.md b/zh-cn/application-dev/reference/apis/js-apis-net-ethernet.md index f7ca1d622ef..2aa48bc6e62 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-net-ethernet.md +++ b/zh-cn/application-dev/reference/apis/js-apis-net-ethernet.md @@ -416,7 +416,7 @@ on(type: 'interfaceStateChange', callback: Callback\<{ iface: string, active: bo **示例:** ```js - ethernet.on('interfaceStateChange', (data) => { +ethernet.on('interfaceStateChange', (data) => { console.log('on interfaceSharingStateChange:' + JSON.stringify(data)); }); ``` diff --git a/zh-cn/application-dev/reference/apis/js-apis-net-policy.md b/zh-cn/application-dev/reference/apis/js-apis-net-policy.md index b62e5209b68..4a5565d1924 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-net-policy.md +++ b/zh-cn/application-dev/reference/apis/js-apis-net-policy.md @@ -12,9 +12,9 @@ import policy from '@ohos.net.policy' ``` -## policy.setBackgroundPolicy +## policy.setBackgroundAllowed -setBackgroundPolicy(isAllowed: boolean, callback: AsyncCallback\): void +setBackgroundAllowed(isAllowed: boolean, callback: AsyncCallback\): void 设置后台网络策略,使用callback方式作为异步方法。 @@ -42,18 +42,16 @@ setBackgroundPolicy(isAllowed: boolean, callback: AsyncCallback\): void **示例:** ```js -policy.setBackgroundPolicy(Boolean(Number.parseInt(this.isBoolean))), (error, data) => { - this.callBack(error, data); +policy.setBackgroundAllowed(Boolean(Number.parseInt(this.isBoolean)), (error) => { + this.callBack(error); console.log(JSON.stringify(error)) - console.log(JSON.stringify(data)) -} -) +}) ; ``` -## policy.setBackgroundPolicy +## policy.setBackgroundAllowed -setBackgroundPolicy(isAllowed: boolean): Promise\ +setBackgroundAllowed(isAllowed: boolean): Promise\ 设置后台网络策略,使用Promise方式作为异步方法。 @@ -86,9 +84,8 @@ setBackgroundPolicy(isAllowed: boolean): Promise\ **示例:** ```js -policy.setBackgroundPolicy(Boolean(Number.parseInt(this.isBoolean))).then(function (error, data) { +policy.setBackgroundAllowed(Boolean(Number.parseInt(this.isBoolean))).then(function (error) { console.log(JSON.stringify(error)) - console.log(JSON.stringify(data)) }) ``` @@ -194,8 +191,8 @@ setPolicyByUid(uid: number, policy: NetUidPolicy, callback: AsyncCallback\ let param = { uid: Number.parseInt(this.firstParam), policy: Number.parseInt(this.currentNetUidPolicy) } -policy.setPolicyByUid(Number.parseInt(this.firstParam), Number.parseInt(this.currentNetUidPolicy), (error, data) => { - this.callBack(error, data); +policy.setPolicyByUid(Number.parseInt(this.firstParam), Number.parseInt(this.currentNetUidPolicy), (error) => { + console.log(JSON.stringify(error)) }); ``` @@ -238,9 +235,8 @@ setPolicyByUid(uid: number, policy: NetUidPolicy): Promise\; let param = { uid: Number.parseInt(this.firstParam), policy: Number.parseInt(this.currentNetUidPolicy) } -policy.setPolicyByUid(Number.parseInt(this.firstParam), Number.parseInt(this.currentNetUidPolicy)).then(function (error, data) { +policy.setPolicyByUid(Number.parseInt(this.firstParam), Number.parseInt(this.currentNetUidPolicy)).then(function (error) { console.log(JSON.stringify(error)) - console.log(JSON.stringify(data)) }) ``` @@ -509,8 +505,8 @@ let param = { }; this.netQuotaPolicyList.push(param); -policy.setNetQuotaPolicies(this.netQuotaPolicyList, (error, data) => { - this.callBack(error, data); +policy.setNetQuotaPolicies(this.netQuotaPolicyList, (error) => { + console.log(JSON.stringify(error)) }); ``` @@ -563,9 +559,8 @@ let param = { }; this.netQuotaPolicyList.push(param); -policy.setNetQuotaPolicies(this.netQuotaPolicyList).then(function (error, data) { +policy.setNetQuotaPolicies(this.netQuotaPolicyList).then(function (error) { console.log(JSON.stringify(error)) - console.log(JSON.stringify(data)) }) ``` @@ -600,8 +595,8 @@ restoreAllPolicies(iccid: string, callback: AsyncCallback\): void ```js this.firstParam = iccid; -policy.restoreAllPolicies(this.firstParam, (error, data) => { - this.callBack(error, data); +policy.restoreAllPolicies(this.firstParam, (error) => { + console.log(JSON.stringify(error)) }); ``` @@ -641,9 +636,8 @@ restoreAllPolicies(iccid: string): Promise\; ```js this.firstParam = iccid; -policy.restoreAllPolicies(this.firstParam).then(function (error, data) { +policy.restoreAllPolicies(this.firstParam).then(function (error) { console.log(JSON.stringify(error)) - console.log(JSON.stringify(data)) }) ``` @@ -724,7 +718,6 @@ isUidNetAllowed(uid: number, isMetered: boolean): Promise\; **示例:** ```js - let param = { uid: Number.parseInt(this.firstParam), isMetered: Boolean(Number.parseInt(this.isBoolean)) } @@ -766,7 +759,6 @@ isUidNetAllowed(uid: number, iface: string, callback: AsyncCallback\): **示例:** ```js - let param = { uid: Number.parseInt(this.firstParam), iface: this.secondParam } @@ -818,7 +810,6 @@ policy.isUidNetAllowed(Number.parseInt(this.firstParam), this.secondParam).then( console.log(JSON.stringify(error)) console.log(JSON.stringify(data)) }) - ``` ## policy.setDeviceIdleAllowList @@ -855,8 +846,8 @@ setDeviceIdleAllowList(uid: number, isAllowed: boolean, callback: AsyncCallback\ let param = { uid: Number.parseInt(this.firstParam), isAllowed: Boolean(Number.parseInt(this.isBoolean)) } -policy.setDeviceIdleAllowList(Number.parseInt(this.firstParam), Boolean(Number.parseInt(this.isBoolean)), (error, data) => { - this.callBack(error, data); +policy.setDeviceIdleAllowList(Number.parseInt(this.firstParam), Boolean(Number.parseInt(this.isBoolean)), (error) => { + console.log(JSON.stringify(error)) }); ``` @@ -899,9 +890,8 @@ setDeviceIdleAllowList(uid: number, isAllowed: boolean): Promise\; let param = { uid: Number.parseInt(this.firstParam), isAllowed: Boolean(Number.parseInt(this.isBoolean)) } -policy.setDeviceIdleAllowList(Number.parseInt(this.firstParam), Boolean(Number.parseInt(this.isBoolean))).then(function (error, data) { +policy.setDeviceIdleAllowList(Number.parseInt(this.firstParam), Boolean(Number.parseInt(this.isBoolean))).then(function (error) { console.log(JSON.stringify(error)) - console.log(JSON.stringify(data)) }) ``` @@ -1080,8 +1070,8 @@ resetPolicies(iccid: string, callback: AsyncCallback\): void ```js this.firstParam = iccid -policy.resetPolicies(this.firstParam, (error, data) => { - this.callBack(error, data); +policy.resetPolicies(this.firstParam, (error) => { + console.log(JSON.stringify(error)) }); ``` @@ -1124,9 +1114,8 @@ policy.getUidsByPolicy(Number.parseInt(this.firstParam)).then(function (error, d }) this.firstParam = iccid -policy.resetPolicies(this.firstParam).then(function (error, data) { +policy.resetPolicies(this.firstParam).then(function (error) { console.log(JSON.stringify(error)) - console.log(JSON.stringify(data)) }) ``` @@ -1252,8 +1241,8 @@ setPowerSaveAllowList(uid: number, isAllowed: boolean, callback: AsyncCallback\< let param = { uid: Number.parseInt(this.firstParam), isAllowed: Boolean(Number.parseInt(this.isBoolean)) } -policy.setPowerSaveAllowList(Number.parseInt(this.firstParam), Boolean(Number.parseInt(this.isBoolean)), (error, data) => { - this.callBack(error, data); +policy.setPowerSaveAllowList(Number.parseInt(this.firstParam), Boolean(Number.parseInt(this.isBoolean)), (error) => { + console.log(JSON.stringify(error)) }); ``` @@ -1296,9 +1285,8 @@ setPowerSaveAllowList(uid: number, isAllowed: boolean): Promise\; let param = { uid: Number.parseInt(this.firstParam), isAllowed: Boolean(Number.parseInt(this.isBoolean)) } -policy.setPowerSaveAllowList(Number.parseInt(this.firstParam), Boolean(Number.parseInt(this.isBoolean))).then(function (error, data) { +policy.setPowerSaveAllowList(Number.parseInt(this.firstParam), Boolean(Number.parseInt(this.isBoolean))).then(function (error) { console.log(JSON.stringify(error)) - console.log(JSON.stringify(data)) }) ``` @@ -1393,7 +1381,7 @@ on(type: "netUidPolicyChange", callback: Callback\<{ uid: number, policy: NetUid ```js policy.on('netUidPolicyChange', (data) => { - this.log('on netUidPolicyChange:' + JSON.stringify(data)); + this.log('on netUidPolicyChange: ' + JSON.stringify(data)); }) ``` @@ -1418,7 +1406,7 @@ on(type: "netUidRuleChange", callback: Callback\<{ uid: number, rule: NetUidRule ```js policy.on('netUidRuleChange', (data) => { - this.log('on netUidRuleChange:' + JSON.stringify(data)); + this.log('on netUidRuleChange: ' + JSON.stringify(data)); }) ``` @@ -1443,7 +1431,7 @@ on(type: "netMeteredIfacesChange", callback: Callback\>): void ```js policy.on('netMeteredIfacesChange', (data) => { - this.log('on netMeteredIfacesChange:' + JSON.stringify(data)); + this.log('on netMeteredIfacesChange: ' + JSON.stringify(data)); }) ``` @@ -1468,7 +1456,7 @@ on(type: "netQuotaPolicyChange", callback: Callback\>): v ```js policy.on('netQuotaPolicyChange', (data) => { - this.log('on netQuotaPolicyChange:' + JSON.stringify(data)); + this.log('on netQuotaPolicyChange: ' + JSON.stringify(data)); }) ``` @@ -1493,7 +1481,7 @@ on(type: "netBackgroundPolicyChange", callback: Callback\): void ```js policy.on('netBackgroundPolicyChange', (data) => { - this.log('on netBackgroundPolicyChange:' + JSON.stringify(data)); + this.log('on netBackgroundPolicyChange: ' + JSON.stringify(data)); }) ``` diff --git a/zh-cn/application-dev/reference/apis/js-apis-socket.md b/zh-cn/application-dev/reference/apis/js-apis-socket.md index 15da749a5bd..1e69b3dc0dc 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-socket.md +++ b/zh-cn/application-dev/reference/apis/js-apis-socket.md @@ -1605,7 +1605,7 @@ bind(address: NetAddress, callback: AsyncCallback\): void **示例:** ```js -tls.bind({address: '192.168.xx.xxx', port: xxxx, family: 1}, err => { +tls.bind({ address: '192.168.xx.xxx', port: xxxx, family: 1 }, err => { if (err) { console.log('bind fail'); return; @@ -1648,7 +1648,7 @@ bind(address: NetAddress): Promise\ **示例:** ```js -let promise = tls.bind({address: '192.168.xx.xxx', port: xxxx, family: 1}); +let promise = tls.bind({ address: '192.168.xx.xxx', port: xxxx, family: 1 }); promise.then(() => { console.log('bind success'); }).catch(err => { @@ -1680,7 +1680,7 @@ getState(callback: AsyncCallback\): void **示例:** ```js -let promise = tls.bind({address: '192.168.xx.xxx', port: xxxx, family: 1}, err => { +let promise = tls.bind({ address: '192.168.xx.xxx', port: xxxx, family: 1 }, err => { if (err) { console.log('bind fail'); return; @@ -1720,7 +1720,7 @@ getState(): Promise\ **示例:** ```js -let promiseBind = tls.bind({address: '192.168.xx.xxx', port: xxxx, family: 1}); +let promiseBind = tls.bind({ address: '192.168.xx.xxx', port: xxxx, family: 1 }); promiseBind.then(() => { console.log('bind success'); }).catch((err) => { @@ -1760,7 +1760,7 @@ setExtraOptions(options: TCPExtraOptions, callback: AsyncCallback\): void **示例:** ```js -tls.bind({address: '192.168.xx.xxx', port: xxxx, family: 1}, err => { +tls.bind({ address: '192.168.xx.xxx', port: xxxx, family: 1 }, err => { if (err) { console.log('bind fail'); return; @@ -1772,7 +1772,7 @@ tls.setExtraOptions({ keepAlive: true, OOBInline: true, TCPNoDelay: true, - socketLinger: {on: true, linger: 10}, + socketLinger: { on: true, linger: 10 }, receiveBufferSize: 1000, sendBufferSize: 1000, reuseAddress: true, @@ -1784,7 +1784,6 @@ tls.setExtraOptions({ } console.log('setExtraOptions success'); }); - ``` ### setExtraOptions9+ @@ -1818,7 +1817,7 @@ setExtraOptions(options: TCPExtraOptions): Promise\ **示例:** ```js -tls.bind({address: '192.168.xx.xxx', port: xxxx, family: 1}, err => { +tls.bind({ address: '192.168.xx.xxx', port: xxxx, family: 1 }, err => { if (err) { console.log('bind fail'); return; @@ -1829,7 +1828,7 @@ let promise = tls.setExtraOptions({ keepAlive: true, OOBInline: true, TCPNoDelay: true, - socketLinger: {on: true, linger: 10}, + socketLinger: { on: true, linger: 10 }, receiveBufferSize: 1000, sendBufferSize: 1000, reuseAddress: true, @@ -1881,7 +1880,7 @@ connect(options: TLSConnectOptions, callback: AsyncCallback\): void ```js let tlsTwoWay = socket.constructTLSSocketInstance(); // Two way authentication -tlsTwoWay.bind({address: '192.168.xxx.xxx', port: 8080, family: 1}, err => { +tlsTwoWay.bind({ address: '192.168.xxx.xxx', port: 8080, family: 1 }, err => { if (err) { console.log('bind fail'); return; @@ -1912,7 +1911,7 @@ tlsTwoWay.connect(options, (err, data) => { }); let tlsOneWay = socket.constructTLSSocketInstance(); // One way authentication -tlsOneWay.bind({address: '192.168.xxx.xxx', port: 8080, family: 1}, err => { +tlsOneWay.bind({ address: '192.168.xxx.xxx', port: 8080, family: 1 }, err => { if (err) { console.log('bind fail'); return; @@ -1980,7 +1979,7 @@ connect(options: TLSConnectOptions): Promise\ ```js let tlsTwoWay = socket.constructTLSSocketInstance(); // Two way authentication -tlsTwoWay.bind({address: '192.168.xxx.xxx', port: 8080, family: 1}, err => { +tlsTwoWay.bind({ address: '192.168.xxx.xxx', port: 8080, family: 1 }, err => { if (err) { console.log('bind fail'); return; @@ -2012,7 +2011,7 @@ tlsTwoWay.connect(options).then(data => { }); let tlsOneWay = socket.constructTLSSocketInstance(); // One way authentication -tlsOneWay.bind({address: '192.168.xxx.xxx', port: 8080, family: 1}, err => { +tlsOneWay.bind({ address: '192.168.xxx.xxx', port: 8080, family: 1 }, err => { if (err) { console.log('bind fail'); return; -- Gitee