未验证 提交 db13b28b 编写于 作者: O openharmony_ci 提交者: Gitee

!12293 修复网络管理js示例代码

Merge pull request !12293 from 刘建东/master
......@@ -490,8 +490,6 @@ flush(callback: AsyncCallback\<void>): void
**示例:**
```js
import http from '@ohos.net.http';
let httpResponseCache = http.createHttpResponseCache();
httpResponseCache.flush(err => {
if (err) {
console.log('flush fail');
......@@ -518,9 +516,7 @@ flush(): Promise\<void>
**示例:**
```js
import http from '@ohos.net.http';
let httpResponseCache = http.createHttpResponseCache();
http.flush().then(() => {
httpResponseCache.flush().then(() => {
console.log('flush success');
}).catch(err => {
console.log('flush fail');
......@@ -544,8 +540,6 @@ delete(callback: AsyncCallback\<void>): void
**示例:**
```js
import http from '@ohos.net.http';
let httpResponseCache = http.createHttpResponseCache();
httpResponseCache.delete(err => {
if (err) {
console.log('delete fail');
......@@ -571,9 +565,7 @@ delete(): Promise\<void>
**示例:**
```js
import http from '@ohos.net.http';
let httpResponseCache = http.createHttpResponseCache();
httpResponseCache.delete().then() => {
httpResponseCache.delete().then(() => {
console.log('delete success');
}).catch(err => {
console.log('delete fail');
......
......@@ -157,7 +157,6 @@ connection.getAllNets(function (error, nets) {
});
```
## connection.getAllNets
getAllNets(): Promise&lt;Array&lt;NetHandle&gt;&gt;
......@@ -379,7 +378,6 @@ connection.getDefaultNet().then(function (netHandle) {
});
```
## connection.reportNetConnected
reportNetConnected(netHandle: NetHandle): Promise&lt;void&gt;
......@@ -412,7 +410,6 @@ connection.getDefaultNet().then(function (netHandle) {
});
```
## connection.reportNetDisconnected
reportNetDisconnected(netHandle: NetHandle, callback: AsyncCallback&lt;void&gt;): void
......@@ -441,7 +438,6 @@ connection.getDefaultNet().then(function (netHandle) {
});
```
## connection.reportNetDisconnected
reportNetDisconnected(netHandle: NetHandle): Promise&lt;void&gt;
......@@ -532,7 +528,6 @@ connection.getAddressesByName(host).then(function (addresses) {
})
```
## connection.enableAirplaneMode
enableAirplaneMode(callback: AsyncCallback\<void>): void
......@@ -581,7 +576,6 @@ connection.enableAirplaneMode().then(function (error) {
})
```
## connection.disableAirplaneMode
disableAirplaneMode(callback: AsyncCallback\<void>): void
......@@ -630,7 +624,6 @@ connection.disableAirplaneMode().then(function (error) {
})
```
## connection.createNetConnection
createNetConnection(netSpecifier?: NetSpecifier, timeout?: number): NetConnection
......@@ -887,33 +880,50 @@ bindSocket(socketParam: TCPSocket \| UDPSocket, callback: AsyncCallback\<void>):
**示例:**
```js
connection.getDefaultNet().then(function (netHandle) {
import socket from "@ohos.net.socket";
connection.getDefaultNet().then((netHandle)=>{
var tcp = socket.constructTCPSocketInstance();
var udp = socket.constructUDPSocketInstance();
let socketType = "xxxx";
let socketType = "TCPSocket";
if (socketType == "TCPSocket") {
tcp.bind({
address: "xxxx", port: xxxx, family: xxxx
address: '192.168.xx.xxx', port: xxxx, family: 1
}, err => {
netHandle.bindSocket(tcp, function (error, data) {
console.log(JSON.stringify(error))
console.log(JSON.stringify(data))
if (err) {
console.log('bind fail');
}
netHandle.bindSocket(tcp, (error, data)=>{
if (error) {
console.log(JSON.stringify(error));
} else {
console.log(JSON.stringify(data));
}
})
})
} else {
let callback = value => {
console.log(TAG + "on message, message:" + value.message + ", remoteInfo:" + value.remoteInfo);
}
udp.on('message', callback);
udp.bind({
address: "xxxx", port: xxxx, family: xxxx
address: '192.168.xx.xxx', port: xxxx, family: 1
}, err => {
if (err) {
console.log('bind fail');
}
udp.on('message', (data) => {
console.log(JSON.stringify(data))
});
netHandle.bindSocket(udp, function (error, data) {
console.log(JSON.stringify(error))
console.log(JSON.stringify(data))
console.log(JSON.stringify(data))
});
netHandle.bindSocket(udp,(error, data)=>{
if (error) {
console.log(JSON.stringify(error));
} else {
console.log(JSON.stringify(data));
}
})
})
}
}
}
})
```
### bindSocket<sup>9+</sup>
......@@ -939,31 +949,50 @@ bindSocket(socketParam: TCPSocket \| UDPSocket): Promise\<void>;
**示例:**
```js
connection.getDefaultNet().then(function (netHandle) {
import socket from "@ohos.net.socket";
connection.getDefaultNet().then((netHandle)=>{
var tcp = socket.constructTCPSocketInstance();
var udp = socket.constructUDPSocketInstance();
let socketType = "xxxx";
if(socketType == "TCPSocket") {
let socketType = "TCPSocket";
if (socketType == "TCPSocket") {
tcp.bind({
address: "xxxx", port: xxxx, family: xxxx
address: '192.168.xx.xxx', port: xxxx, family: 1
}, err => {
netHandle.bindSocket(tcp).then(err, data) {
console.log(JSON.stringify(data))
if (err) {
console.log('bind fail');
}
netHandle.bindSocket(tcp).then((err, data) => {
if (err) {
console.log(JSON.stringify(err));
} else {
console.log(JSON.stringify(data));
}
})
})
} else {
let callback = value => {
console.log(TAG + "on message, message:" + value.message + ", remoteInfo:" + value.remoteInfo);
}
udp.on('message', callback);
udp.bind({
address: "xxxx", port: xxxx, family: xxxx
address: '192.168.xx.xxx', port: xxxx, family: 1
}, err => {
if (err) {
console.log('bind fail');
}
udp.on('message', (data) => {
console.log(JSON.stringify(data))
});
netHandle.bindSocket(tcp).then(err, data) {
console.log(JSON.stringify(data))
});
console.log(JSON.stringify(data));
})
netHandle.bindSocket(udp).then((err, data) => {
if (err) {
console.log(JSON.stringify(err));
} else {
console.log(JSON.stringify(data));
}
})
})
}
}
}
})
```
### getAddressesByName
......
......@@ -499,7 +499,7 @@ getSharingState(type: SharingIfaceType, callback: AsyncCallback\<SharingIfaceSta
**示例:**
```js
import SharingIfaceState from '@ohos.net.sharing'
import SharingIfaceType from '@ohos.net.sharing'
sharing.getSharingState(SharingIfaceType.SHARING_WIFI, (error, data) => {
console.log(JSON.stringify(error));
console.log(JSON.stringify(data));
......@@ -534,7 +534,7 @@ getSharingState(type: SharingIfaceType): Promise\<SharingIfaceState>
```js
import SharingIfaceType from '@ohos.net.sharing'
sharing.getSharingIfaces(SharingIfaceType.SHARING_WIFI).then(data => {
sharing.getSharingState(SharingIfaceType.SHARING_WIFI).then(data => {
console.log(JSON.stringify(data));
}).catch(error => {
console.log(JSON.stringify(error));
......@@ -563,8 +563,8 @@ getSharableRegexes(type: SharingIfaceType, callback: AsyncCallback\<Array\<strin
**示例:**
```js
import SharingIfaceState from '@ohos.net.sharing'
sharing.getSharingState(SharingIfaceType.SHARING_WIFI, (error, data) => {
import SharingIfaceType from '@ohos.net.sharing'
sharing.getSharableRegexes(SharingIfaceType.SHARING_WIFI, (error, data) => {
console.log(JSON.stringify(error));
console.log(JSON.stringify(data));
});
......@@ -605,7 +605,7 @@ sharing.getSharableRegexes(SharingIfaceType.SHARING_WIFI).then(data => {
});
```
## on('sharingStateChange')
## sharing.on('sharingStateChange')
on(type: 'sharingStateChange', callback: Callback\<boolean>): void
......@@ -633,7 +633,7 @@ sharing.on('sharingStateChange', (error, data) => {
});
```
## off('sharingStateChange')
## sharing.off('sharingStateChange')
off(type: 'sharingStateChange', callback?: Callback\<boolean>): void
......@@ -661,7 +661,7 @@ sharing.off('sharingStateChange', (error, data) => {
});
```
## on('interfaceSharingStateChange')
## sharing.on('interfaceSharingStateChange')
on(type: 'interfaceSharingStateChange', callback: Callback\<{ type: SharingIfaceType, iface: string, state: SharingIfaceState }>): void
......@@ -689,7 +689,7 @@ sharing.on('interfaceSharingStateChange', (error, data) => {
});
```
## off('interfaceSharingStateChange')
## sharing.off('interfaceSharingStateChange')
off(type: 'interfaceSharingStateChange', callback?: Callback\<{ type: SharingIfaceType, iface: string, state: SharingIfaceState }>): void
......@@ -717,7 +717,7 @@ sharing.off('interfaceSharingStateChange', (error, data) => {
});
```
## on('sharingUpstreamChange')
## sharing.on('sharingUpstreamChange')
on(type: 'sharingUpstreamChange', callback: Callback\<NetHandle>): void
......@@ -745,7 +745,7 @@ sharing.on('sharingUpstreamChange', (error, data) => {
});
```
## off('sharingUpstreamChange')
## sharing.off('sharingUpstreamChange')
off(type: 'sharingUpstreamChange', callback?: Callback\<NetHandle>): void
......
......@@ -1507,7 +1507,6 @@ bind\(address: NetAddress, callback: AsyncCallback<void\>\): void
**示例:**
```js
let tls = socket.constructTLSSocketInstance();
tls.bind({address: '192.168.xx.xxx', port: xxxx, family: 1}, err => {
if (err) {
console.log('bind fail');
......@@ -1551,7 +1550,6 @@ bind\(address: NetAddress\): Promise<void\>
**示例:**
```js
let tls = socket.constructTLSSocketInstance();
let promise = tls.bind({address: '192.168.xx.xxx', port: xxxx, family: 1});
promise.then(() => {
console.log('bind success');
......@@ -1584,7 +1582,6 @@ getState\(callback: AsyncCallback<SocketStateBase\>\): void
**示例:**
```js
let tls = socket.constructTLSSocketInstance();
let promise = tls.bind({address: '192.168.xx.xxx', port: xxxx, family: 1}, err => {
if (err) {
console.log('bind fail');
......@@ -1625,7 +1622,6 @@ getState\(\): Promise<SocketStateBase\>
**示例:**
```js
let tls = socket.constructTLSSocketInstance();
tls.bind({address: '192.168.xx.xxx', port: xxxx, family: 1}, err => {
if (err) {
console.log('bind fail');
......@@ -1667,7 +1663,6 @@ setExtraOptions\(options: TCPExtraOptions, callback: AsyncCallback<void\>\): voi
**示例:**
```js
let tls = socket.constructTLSSocketInstance();
tls.bind({address: '192.168.xx.xxx', port: xxxx, family: 1}, err => {
if (err) {
console.log('bind fail');
......@@ -1725,7 +1720,6 @@ setExtraOptions\(options: TCPExtraOptions\): Promise<void\>
**示例:**
```js
let tls = socket.constructTLSSocketInstance();
tls.bind({address: '192.168.xx.xxx', port: xxxx, family: 1}, err => {
if (err) {
console.log('bind fail');
......@@ -1789,15 +1783,14 @@ connect(options: TLSConnectOptions, callback: AsyncCallback\<void>): void
**示例:**
```js
let tls = socket.constructTLSSocketInstance(); // Two way authentication
tls.bind({address: '192.168.xxx.xxx', port: xxxx, family: 1}, err => {
let tlsTwoWay = socket.constructTLSSocketInstance(); // Two way authentication
tlsTwoWay.bind({address: '192.168.xxx.xxx', port: xxxx, family: 1}, err => {
if (err) {
console.log('bind fail');
return;
}
console.log('bind success');
});
let Protocol = socket.Protocol;
let options = {
ALPNProtocols: ["spdy/1", "http/1.1"],
address: {
......@@ -1810,15 +1803,15 @@ let options = {
cert: "xxxx",
ca: ["xxxx"],
passwd: "xxxx",
protocols: [Protocol.TLSv12],
protocols: [socket.Protocol.TLSv12],
useRemoteCipherPrefer: true,
signatureAlgorithms: rsa_pss_rsae_sha256:ECDSA+SHA25,
cipherSuite: AES256-SHA256,
signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256",
cipherSuite: "AES256-SHA256",
},
};
tls.connect(options, (err, data) => {
console.info(err);
console.info(data);
tlsTwoWay.connect(options, (err, data) => {
console.error(err);
console.log(data);
});
let tlsOneWay = socket.constructTLSSocketInstance(); // One way authentication
......@@ -1837,12 +1830,12 @@ let oneWayOptions = {
},
secureOptions: {
ca: ["xxxx","xxxx"],
cipherSuite: AES256-SHA256,
cipherSuite: "AES256-SHA256",
},
};
tlsOneWay.connect(options, (err, data) => {
console.info(err);
console.info(data);
tlsOneWay.connect(oneWayOptions, (err, data) => {
console.error(err);
console.log(data);
});
```
......@@ -1890,15 +1883,14 @@ connect(options: TLSConnectOptions): Promise\<void>
**示例:**
```js
let tls = socket.constructTLSSocketInstance(); // Two way authentication
tls.bind({address: '192.168.xxx.xxx', port: xxxx, family: 1}, err => {
let tlsTwoWay = socket.constructTLSSocketInstance(); // Two way authentication
tlsTwoWay.bind({address: '192.168.xxx.xxx', port: xxxx, family: 1}, err => {
if (err) {
console.log('bind fail');
return;
}
console.log('bind success');
});
let Protocol = socket.Protocol;
let options = {
ALPNProtocols: ["spdy/1", "http/1.1"],
address: {
......@@ -1911,14 +1903,14 @@ let options = {
cert: "xxxx",
ca: ["xxxx"],
passwd: "xxxx",
protocols: [Protocol.TLSv12],
protocols: [socket.Protocol.TLSv12],
useRemoteCipherPrefer: true,
signatureAlgorithms: rsa_pss_rsae_sha256:ECDSA+SHA25,
cipherSuite: AES256-SHA256,
signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256",
cipherSuite: "AES256-SHA256",
},
};
tls.connect(options).then(data => {
console.info(data);
tlsTwoWay.connect(options).then(data => {
console.log(data);
}).catch(err => {
console.error(err);
});
......@@ -1939,11 +1931,11 @@ let oneWayOptions = {
},
secureOptions: {
ca: ["xxxx","xxxx"],
cipherSuite: AES256-SHA256,
cipherSuite: "AES256-SHA256",
},
};
tlsOneWay.connect(oneWayOptions).then(data => {
console.info(data);
console.log(data);
}).catch(err => {
console.error(err);
});
......@@ -2074,7 +2066,7 @@ getCertificate():Promise\<[X509CertRawData](#x509certrawdata9)>
```js
tls.getCertificate().then(data => {
console.info(data);
console.log(data);
}).catch(err => {
console.error(err);
});
......@@ -2138,7 +2130,7 @@ getRemoteCertificate():Promise\<[X509CertRawData](#x509certrawdata9)>
```js
tls.getRemoteCertificate().then(data => {
console.info(data);
console.log(data);
}).catch(err => {
console.error(err);
});
......@@ -2204,7 +2196,7 @@ getProtocol():Promise\<string>
```js
tls.getProtocol().then(data => {
console.info(data);
console.log(data);
}).catch(err => {
console.error(err);
});
......@@ -2272,7 +2264,7 @@ getCipherSuite(): Promise\<Array\<string>>
```js
tls.getCipherSuite().then(data => {
console.info(data);
console.log(data);
}).catch(err => {
console.error(err);
});
......@@ -2336,7 +2328,7 @@ getSignatureAlgorithms(): Promise\<Array\<string>>
```js
tls.getSignatureAlgorithms().then(data => {
console.info(data);
console.log(data);
}).catch(err => {
console.error(err);
});
......@@ -2499,7 +2491,7 @@ TLS连接的操作。
| -------------- | ------------------------------------- | --- |-------------- |
| address | [NetAddress](#netaddress) | 是 | 网关地址。 |
| secureOptions | [TLSSecureOptions](#tlssecureoptions9) | 是 | TLS安全相关操作。|
| ALPNProtocols | Array\<string> | | ALPN协议。 |
| ALPNProtocols | Array\<string> | | ALPN协议。 |
## TLSSecureOptions<sup>9+</sup>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册