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

!14058 example-code

Merge pull request !14058 from 张海丰/example-code
...@@ -1498,18 +1498,18 @@ bindSocket(socketParam: TCPSocket \| UDPSocket, callback: AsyncCallback\<void>): ...@@ -1498,18 +1498,18 @@ bindSocket(socketParam: TCPSocket \| UDPSocket, callback: AsyncCallback\<void>):
```js ```js
import socket from "@ohos.net.socket"; import socket from "@ohos.net.socket";
connection.getDefaultNet().then((netHandle)=>{ connection.getDefaultNet().then((netHandle) => {
var tcp = socket.constructTCPSocketInstance(); var tcp = socket.constructTCPSocketInstance();
var udp = socket.constructUDPSocketInstance(); var udp = socket.constructUDPSocketInstance();
let socketType = "TCPSocket"; let socketType = "TCPSocket";
if (socketType == "TCPSocket") { if (socketType == "TCPSocket") {
tcp.bind({ tcp.bind({
address: '192.168.xx.xxx', port: xxxx, family: 1 address: '192.168.xx.xxx', port: 8080, family: 1
}, err => { }, error => {
if (err) { if (error) {
console.log('bind fail'); console.log('bind fail');
} }
netHandle.bindSocket(tcp, (error, data)=>{ netHandle.bindSocket(tcp, (error, data) => {
if (error) { if (error) {
console.log(JSON.stringify(error)); console.log(JSON.stringify(error));
} else { } else {
...@@ -1519,19 +1519,19 @@ connection.getDefaultNet().then((netHandle)=>{ ...@@ -1519,19 +1519,19 @@ connection.getDefaultNet().then((netHandle)=>{
}) })
} else { } else {
let callback = value => { let callback = value => {
console.log(TAG + "on message, message:" + value.message + ", remoteInfo:" + value.remoteInfo); console.log("on message, message:" + value.message + ", remoteInfo:" + value.remoteInfo);
} }
udp.on('message', callback); udp.on('message', callback);
udp.bind({ udp.bind({
address: '192.168.xx.xxx', port: xxxx, family: 1 address: '192.168.xx.xxx', port: 8080, family: 1
}, err => { }, error => {
if (err) { if (error) {
console.log('bind fail'); console.log('bind fail');
} }
udp.on('message', (data) => { udp.on('message', (data) => {
console.log(JSON.stringify(data)) console.log(JSON.stringify(data))
}); });
netHandle.bindSocket(udp,(error, data)=>{ netHandle.bindSocket(udp, (error, data) => {
if (error) { if (error) {
console.log(JSON.stringify(error)); console.log(JSON.stringify(error));
} else { } else {
...@@ -1576,45 +1576,41 @@ bindSocket(socketParam: TCPSocket \| UDPSocket): Promise\<void>; ...@@ -1576,45 +1576,41 @@ bindSocket(socketParam: TCPSocket \| UDPSocket): Promise\<void>;
```js ```js
import socket from "@ohos.net.socket"; import socket from "@ohos.net.socket";
connection.getDefaultNet().then((netHandle)=>{ connection.getDefaultNet().then((netHandle) => {
var tcp = socket.constructTCPSocketInstance(); var tcp = socket.constructTCPSocketInstance();
var udp = socket.constructUDPSocketInstance(); var udp = socket.constructUDPSocketInstance();
let socketType = "TCPSocket"; let socketType = "TCPSocket";
if (socketType == "TCPSocket") { if (socketType == "TCPSocket") {
tcp.bind({ tcp.bind({
address: '192.168.xx.xxx', port: xxxx, family: 1 address: '192.168.xx.xxx', port: 8080, family: 1
}, err => { }, error => {
if (err) { if (error) {
console.log('bind fail'); console.log('bind fail');
} }
netHandle.bindSocket(tcp).then((err, data) => { netHandle.bindSocket(tcp).then((data) => {
if (err) { console.log(JSON.stringify(data));
console.log(JSON.stringify(err)); }).catch(error => {
} else { console.log(JSON.stringify(error));
console.log(JSON.stringify(data));
}
}) })
}) })
} else { } else {
let callback = value => { let callback = value => {
console.log(TAG + "on message, message:" + value.message + ", remoteInfo:" + value.remoteInfo); console.log("on message, message:" + value.message + ", remoteInfo:" + value.remoteInfo);
} }
udp.on('message', callback); udp.on('message', callback);
udp.bind({ udp.bind({
address: '192.168.xx.xxx', port: xxxx, family: 1 address: '192.168.xx.xxx', port: 8080, family: 1
}, err => { }, error => {
if (err) { if (error) {
console.log('bind fail'); console.log('bind fail');
} }
udp.on('message', (data) => { udp.on('message', (data) => {
console.log(JSON.stringify(data)); console.log(JSON.stringify(data));
}) })
netHandle.bindSocket(udp).then((err, data) => { netHandle.bindSocket(udp).then((data) => {
if (err) { console.log(JSON.stringify(data));
console.log(JSON.stringify(err)); }).catch(error => {
} else { console.log(JSON.stringify(error));
console.log(JSON.stringify(data));
}
}) })
}) })
} }
......
...@@ -47,15 +47,21 @@ setIfaceConfig(iface: string, ic: InterfaceConfiguration, callback: AsyncCallbac ...@@ -47,15 +47,21 @@ setIfaceConfig(iface: string, ic: InterfaceConfiguration, callback: AsyncCallbac
**示例:** **示例:**
```js ```js
ethernet.setIfaceConfig("eth0", {mode:ethernet.STATIC,ipAddr:"192.168.xx.xx", routeAddr:"192.168.xx.xx", ethernet.setIfaceConfig("eth0", {
gateAddr:"192.168.xx.xx", maskAddr:"255.255.xx.xx", dnsAddr0:"1.1.xx.xx", dnsAddr1:"2.2.xx.xx"}, mode: 0,
(error) => { ipAddr: "192.168.xx.xxx",
if (error) { route: "192.168.xx.xxx",
console.log("setIfaceConfig callback error = " + error); gateway: "192.168.xx.xxx",
} else { netMask: "255.255.255.0",
console.log("setIfaceConfig callback ok "); dnsServers: "1.1.1.1",
} domain: "2.2.2.2"
}); }, (error) => {
if (error) {
console.log("setIfaceConfig callback error = " + error);
} else {
console.log("setIfaceConfig callback ok ");
}
});
``` ```
## ethernet.setIfaceConfig ## ethernet.setIfaceConfig
...@@ -99,10 +105,17 @@ setIfaceConfig(iface: string, ic: InterfaceConfiguration): Promise\<void> ...@@ -99,10 +105,17 @@ setIfaceConfig(iface: string, ic: InterfaceConfiguration): Promise\<void>
**示例:** **示例:**
```js ```js
ethernet.setIfaceConfig("eth0", {mode:ethernet.STATIC,ipAddr:"192.168.xx.xx", routeAddr:"192.168.xx.xx", ethernet.setIfaceConfig("eth0", {
gateAddr:"192.168.xx.xx", maskAddr:"255.255.xx.xx", dnsAddr0:"1.1.xx.xx", dnsAddr1:"2.2.xx.xx"}).then(() => { mode: 0,
ipAddr: "192.168.xx.xxx",
route: "192.168.xx.xxx",
gateway: "192.168.xx.xxx",
netMask: "255.255.255.0",
dnsServers: "1.1.1.1",
domain: "2.2.2.2"
}).then(() => {
console.log("setIfaceConfig promiss ok "); console.log("setIfaceConfig promiss ok ");
}).catch((error) => { }).catch(error => {
console.log("setIfaceConfig promiss error = " + error); console.log("setIfaceConfig promiss error = " + error);
}); });
``` ```
...@@ -146,11 +159,11 @@ ethernet.getIfaceConfig("eth0", (error, value) => { ...@@ -146,11 +159,11 @@ ethernet.getIfaceConfig("eth0", (error, value) => {
} else { } else {
console.log("getIfaceConfig callback mode = " + value.mode); console.log("getIfaceConfig callback mode = " + value.mode);
console.log("getIfaceConfig callback ipAddr = " + value.ipAddr); console.log("getIfaceConfig callback ipAddr = " + value.ipAddr);
console.log("getIfaceConfig callback routeAddr = " + value.routeAddr); console.log("getIfaceConfig callback route = " + value.route);
console.log("getIfaceConfig callback gateAddr = " + value.gateAddr); console.log("getIfaceConfig callback gateway = " + value.gateway);
console.log("getIfaceConfig callback maskAddr = " + value.maskAddr); console.log("getIfaceConfig callback netMask = " + value.netMask);
console.log("getIfaceConfig callback dns0Addr = " + value.dns0Addr); console.log("getIfaceConfig callback dnsServers = " + value.dnsServers);
console.log("getIfaceConfig callback dns1Addr = " + value.dns1Addr); console.log("getIfaceConfig callback domain = " + value.domain);
} }
}); });
``` ```
...@@ -196,12 +209,12 @@ getIfaceConfig(iface: string): Promise\<InterfaceConfiguration> ...@@ -196,12 +209,12 @@ getIfaceConfig(iface: string): Promise\<InterfaceConfiguration>
ethernet.getIfaceConfig("eth0").then((data) => { ethernet.getIfaceConfig("eth0").then((data) => {
console.log("getIfaceConfig promiss mode = " + data.mode); console.log("getIfaceConfig promiss mode = " + data.mode);
console.log("getIfaceConfig promiss ipAddr = " + data.ipAddr); console.log("getIfaceConfig promiss ipAddr = " + data.ipAddr);
console.log("getIfaceConfig promiss routeAddr = " + data.routeAddr); console.log("getIfaceConfig promiss route = " + data.route);
console.log("getIfaceConfig promiss gateAddr = " + data.gateAddr); console.log("getIfaceConfig promiss gateway = " + data.gateway);
console.log("getIfaceConfig promiss maskAddr = " + data.maskAddr); console.log("getIfaceConfig promiss netMask = " + data.netMask);
console.log("getIfaceConfig promiss dns0Addr = " + data.dns0Addr); console.log("getIfaceConfig promiss dnsServers = " + data.dnsServers);
console.log("getIfaceConfig promiss dns1Addr = " + data.dns1Addr); console.log("getIfaceConfig promiss domain = " + data.domain);
}).catch((error) => { }).catch(error => {
console.log("getIfaceConfig promiss error = " + error); console.log("getIfaceConfig promiss error = " + error);
}); });
``` ```
...@@ -240,11 +253,11 @@ isIfaceActive(iface: string, callback: AsyncCallback\<number>): void ...@@ -240,11 +253,11 @@ isIfaceActive(iface: string, callback: AsyncCallback\<number>): void
```js ```js
ethernet.isIfaceActive("eth0", (error, value) => { ethernet.isIfaceActive("eth0", (error, value) => {
if (error) { if (error) {
console.log("whether2Activate callback error = " + error); console.log("whether2Activate callback error = " + error);
} else { } else {
console.log("whether2Activate callback = " + value); console.log("whether2Activate callback = " + value);
} }
}); });
``` ```
...@@ -287,9 +300,9 @@ isIfaceActive(iface: string): Promise\<number> ...@@ -287,9 +300,9 @@ isIfaceActive(iface: string): Promise\<number>
```js ```js
ethernet.isIfaceActive("eth0").then((data) => { ethernet.isIfaceActive("eth0").then((data) => {
console.log("isIfaceActive promiss = " + data); console.log("isIfaceActive promiss = " + data);
}).catch((error) => { }).catch(error => {
console.log("isIfaceActive promiss error = " + error); console.log("isIfaceActive promiss error = " + error);
}); });
``` ```
...@@ -323,14 +336,14 @@ getAllActiveIfaces(callback: AsyncCallback\<Array\<string>>): void ...@@ -323,14 +336,14 @@ getAllActiveIfaces(callback: AsyncCallback\<Array\<string>>): void
```js ```js
ethernet.getAllActiveIfaces((error, value) => { ethernet.getAllActiveIfaces((error, value) => {
if (error) { if (error) {
console.log("getAllActiveIfaces callback error = " + error); console.log("getAllActiveIfaces callback error = " + error);
} else { } else {
console.log("getAllActiveIfaces callback value.length = " + value.length); console.log("getAllActiveIfaces callback value.length = " + value.length);
for (let i = 0; i < value.length; i++) { for (let i = 0; i < value.length; i++) {
console.log("getAllActiveIfaces callback = " + value[i]); console.log("getAllActiveIfaces callback = " + value[i]);
}
} }
}
}); });
``` ```
...@@ -365,11 +378,11 @@ getAllActiveIfaces(): Promise\<Array\<string>> ...@@ -365,11 +378,11 @@ getAllActiveIfaces(): Promise\<Array\<string>>
```js ```js
ethernet.getAllActiveIfaces().then((data) => { ethernet.getAllActiveIfaces().then((data) => {
console.log("getAllActiveIfaces promiss data.length = " + data.length); console.log("getAllActiveIfaces promiss data.length = " + data.length);
for (let i = 0; i < data.length; i++) { for (let i = 0; i < data.length; i++) {
console.log("getAllActiveIfaces promiss = " + data[i]); console.log("getAllActiveIfaces promiss = " + data[i]);
} }
}).catch((error) => { }).catch(error => {
console.log("getAllActiveIfaces promiss error = " + error); console.log("getAllActiveIfaces promiss error = " + error);
}); });
``` ```
......
...@@ -194,7 +194,8 @@ startSharing(type: SharingIfaceType, callback: AsyncCallback\<void>): void ...@@ -194,7 +194,8 @@ startSharing(type: SharingIfaceType, callback: AsyncCallback\<void>): void
```js ```js
import SharingIfaceType from '@ohos.net.sharing' import SharingIfaceType from '@ohos.net.sharing'
sharing.startSharing(SharingIfaceType.SHARING_WIFI, (error) => { let SHARING_WIFI=0;
sharing.startSharing(SHARING_WIFI, (error) => {
console.log(JSON.stringify(error)); console.log(JSON.stringify(error));
}); });
``` ```
...@@ -242,7 +243,8 @@ startSharing(type: SharingIfaceType): Promise\<void> ...@@ -242,7 +243,8 @@ startSharing(type: SharingIfaceType): Promise\<void>
```js ```js
import SharingIfaceType from '@ohos.net.sharing' import SharingIfaceType from '@ohos.net.sharing'
sharing.startSharing(SharingIfaceType.SHARING_WIFI).then(() => { let SHARING_WIFI=0;
sharing.startSharing(SHARING_WIFI).then(() => {
console.log("start wifi sharing successful"); console.log("start wifi sharing successful");
}).catch(error => { }).catch(error => {
console.log("start wifi sharing failed"); console.log("start wifi sharing failed");
...@@ -285,7 +287,8 @@ stopSharing(type: SharingIfaceType, callback: AsyncCallback\<void>): void ...@@ -285,7 +287,8 @@ stopSharing(type: SharingIfaceType, callback: AsyncCallback\<void>): void
```js ```js
import SharingIfaceType from '@ohos.net.sharing' import SharingIfaceType from '@ohos.net.sharing'
sharing.stopSharing(SharingIfaceType.SHARING_WIFI, (error) => { let SHARING_WIFI=0;
sharing.stopSharing(SHARING_WIFI, (error) => {
console.log(JSON.stringify(error)); console.log(JSON.stringify(error));
}); });
``` ```
...@@ -331,7 +334,8 @@ stopSharing(type: SharingIfaceType): Promise\<void> ...@@ -331,7 +334,8 @@ stopSharing(type: SharingIfaceType): Promise\<void>
```js ```js
import SharingIfaceType from '@ohos.net.sharing' import SharingIfaceType from '@ohos.net.sharing'
sharing.stopSharing(SharingIfaceType.SHARING_WIFI).then(() => { let SHARING_WIFI=0;
sharing.stopSharing(SHARING_WIFI).then(() => {
console.log("stop wifi sharing successful"); console.log("stop wifi sharing successful");
}).catch(error => { }).catch(error => {
console.log("stop wifi sharing failed"); console.log("stop wifi sharing failed");
...@@ -584,7 +588,8 @@ getSharingIfaces(state: SharingIfaceState, callback: AsyncCallback\<Array\<strin ...@@ -584,7 +588,8 @@ getSharingIfaces(state: SharingIfaceState, callback: AsyncCallback\<Array\<strin
```js ```js
import SharingIfaceState from '@ohos.net.sharing' import SharingIfaceState from '@ohos.net.sharing'
sharing.getSharingIfaces(SharingIfaceState.SHARING_NIC_CAN_SERVER, (error, data) => { let SHARING_BLUETOOTH=2;
sharing.getSharingIfaces(SHARING_BLUETOOTH, (error, data) => {
console.log(JSON.stringify(error)); console.log(JSON.stringify(error));
console.log(JSON.stringify(data)); console.log(JSON.stringify(data));
}); });
...@@ -628,7 +633,8 @@ getSharingIfaces(state: SharingIfaceState): Promise\<Array\<string>> ...@@ -628,7 +633,8 @@ getSharingIfaces(state: SharingIfaceState): Promise\<Array\<string>>
```js ```js
import SharingIfaceState from '@ohos.net.sharing' import SharingIfaceState from '@ohos.net.sharing'
sharing.getSharingIfaces(SharingIfaceState.SHARING_NIC_CAN_SERVER).then(data => { let SHARING_BLUETOOTH=2;
sharing.getSharingIfaces(SHARING_BLUETOOTH).then(data => {
console.log(JSON.stringify(data)); console.log(JSON.stringify(data));
}).catch(error => { }).catch(error => {
console.log(JSON.stringify(error)); console.log(JSON.stringify(error));
...@@ -668,7 +674,8 @@ getSharingState(type: SharingIfaceType, callback: AsyncCallback\<SharingIfaceSta ...@@ -668,7 +674,8 @@ getSharingState(type: SharingIfaceType, callback: AsyncCallback\<SharingIfaceSta
```js ```js
import SharingIfaceType from '@ohos.net.sharing' import SharingIfaceType from '@ohos.net.sharing'
sharing.getSharingState(SharingIfaceType.SHARING_WIFI, (error, data) => { let SHARING_WIFI=0;
sharing.getSharingState(SHARING_WIFI, (error, data) => {
console.log(JSON.stringify(error)); console.log(JSON.stringify(error));
console.log(JSON.stringify(data)); console.log(JSON.stringify(data));
}); });
...@@ -712,7 +719,8 @@ getSharingState(type: SharingIfaceType): Promise\<SharingIfaceState> ...@@ -712,7 +719,8 @@ getSharingState(type: SharingIfaceType): Promise\<SharingIfaceState>
```js ```js
import SharingIfaceType from '@ohos.net.sharing' import SharingIfaceType from '@ohos.net.sharing'
sharing.getSharingState(SharingIfaceType.SHARING_WIFI).then(data => { let SHARING_WIFI=0;
sharing.getSharingState(SHARING_WIFI).then(data => {
console.log(JSON.stringify(data)); console.log(JSON.stringify(data));
}).catch(error => { }).catch(error => {
console.log(JSON.stringify(error)); console.log(JSON.stringify(error));
...@@ -752,7 +760,8 @@ getSharableRegexes(type: SharingIfaceType, callback: AsyncCallback\<Array\<strin ...@@ -752,7 +760,8 @@ getSharableRegexes(type: SharingIfaceType, callback: AsyncCallback\<Array\<strin
```js ```js
import SharingIfaceType from '@ohos.net.sharing' import SharingIfaceType from '@ohos.net.sharing'
sharing.getSharableRegexes(SharingIfaceType.SHARING_WIFI, (error, data) => { let SHARING_WIFI=0;
sharing.getSharableRegexes(SHARING_WIFI, (error, data) => {
console.log(JSON.stringify(error)); console.log(JSON.stringify(error));
console.log(JSON.stringify(data)); console.log(JSON.stringify(data));
}); });
...@@ -796,7 +805,8 @@ getSharableRegexes(type: SharingIfaceType): Promise\<Array\<string>> ...@@ -796,7 +805,8 @@ getSharableRegexes(type: SharingIfaceType): Promise\<Array\<string>>
```js ```js
import SharingIfaceType from '@ohos.net.sharing' import SharingIfaceType from '@ohos.net.sharing'
sharing.getSharableRegexes(SharingIfaceType.SHARING_WIFI).then(data => { let SHARING_WIFI=0;
sharing.getSharableRegexes(SHARING_WIFI).then(data => {
console.log(JSON.stringify(data)); console.log(JSON.stringify(data));
}).catch(error => { }).catch(error => {
console.log(JSON.stringify(error)); console.log(JSON.stringify(error));
...@@ -832,9 +842,8 @@ on(type: 'sharingStateChange', callback: Callback\<boolean>): void ...@@ -832,9 +842,8 @@ on(type: 'sharingStateChange', callback: Callback\<boolean>): void
**示例:** **示例:**
```js ```js
sharing.on('sharingStateChange', (error, data) => { sharing.on('sharingStateChange', (data) => {
console.log(JSON.stringify(error)); console.log('on sharingStateChange:' + JSON.stringify(data));
console.log(JSON.stringify(data));
}); });
``` ```
...@@ -867,8 +876,7 @@ off(type: 'sharingStateChange', callback?: Callback\<boolean>): void ...@@ -867,8 +876,7 @@ off(type: 'sharingStateChange', callback?: Callback\<boolean>): void
**示例:** **示例:**
```js ```js
sharing.off('sharingStateChange', (error, data) => { sharing.off('sharingStateChange', (data) => {
console.log(JSON.stringify(error));
console.log(JSON.stringify(data)); console.log(JSON.stringify(data));
}); });
``` ```
...@@ -902,9 +910,8 @@ on(type: 'interfaceSharingStateChange', callback: Callback\<{ type: SharingIface ...@@ -902,9 +910,8 @@ on(type: 'interfaceSharingStateChange', callback: Callback\<{ type: SharingIface
**示例:** **示例:**
```js ```js
sharing.on('interfaceSharingStateChange', (error, data) => { sharing.on('interfaceSharingStateChange', (data) => {
console.log(JSON.stringify(error)); console.log('on interfaceSharingStateChange:' + JSON.stringify(data));
console.log(JSON.stringify(data));
}); });
``` ```
...@@ -937,8 +944,7 @@ off(type: 'interfaceSharingStateChange', callback?: Callback\<{ type: SharingIfa ...@@ -937,8 +944,7 @@ off(type: 'interfaceSharingStateChange', callback?: Callback\<{ type: SharingIfa
**示例:** **示例:**
```js ```js
sharing.off('interfaceSharingStateChange', (error, data) => { sharing.off('interfaceSharingStateChange', (data) => {
console.log(JSON.stringify(error));
console.log(JSON.stringify(data)); console.log(JSON.stringify(data));
}); });
``` ```
...@@ -972,9 +978,8 @@ on(type: 'sharingUpstreamChange', callback: Callback\<NetHandle>): void ...@@ -972,9 +978,8 @@ on(type: 'sharingUpstreamChange', callback: Callback\<NetHandle>): void
**示例:** **示例:**
```js ```js
sharing.on('sharingUpstreamChange', (error, data) => { sharing.on('sharingUpstreamChange', (data) => {
console.log(JSON.stringify(error)); console.log('on sharingUpstreamChange:' + JSON.stringify(data));
console.log(JSON.stringify(data));
}); });
``` ```
...@@ -1007,8 +1012,7 @@ off(type: 'sharingUpstreamChange', callback?: Callback\<NetHandle>): void ...@@ -1007,8 +1012,7 @@ off(type: 'sharingUpstreamChange', callback?: Callback\<NetHandle>): void
**示例:** **示例:**
```js ```js
sharing.off('sharingUpstreamChange', (error, data) => { sharing.off('sharingUpstreamChange', (data) => {
console.log(JSON.stringify(error));
console.log(JSON.stringify(data)); console.log(JSON.stringify(data));
}); });
``` ```
......
...@@ -1998,7 +1998,7 @@ connect(options: TLSConnectOptions, callback: AsyncCallback\<void>): void ...@@ -1998,7 +1998,7 @@ connect(options: TLSConnectOptions, callback: AsyncCallback\<void>): void
```js ```js
let tlsTwoWay = socket.constructTLSSocketInstance(); // Two way authentication let tlsTwoWay = socket.constructTLSSocketInstance(); // Two way authentication
tlsTwoWay.bind({address: '192.168.xxx.xxx', port: xxxx, family: 1}, err => { tlsTwoWay.bind({address: '192.168.xxx.xxx', port: 8080, family: 1}, err => {
if (err) { if (err) {
console.log('bind fail'); console.log('bind fail');
return; return;
...@@ -2009,14 +2009,14 @@ let options = { ...@@ -2009,14 +2009,14 @@ let options = {
ALPNProtocols: ["spdy/1", "http/1.1"], ALPNProtocols: ["spdy/1", "http/1.1"],
address: { address: {
address: "192.168.xx.xxx", address: "192.168.xx.xxx",
port: xxxx, port: 8080,
family: 1, family: 1,
}, },
secureOptions: { secureOptions: {
key: "xxxx", key: "xxxx",
cert: "xxxx", cert: "xxxx",
ca: ["xxxx"], ca: ["xxxx"],
passwd: "xxxx", password: "xxxx",
protocols: [socket.Protocol.TLSv12], protocols: [socket.Protocol.TLSv12],
useRemoteCipherPrefer: true, useRemoteCipherPrefer: true,
signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256", signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256",
...@@ -2024,12 +2024,12 @@ let options = { ...@@ -2024,12 +2024,12 @@ let options = {
}, },
}; };
tlsTwoWay.connect(options, (err, data) => { tlsTwoWay.connect(options, (err, data) => {
console.error(err); console.error("connect callback error"+err);
console.log(data); console.log(JSON.stringify(data));
}); });
let tlsOneWay = socket.constructTLSSocketInstance(); // One way authentication let tlsOneWay = socket.constructTLSSocketInstance(); // One way authentication
tlsOneWay.bind({address: '192.168.xxx.xxx', port: xxxx, family: 1}, err => { tlsOneWay.bind({address: '192.168.xxx.xxx', port: 8080, family: 1}, err => {
if (err) { if (err) {
console.log('bind fail'); console.log('bind fail');
return; return;
...@@ -2039,7 +2039,7 @@ tlsOneWay.bind({address: '192.168.xxx.xxx', port: xxxx, family: 1}, err => { ...@@ -2039,7 +2039,7 @@ tlsOneWay.bind({address: '192.168.xxx.xxx', port: xxxx, family: 1}, err => {
let oneWayOptions = { let oneWayOptions = {
address: { address: {
address: "192.168.xxx.xxx", address: "192.168.xxx.xxx",
port: xxxx, port: 8080,
family: 1, family: 1,
}, },
secureOptions: { secureOptions: {
...@@ -2048,8 +2048,8 @@ let oneWayOptions = { ...@@ -2048,8 +2048,8 @@ let oneWayOptions = {
}, },
}; };
tlsOneWay.connect(oneWayOptions, (err, data) => { tlsOneWay.connect(oneWayOptions, (err, data) => {
console.error(err); console.error("connect callback error"+err);
console.log(data); console.log(JSON.stringify(data));
}); });
``` ```
...@@ -2098,7 +2098,7 @@ connect(options: TLSConnectOptions): Promise\<void> ...@@ -2098,7 +2098,7 @@ connect(options: TLSConnectOptions): Promise\<void>
```js ```js
let tlsTwoWay = socket.constructTLSSocketInstance(); // Two way authentication let tlsTwoWay = socket.constructTLSSocketInstance(); // Two way authentication
tlsTwoWay.bind({address: '192.168.xxx.xxx', port: xxxx, family: 1}, err => { tlsTwoWay.bind({address: '192.168.xxx.xxx', port: 8080, family: 1}, err => {
if (err) { if (err) {
console.log('bind fail'); console.log('bind fail');
return; return;
...@@ -2109,14 +2109,14 @@ let options = { ...@@ -2109,14 +2109,14 @@ let options = {
ALPNProtocols: ["spdy/1", "http/1.1"], ALPNProtocols: ["spdy/1", "http/1.1"],
address: { address: {
address: "xxxx", address: "xxxx",
port: xxxx, port: 8080,
family: 1, family: 1,
}, },
secureOptions: { secureOptions: {
key: "xxxx", key: "xxxx",
cert: "xxxx", cert: "xxxx",
ca: ["xxxx"], ca: ["xxxx"],
passwd: "xxxx", password: "xxxx",
protocols: [socket.Protocol.TLSv12], protocols: [socket.Protocol.TLSv12],
useRemoteCipherPrefer: true, useRemoteCipherPrefer: true,
signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256", signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256",
...@@ -2124,13 +2124,13 @@ let options = { ...@@ -2124,13 +2124,13 @@ let options = {
}, },
}; };
tlsTwoWay.connect(options).then(data => { tlsTwoWay.connect(options).then(data => {
console.log(data); console.log(JSON.stringify(data));
}).catch(err => { }).catch(err => {
console.error(err); console.error(err);
}); });
let tlsOneWay = socket.constructTLSSocketInstance(); // One way authentication let tlsOneWay = socket.constructTLSSocketInstance(); // One way authentication
tlsOneWay.bind({address: '192.168.xxx.xxx', port: xxxx, family: 1}, err => { tlsOneWay.bind({address: '192.168.xxx.xxx', port: 8080, family: 1}, err => {
if (err) { if (err) {
console.log('bind fail'); console.log('bind fail');
return; return;
...@@ -2140,7 +2140,7 @@ tlsOneWay.bind({address: '192.168.xxx.xxx', port: xxxx, family: 1}, err => { ...@@ -2140,7 +2140,7 @@ tlsOneWay.bind({address: '192.168.xxx.xxx', port: xxxx, family: 1}, err => {
let oneWayOptions = { let oneWayOptions = {
address: { address: {
address: "192.168.xxx.xxx", address: "192.168.xxx.xxx",
port: xxxx, port: 8080,
family: 1, family: 1,
}, },
secureOptions: { secureOptions: {
...@@ -2149,7 +2149,7 @@ let oneWayOptions = { ...@@ -2149,7 +2149,7 @@ let oneWayOptions = {
}, },
}; };
tlsOneWay.connect(oneWayOptions).then(data => { tlsOneWay.connect(oneWayOptions).then(data => {
console.log(data); console.log(JSON.stringify(data));
}).catch(err => { }).catch(err => {
console.error(err); console.error(err);
}); });
...@@ -2478,7 +2478,7 @@ getCipherSuite(): Promise\<Array\<string>> ...@@ -2478,7 +2478,7 @@ getCipherSuite(): Promise\<Array\<string>>
```js ```js
tls.getCipherSuite().then(data => { tls.getCipherSuite().then(data => {
console.log(data); console.log('getCipherSuite success:' + JSON.stringify(data));
}).catch(err => { }).catch(err => {
console.error(err); console.error(err);
}); });
...@@ -2542,7 +2542,7 @@ getSignatureAlgorithms(): Promise\<Array\<string>> ...@@ -2542,7 +2542,7 @@ getSignatureAlgorithms(): Promise\<Array\<string>>
```js ```js
tls.getSignatureAlgorithms().then(data => { tls.getSignatureAlgorithms().then(data => {
console.log(data); console.log("getSignatureAlgorithms success" + data);
}).catch(err => { }).catch(err => {
console.error(err); console.error(err);
}); });
...@@ -2718,7 +2718,7 @@ TLS安全相关操作,其中ca证书为必选参数,其他参数为可选参 ...@@ -2718,7 +2718,7 @@ TLS安全相关操作,其中ca证书为必选参数,其他参数为可选参
| ca | string \| Array\<string> | 是 | 服务端的ca证书,用于认证校验服务端的数字证书。| | ca | string \| Array\<string> | 是 | 服务端的ca证书,用于认证校验服务端的数字证书。|
| cert | string | 否 | 本地客户端的数字证书。 | | cert | string | 否 | 本地客户端的数字证书。 |
| key | string | 否 | 本地数字证书的私钥。 | | key | string | 否 | 本地数字证书的私钥。 |
| passwd | string | 否 | 读取私钥的密码。 | | password | string | 否 | 读取私钥的密码。 |
| protocols | [Protocol](#protocol9) \|Array\<[Protocol](#protocol9)> | 否 | TLS的协议版本。 | | protocols | [Protocol](#protocol9) \|Array\<[Protocol](#protocol9)> | 否 | TLS的协议版本。 |
| useRemoteCipherPrefer | boolean | 否 | 优先使用对等方的密码套件。 | | useRemoteCipherPrefer | boolean | 否 | 优先使用对等方的密码套件。 |
| signatureAlgorithms | string | 否 | 通信过程中的签名算法。 | | signatureAlgorithms | string | 否 | 通信过程中的签名算法。 |
...@@ -2743,4 +2743,4 @@ TLS通信的协议版本。 ...@@ -2743,4 +2743,4 @@ TLS通信的协议版本。
| 类型 | 说明 | | 类型 | 说明 |
| --------------------------------------------------------------------- | --------------------- | | --------------------------------------------------------------------- | --------------------- |
|[cryptoFramework.EncodingBlob](js-apis-cryptoFramework.md#datablob) | 存储证书的数据和编码格式 | |[cryptoFramework.EncodingBlob](js-apis-cryptoFramework.md#datablob) | 存储证书的数据和编码格式 |
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册