Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
732ecacf
D
Docs
项目概览
OpenHarmony
/
Docs
大约 2 年 前同步成功
通知
161
Star
293
Fork
28
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
Docs
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
732ecacf
编写于
4月 13, 2023
作者:
Y
Yangys
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Modify indent
Signed-off-by:
N
Yangys
<
yangyousheng@huawei.com
>
上级
4efaacd9
变更
14
隐藏空白更改
内联
并排
Showing
14 changed file
with
1670 addition
and
1614 deletion
+1670
-1614
zh-cn/application-dev/connectivity/http-request.md
zh-cn/application-dev/connectivity/http-request.md
+74
-72
zh-cn/application-dev/connectivity/net-connection-manager.md
zh-cn/application-dev/connectivity/net-connection-manager.md
+153
-144
zh-cn/application-dev/connectivity/net-ethernet.md
zh-cn/application-dev/connectivity/net-ethernet.md
+105
-98
zh-cn/application-dev/connectivity/net-sharing.md
zh-cn/application-dev/connectivity/net-sharing.md
+63
-56
zh-cn/application-dev/connectivity/socket-connection.md
zh-cn/application-dev/connectivity/socket-connection.md
+133
-132
zh-cn/application-dev/connectivity/websocket-connection.md
zh-cn/application-dev/connectivity/websocket-connection.md
+2
-4
zh-cn/application-dev/reference/apis/js-apis-http.md
zh-cn/application-dev/reference/apis/js-apis-http.md
+141
-138
zh-cn/application-dev/reference/apis/js-apis-net-connection.md
.../application-dev/reference/apis/js-apis-net-connection.md
+202
-208
zh-cn/application-dev/reference/apis/js-apis-net-ethernet.md
zh-cn/application-dev/reference/apis/js-apis-net-ethernet.md
+61
-61
zh-cn/application-dev/reference/apis/js-apis-net-mdns.md
zh-cn/application-dev/reference/apis/js-apis-net-mdns.md
+80
-79
zh-cn/application-dev/reference/apis/js-apis-net-policy.md
zh-cn/application-dev/reference/apis/js-apis-net-policy.md
+119
-99
zh-cn/application-dev/reference/apis/js-apis-net-sharing.md
zh-cn/application-dev/reference/apis/js-apis-net-sharing.md
+58
-46
zh-cn/application-dev/reference/apis/js-apis-socket.md
zh-cn/application-dev/reference/apis/js-apis-socket.md
+396
-396
zh-cn/application-dev/reference/apis/js-apis-webSocket.md
zh-cn/application-dev/reference/apis/js-apis-webSocket.md
+83
-81
未找到文件。
zh-cn/application-dev/connectivity/http-request.md
浏览文件 @
732ecacf
...
...
@@ -49,44 +49,44 @@ let httpRequest = http.createHttp();
// 用于订阅HTTP响应头,此接口会比request请求先返回。可以根据业务需要订阅此消息
// 从API 8开始,使用on('headersReceive', Callback)替代on('headerReceive', AsyncCallback)。 8+
httpRequest
.
on
(
'
headersReceive
'
,
(
header
)
=>
{
console
.
info
(
'
header:
'
+
JSON
.
stringify
(
header
));
console
.
info
(
'
header:
'
+
JSON
.
stringify
(
header
));
});
httpRequest
.
request
(
// 填写HTTP请求的URL地址,可以带参数也可以不带参数。URL地址需要开发者自定义。请求的参数可以在extraData中指定
"
EXAMPLE_URL
"
,
{
method
:
http
.
RequestMethod
.
POST
,
// 可选,默认为http.RequestMethod.GET
// 开发者根据自身业务需要添加header字段
header
:
{
'
Content-Type
'
:
'
application/json
'
},
// 当使用POST请求时此字段用于传递内容
extraData
:
{
"
data
"
:
"
data to send
"
,
},
expectDataType
:
http
.
HttpDataType
.
STRING
,
// 可选,指定返回数据的类型
usingCache
:
true
,
// 可选,默认为true
priority
:
1
,
// 可选,默认为1
connectTimeout
:
60000
,
// 可选,默认为60000ms
readTimeout
:
60000
,
// 可选,默认为60000ms
usingProtocol
:
http
.
HttpProtocol
.
HTTP1_1
,
// 可选,协议类型默认值由系统自动指定
usingProxy
:
false
,
//可选,默认不使用网络代理,自API 10开始支持该属性
},
(
err
,
data
)
=>
{
if
(
!
err
)
{
// data.result为HTTP响应内容,可根据业务需要进行解析
console
.
info
(
'
Result:
'
+
JSON
.
stringify
(
data
.
result
));
console
.
info
(
'
code:
'
+
JSON
.
stringify
(
data
.
responseCode
));
// data.header为HTTP响应头,可根据业务需要进行解析
console
.
info
(
'
header:
'
+
JSON
.
stringify
(
data
.
header
));
console
.
info
(
'
cookies:
'
+
JSON
.
stringify
(
data
.
cookies
));
// 8+
}
else
{
console
.
info
(
'
error:
'
+
JSON
.
stringify
(
err
));
// 取消订阅HTTP响应头事件
httpRequest
.
off
(
'
headersReceive
'
);
// 当该请求使用完毕时,调用destroy方法主动销毁
httpRequest
.
destroy
();
}
// 填写HTTP请求的URL地址,可以带参数也可以不带参数。URL地址需要开发者自定义。请求的参数可以在extraData中指定
"
EXAMPLE_URL
"
,
{
method
:
http
.
RequestMethod
.
POST
,
// 可选,默认为http.RequestMethod.GET
// 开发者根据自身业务需要添加header字段
header
:
{
'
Content-Type
'
:
'
application/json
'
},
// 当使用POST请求时此字段用于传递内容
extraData
:
{
"
data
"
:
"
data to send
"
,
},
expectDataType
:
http
.
HttpDataType
.
STRING
,
// 可选,指定返回数据的类型
usingCache
:
true
,
// 可选,默认为true
priority
:
1
,
// 可选,默认为1
connectTimeout
:
60000
,
// 可选,默认为60000ms
readTimeout
:
60000
,
// 可选,默认为60000ms
usingProtocol
:
http
.
HttpProtocol
.
HTTP1_1
,
// 可选,协议类型默认值由系统自动指定
usingProxy
:
false
,
//可选,默认不使用网络代理,自API 10开始支持该属性
},
(
err
,
data
)
=>
{
if
(
!
err
)
{
// data.result为HTTP响应内容,可根据业务需要进行解析
console
.
info
(
'
Result:
'
+
JSON
.
stringify
(
data
.
result
));
console
.
info
(
'
code:
'
+
JSON
.
stringify
(
data
.
responseCode
));
// data.header为HTTP响应头,可根据业务需要进行解析
console
.
info
(
'
header:
'
+
JSON
.
stringify
(
data
.
header
));
console
.
info
(
'
cookies:
'
+
JSON
.
stringify
(
data
.
cookies
));
// 8+
}
else
{
console
.
info
(
'
error:
'
+
JSON
.
stringify
(
err
));
// 取消订阅HTTP响应头事件
httpRequest
.
off
(
'
headersReceive
'
);
// 当该请求使用完毕时,调用destroy方法主动销毁
httpRequest
.
destroy
();
}
}
);
```
...
...
@@ -108,61 +108,63 @@ import http from '@ohos.net.http'
let
httpRequest
=
http
.
createHttp
();
// 用于订阅HTTP响应头事件
httpRequest
.
on
(
'
headersReceive
'
,
(
header
)
=>
{
console
.
info
(
'
header:
'
+
JSON
.
stringify
(
header
));
console
.
info
(
'
header:
'
+
JSON
.
stringify
(
header
));
});
// 用于订阅HTTP流式响应数据接收事件
let
res
=
''
;
httpRequest
.
on
(
'
dataReceive
'
,
(
data
)
=>
{
res
+=
data
;
console
.
info
(
'
res:
'
+
res
);
res
+=
data
;
console
.
info
(
'
res:
'
+
res
);
});
// 用于订阅HTTP流式响应数据接收完毕事件
httpRequest
.
on
(
'
dataEnd
'
,
()
=>
{
console
.
info
(
'
No more data in response, data receive end
'
);
console
.
info
(
'
No more data in response, data receive end
'
);
});
// 用于订阅HTTP流式响应数据接收进度事件
httpRequest
.
on
(
'
dataProgress
'
,
(
data
)
=>
{
console
.
log
(
"
dataProgress receiveSize:
"
+
data
.
receiveSize
+
"
, totalSize:
"
+
data
.
totalSize
);
console
.
log
(
"
dataProgress receiveSize:
"
+
data
.
receiveSize
+
"
, totalSize:
"
+
data
.
totalSize
);
});
httpRequest
.
request2
(
// 填写HTTP请求的URL地址,可以带参数也可以不带参数。URL地址需要开发者自定义。请求的参数可以在extraData中指定
"
EXAMPLE_URL
"
,
{
method
:
http
.
RequestMethod
.
POST
,
// 可选,默认为http.RequestMethod.GET
// 开发者根据自身业务需要添加header字段
header
:
{
'
Content-Type
'
:
'
application/json
'
},
// 当使用POST请求时此字段用于传递内容
extraData
:
{
"
data
"
:
"
data to send
"
,
},
expectDataType
:
http
.
HttpDataType
.
STRING
,
// 可选,指定返回数据的类型
usingCache
:
true
,
// 可选,默认为true
priority
:
1
,
// 可选,默认为1
connectTimeout
:
60000
,
// 可选,默认为60000ms
readTimeout
:
60000
,
// 可选,默认为60000ms。若传输的数据较大,需要较长的时间,建议增大该参数以保证数据传输正常终止
usingProtocol
:
http
.
HttpProtocol
.
HTTP1_1
,
// 可选,协议类型默认值由系统自动指定
},
(
err
,
data
)
=>
{
console
.
info
(
'
error:
'
+
JSON
.
stringify
(
err
));
console
.
info
(
'
ResponseCode :
'
+
JSON
.
stringify
(
data
));
// 取消订阅HTTP响应头事件
httpRequest
.
off
(
'
headersReceive
'
);
// 取消订阅HTTP流式响应数据接收事件
httpRequest
.
off
(
'
dataReceive
'
);
// 取消订阅HTTP流式响应数据接收进度事件
httpRequest
.
off
(
'
dataProgress
'
);
// 取消订阅HTTP流式响应数据接收完毕事件
httpRequest
.
off
(
'
dataEnd
'
);
// 当该请求使用完毕时,调用destroy方法主动销毁
httpRequest
.
destroy
();
}
// 填写HTTP请求的URL地址,可以带参数也可以不带参数。URL地址需要开发者自定义。请求的参数可以在extraData中指定
"
EXAMPLE_URL
"
,
{
method
:
http
.
RequestMethod
.
POST
,
// 可选,默认为http.RequestMethod.GET
// 开发者根据自身业务需要添加header字段
header
:
{
'
Content-Type
'
:
'
application/json
'
},
// 当使用POST请求时此字段用于传递内容
extraData
:
{
"
data
"
:
"
data to send
"
,
},
expectDataType
:
http
.
HttpDataType
.
STRING
,
// 可选,指定返回数据的类型
usingCache
:
true
,
// 可选,默认为true
priority
:
1
,
// 可选,默认为1
connectTimeout
:
60000
,
// 可选,默认为60000ms
readTimeout
:
60000
,
// 可选,默认为60000ms。若传输的数据较大,需要较长的时间,建议增大该参数以保证数据传输正常终止
usingProtocol
:
http
.
HttpProtocol
.
HTTP1_1
,
// 可选,协议类型默认值由系统自动指定
},
(
err
,
data
)
=>
{
console
.
info
(
'
error:
'
+
JSON
.
stringify
(
err
));
console
.
info
(
'
ResponseCode :
'
+
JSON
.
stringify
(
data
));
// 取消订阅HTTP响应头事件
httpRequest
.
off
(
'
headersReceive
'
);
// 取消订阅HTTP流式响应数据接收事件
httpRequest
.
off
(
'
dataReceive
'
);
// 取消订阅HTTP流式响应数据接收进度事件
httpRequest
.
off
(
'
dataProgress
'
);
// 取消订阅HTTP流式响应数据接收完毕事件
httpRequest
.
off
(
'
dataEnd
'
);
// 当该请求使用完毕时,调用destroy方法主动销毁
httpRequest
.
destroy
();
}
);
```
## 相关实例
针对HTTP数据请求,有以下相关实例可供参考:
-
[
`Http:`数据请求(ArkTS)(API9))
](
https://gitee.com/openharmony/applications_app_samples/tree/master/code/BasicFeature/Connectivity/Http
)
-
[
使用HTTP实现与服务端通信(ArkTS)(API9)
](
https://gitee.com/openharmony/codelabs/tree/master/NetworkManagement/SmartChatEtsOH
)
\ No newline at end of file
zh-cn/application-dev/connectivity/net-connection-manager.md
浏览文件 @
732ecacf
# 网络连接管理
## 简介
网络连接管理提供管理网络一些基础能力,包括WiFi/蜂窝/Ethernet等多网络连接优先级管理、网络质量评估、订阅默认/指定网络连接状态变化、查询网络连接信息、DNS解析等功能。
> **说明:**
> 为了保证应用的运行效率,大部分API调用都是异步的,对于异步调用的API均提供了callback和Promise两种方式,以下示例均采用callback函数,更多方式可以查阅[API参考](../reference/apis/js-apis-net-connection.md)。
## 基本概念
-
网络生产者:数据网络的提供方,比如WiFi、蜂窝、Ethernet等。
-
网络消费者:数据网络的使用方,比如应用或系统服务。
-
网络探测:检测网络有效性,避免将网络从可用网络切换到不可用网络。内容包括绑定网络探测、DNS探测、HTTP探测及HTTPS探测。
-
网络优选:处理多网络共存时选择最优网络。在网络状态、网络信息及评分发生变化时被触发。
-
网络生产者:数据网络的提供方,比如WiFi、蜂窝、Ethernet等。
-
网络消费者:数据网络的使用方,比如应用或系统服务。
-
网络探测:检测网络有效性,避免将网络从可用网络切换到不可用网络。内容包括绑定网络探测、DNS探测、HTTP探测及HTTPS探测。
-
网络优选:处理多网络共存时选择最优网络。在网络状态、网络信息及评分发生变化时被触发。
## 约束
-
开发语言:C++ JS
-
系统:linux内核
-
本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
-
开发语言:C++ JS
-
系统:linux内核
-
本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
## 场景介绍
网络连接管理的典型场景有:
-
接收指定网络的状态变化通知
-
获取所有注册的网络
-
根据数据网络查询网络的连接信息
-
使用对应网络解析域名,获取所有IP
-
接收指定网络的状态变化通知
-
获取所有注册的网络
-
根据数据网络查询网络的连接信息
-
使用对应网络解析域名,获取所有IP
以下分别介绍具体开发方式。
## 接口说明
完整的JS API说明以及实例代码请参考:
[
网络连接管理
](
../reference/apis/js-apis-net-connection.md
)
。
| 类型 | 接口 | 功能说明 |
...
...
@@ -75,44 +82,46 @@
```
js
// 引入包名
import
connection
from
'
@ohos.net.connection
'
let
netCap
=
{
// 假设当前默认网络是WiFi,需要创建蜂窝网络连接,可指定网络类型为蜂窝网
bearerTypes
:
[
connection
.
NetBearType
.
BEARER_CELLULAR
],
// 指定网络能力为Internet
networkCap
:
[
connection
.
NetCap
.
NET_CAPABILITY_INTERNET
],
};
let
netSpec
=
{
netCapabilities
:
netCap
,
};
// 指定超时时间为10s(默认值为0)
let
timeout
=
10
*
1000
;
// 创建NetConnection对象
let
conn
=
connection
.
createNetConnection
(
netSpec
,
timeout
);
// 订阅事件,如果当前指定网络可用,通过on_netAvailable通知用户
conn
.
on
(
'
netAvailable
'
,
(
data
=>
{
console
.
log
(
"
net is available, netId is
"
+
data
.
netId
);
}));
// 订阅事件,如果当前指定网络不可用,通过on_netUnavailable通知用户
conn
.
on
(
'
netUnavailable
'
,
(
data
=>
{
console
.
log
(
"
net is unavailable, netId is
"
+
data
.
netId
);
}));
// 订阅指定网络状态变化的通知
conn
.
register
((
err
,
data
)
=>
{});
// 当不使用该网络时,可以调用该对象的unregister()方法,取消订阅
conn
.
unregister
((
err
,
data
)
=>
{});
import
connection
from
'
@ohos.net.connection
'
let
netCap
=
{
// 假设当前默认网络是WiFi,需要创建蜂窝网络连接,可指定网络类型为蜂窝网
bearerTypes
:
[
connection
.
NetBearType
.
BEARER_CELLULAR
],
// 指定网络能力为Internet
networkCap
:
[
connection
.
NetCap
.
NET_CAPABILITY_INTERNET
],
};
let
netSpec
=
{
netCapabilities
:
netCap
,
};
// 指定超时时间为10s(默认值为0)
let
timeout
=
10
*
1000
;
// 创建NetConnection对象
let
conn
=
connection
.
createNetConnection
(
netSpec
,
timeout
);
// 订阅事件,如果当前指定网络可用,通过on_netAvailable通知用户
conn
.
on
(
'
netAvailable
'
,
(
data
=>
{
console
.
log
(
"
net is available, netId is
"
+
data
.
netId
);
}));
// 订阅事件,如果当前指定网络不可用,通过on_netUnavailable通知用户
conn
.
on
(
'
netUnavailable
'
,
(
data
=>
{
console
.
log
(
"
net is unavailable, netId is
"
+
data
.
netId
);
}));
// 订阅指定网络状态变化的通知
conn
.
register
((
err
,
data
)
=>
{
});
// 当不使用该网络时,可以调用该对象的unregister()方法,取消订阅
conn
.
unregister
((
err
,
data
)
=>
{
});
```
##
获取所有注册的网络
## 获取所有注册的网络
###
开发步骤
### 开发步骤
1.
从@ohos.net.connection.d.ts中导入connection命名空间。
...
...
@@ -120,21 +129,21 @@
```
js
// 引入包名
import
connection
from
'
@ohos.net.connection
'
// 获取所有处于连接状态的网络列表
connection
.
getAllNets
((
err
,
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
err
));
console
.
log
(
JSON
.
stringify
(
data
));
if
(
data
)
{
this
.
netList
=
data
;
}
})
import
connection
from
'
@ohos.net.connection
'
// 获取所有处于连接状态的网络列表
connection
.
getAllNets
((
err
,
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
err
));
console
.
log
(
JSON
.
stringify
(
data
));
if
(
data
)
{
this
.
netList
=
data
;
}
})
```
##
根据数据网络查询网络的能力信息及连接信息
## 根据数据网络查询网络的能力信息及连接信息
###
开发步骤
### 开发步骤
1.
从@ohos.net.connection.d.ts中导入connection命名空间。
...
...
@@ -146,89 +155,89 @@
```
js
// 引入包名
import
connection
from
'
@ohos.net.connection
'
// 调用getDefaultNet方法,获取默认的数据网络(NetHandle)
connection
.
getDefaultNet
((
err
,
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
err
));
console
.
log
(
JSON
.
stringify
(
data
));
if
(
data
)
{
this
.
netHandle
=
data
;
}
})
// 获取netHandle对应网络的能力信息。能力信息包含了网络类型、网络具体能力等网络信息
connection
.
getNetCapabilities
(
this
.
netHandle
,
(
err
,
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
err
));
// 获取网络类型(bearerTypes)
for
(
let
item
of
data
.
bearerTypes
)
{
if
(
item
==
0
)
{
// 蜂窝网
console
.
log
(
JSON
.
stringify
(
"
BEARER_CELLULAR
"
));
}
else
if
(
item
==
1
)
{
// Wi-Fi网络
console
.
log
(
JSON
.
stringify
(
"
BEARER_WIFI
"
));
}
else
if
(
item
==
3
)
{
// 以太网网络
console
.
log
(
JSON
.
stringify
(
"
BEARER_ETHERNET
"
));
}
}
// 获取网络具体能力(networkCap)
for
(
let
item
of
data
.
networkCap
)
{
if
(
item
==
0
)
{
// 表示网络可以访问运营商的MMSC(Multimedia Message Service,多媒体短信服务)发送和接收彩信
console
.
log
(
JSON
.
stringify
(
"
NET_CAPABILITY_MMS
"
));
}
else
if
(
item
==
11
)
{
// 表示网络流量未被计费
console
.
log
(
JSON
.
stringify
(
"
NET_CAPABILITY_NOT_METERED
"
));
}
else
if
(
item
==
12
)
{
// 表示该网络应具有访问Internet的能力,该能力由网络提供者设置
console
.
log
(
JSON
.
stringify
(
"
NET_CAPABILITY_INTERNET
"
));
}
else
if
(
item
==
15
)
{
// 表示网络不使用VPN(Virtual Private Network,虚拟专用网络)
console
.
log
(
JSON
.
stringify
(
"
NET_CAPABILITY_NOT_VPN
"
));
}
else
if
(
item
==
16
)
{
// 表示该网络访问Internet的能力被网络管理成功验证,该能力由网络管理模块设置
console
.
log
(
JSON
.
stringify
(
"
NET_CAPABILITY_VALIDATED
"
));
}
}
})
// 获取netHandle对应网络的连接信息。连接信息包含了链路信息、路由信息等
connection
.
getConnectionProperties
(
this
.
netHandle
,
(
err
,
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
err
));
console
.
log
(
JSON
.
stringify
(
data
));
})
// 调用getAllNets,获取所有处于连接状态的网络列表(Array<NetHandle>)
connection
.
getAllNets
((
err
,
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
err
));
console
.
log
(
JSON
.
stringify
(
data
));
if
(
data
)
{
this
.
netList
=
data
;
}
})
for
(
let
item
of
this
.
netList
)
{
// 循环获取网络列表每个netHandle对应网络的能力信息
connection
.
getNetCapabilities
(
item
,
(
err
,
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
err
));
console
.
log
(
JSON
.
stringify
(
data
));
})
// 循环获取网络列表每个netHandle对应的网络的连接信息
connection
.
getConnectionProperties
(
item
,
(
err
,
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
err
));
console
.
log
(
JSON
.
stringify
(
data
));
})
}
import
connection
from
'
@ohos.net.connection
'
// 调用getDefaultNet方法,获取默认的数据网络(NetHandle)
connection
.
getDefaultNet
((
err
,
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
err
));
console
.
log
(
JSON
.
stringify
(
data
));
if
(
data
)
{
this
.
netHandle
=
data
;
}
})
// 获取netHandle对应网络的能力信息。能力信息包含了网络类型、网络具体能力等网络信息
connection
.
getNetCapabilities
(
this
.
netHandle
,
(
err
,
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
err
));
// 获取网络类型(bearerTypes)
for
(
let
item
of
data
.
bearerTypes
)
{
if
(
item
==
0
)
{
// 蜂窝网
console
.
log
(
JSON
.
stringify
(
"
BEARER_CELLULAR
"
));
}
else
if
(
item
==
1
)
{
// Wi-Fi网络
console
.
log
(
JSON
.
stringify
(
"
BEARER_WIFI
"
));
}
else
if
(
item
==
3
)
{
// 以太网网络
console
.
log
(
JSON
.
stringify
(
"
BEARER_ETHERNET
"
));
}
}
// 获取网络具体能力(networkCap)
for
(
let
item
of
data
.
networkCap
)
{
if
(
item
==
0
)
{
// 表示网络可以访问运营商的MMSC(Multimedia Message Service,多媒体短信服务)发送和接收彩信
console
.
log
(
JSON
.
stringify
(
"
NET_CAPABILITY_MMS
"
));
}
else
if
(
item
==
11
)
{
// 表示网络流量未被计费
console
.
log
(
JSON
.
stringify
(
"
NET_CAPABILITY_NOT_METERED
"
));
}
else
if
(
item
==
12
)
{
// 表示该网络应具有访问Internet的能力,该能力由网络提供者设置
console
.
log
(
JSON
.
stringify
(
"
NET_CAPABILITY_INTERNET
"
));
}
else
if
(
item
==
15
)
{
// 表示网络不使用VPN(Virtual Private Network,虚拟专用网络)
console
.
log
(
JSON
.
stringify
(
"
NET_CAPABILITY_NOT_VPN
"
));
}
else
if
(
item
==
16
)
{
// 表示该网络访问Internet的能力被网络管理成功验证,该能力由网络管理模块设置
console
.
log
(
JSON
.
stringify
(
"
NET_CAPABILITY_VALIDATED
"
));
}
}
})
// 获取netHandle对应网络的连接信息。连接信息包含了链路信息、路由信息等
connection
.
getConnectionProperties
(
this
.
netHandle
,
(
err
,
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
err
));
console
.
log
(
JSON
.
stringify
(
data
));
})
// 调用getAllNets,获取所有处于连接状态的网络列表(Array<NetHandle>)
connection
.
getAllNets
((
err
,
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
err
));
console
.
log
(
JSON
.
stringify
(
data
));
if
(
data
)
{
this
.
netList
=
data
;
}
})
for
(
let
item
of
this
.
netList
)
{
// 循环获取网络列表每个netHandle对应网络的能力信息
connection
.
getNetCapabilities
(
item
,
(
err
,
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
err
));
console
.
log
(
JSON
.
stringify
(
data
));
})
// 循环获取网络列表每个netHandle对应的网络的连接信息
connection
.
getConnectionProperties
(
item
,
(
err
,
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
err
));
console
.
log
(
JSON
.
stringify
(
data
));
})
}
```
##
使用对应网络解析域名,获取所有IP
## 使用对应网络解析域名,获取所有IP
###
开发步骤
### 开发步骤
1.
从@ohos.net.connection.d.ts中导入connection命名空间。
...
...
@@ -236,11 +245,11 @@
```
js
// 引入包名
import
connection
from
'
@ohos.net.connection
'
import
connection
from
'
@ohos.net.connection
'
// 使用默认网络解析主机名以获取所有IP地址
connection
.
getAddressesByName
(
this
.
host
,
(
err
,
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
err
));
console
.
log
(
JSON
.
stringify
(
data
));
})
// 使用默认网络解析主机名以获取所有IP地址
connection
.
getAddressesByName
(
this
.
host
,
(
err
,
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
err
));
console
.
log
(
JSON
.
stringify
(
data
));
})
```
zh-cn/application-dev/connectivity/net-ethernet.md
浏览文件 @
732ecacf
# 以太网连接
## 简介
以太网连接的功能是提供支持设备通过硬件接口,以插入网线的形式访问互联网的能力。
设备接入网线后,可以获取动态分配的IP地址,子网掩码,Gateway,DNS等一系列网络属性;通过静态模式,手动配置与获取设备的网络属性。
以太网连接的功能是提供支持设备通过硬件接口,以插入网线的形式访问互联网的能力。
设备接入网线后,可以获取动态分配的IP地址,子网掩码,Gateway,DNS等一系列网络属性;通过静态模式,手动配置与获取设备的网络属性。
> **说明:**
> 为了保证应用的运行效率,大部分API调用都是异步的,对于异步调用的API均提供了callback和Promise两种方式,以下示例均采用callback函数,更多方式可以查阅[API参考](../reference/apis/js-apis-net-ethernet.md)。
## 约束
-
开发语言:C++ JS
-
系统:linux内核
-
本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
-
开发语言:C++ JS
-
系统:linux内核
-
本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
## 场景介绍
以太网连接的典型场景有:
-
DHCP模式,通过动态分配IP地址,子网掩码,Gateway,DNS等一系列网络属性,使能访问网络。
-
静态模式,通过静态配置IP地址,子网掩码,Gateway,DNS等一系列网络属性,使能访问网络。
-
DHCP模式,通过动态分配IP地址,子网掩码,Gateway,DNS等一系列网络属性,使能访问网络。
-
静态模式,通过静态配置IP地址,子网掩码,Gateway,DNS等一系列网络属性,使能访问网络。
以下分别介绍具体开发方式。
## 接口说明
完整的JS API说明以及实例代码请参考:
[
以太网连接
](
../reference/apis/js-apis-net-ethernet.md
)
。
| 类型 | 接口 | 功能说明 |
...
...
@@ -41,44 +45,45 @@
```
js
// 从@ohos.net.ethernet中导入ethernet命名空间
import
ethernet
from
'
@ohos.net.ethernet
'
// getAllActiveIfaces获取所有活动的网络设备名称
ethernet
.
getAllActiveIfaces
((
error
,
data
)
=>
{
if
(
error
)
{
console
.
log
(
"
getAllActiveIfaces callback error =
"
+
error
);
}
else
{
console
.
log
(
"
getAllActiveIfaces callback data.length =
"
+
data
.
length
);
for
(
let
i
=
0
;
i
<
data
.
length
;
i
++
)
{
console
.
log
(
"
getAllActiveIfaces callback =
"
+
data
[
i
]);
}
}
});
// isIfaceActive判断指定网口是否已激活
ethernet
.
isIfaceActive
(
"
eth0
"
,
(
error
,
data
)
=>
{
if
(
error
)
{
console
.
log
(
"
isIfaceActive callback error =
"
+
error
);
}
else
{
console
.
log
(
"
isIfaceActive callback =
"
+
data
);
}
});
// getIfaceConfig获取指定以太网的网络属性
ethernet
.
getIfaceConfig
(
"
eth0
"
,
(
error
,
data
)
=>
{
if
(
error
)
{
console
.
log
(
"
getIfaceConfig callback error =
"
+
error
);
}
else
{
console
.
log
(
"
getIfaceConfig callback mode =
"
+
data
.
mode
);
console
.
log
(
"
getIfaceConfig callback ipAddr =
"
+
data
.
ipAddr
);
console
.
log
(
"
getIfaceConfig callback routeAddr =
"
+
data
.
routeAddr
);
console
.
log
(
"
getIfaceConfig callback gateAddr =
"
+
data
.
gateAddr
);
console
.
log
(
"
getIfaceConfig callback maskAddr =
"
+
data
.
maskAddr
);
console
.
log
(
"
getIfaceConfig callback dns0Addr =
"
+
data
.
dns0Addr
);
console
.
log
(
"
getIfaceConfig callback dns1Addr =
"
+
data
.
dns1Addr
);
}
});
import
ethernet
from
'
@ohos.net.ethernet
'
// getAllActiveIfaces获取所有活动的网络设备名称
ethernet
.
getAllActiveIfaces
((
error
,
data
)
=>
{
if
(
error
)
{
console
.
log
(
"
getAllActiveIfaces callback error =
"
+
error
);
}
else
{
console
.
log
(
"
getAllActiveIfaces callback data.length =
"
+
data
.
length
);
for
(
let
i
=
0
;
i
<
data
.
length
;
i
++
)
{
console
.
log
(
"
getAllActiveIfaces callback =
"
+
data
[
i
]);
}
}
});
// isIfaceActive判断指定网口是否已激活
ethernet
.
isIfaceActive
(
"
eth0
"
,
(
error
,
data
)
=>
{
if
(
error
)
{
console
.
log
(
"
isIfaceActive callback error =
"
+
error
);
}
else
{
console
.
log
(
"
isIfaceActive callback =
"
+
data
);
}
});
// getIfaceConfig获取指定以太网的网络属性
ethernet
.
getIfaceConfig
(
"
eth0
"
,
(
error
,
data
)
=>
{
if
(
error
)
{
console
.
log
(
"
getIfaceConfig callback error =
"
+
error
);
}
else
{
console
.
log
(
"
getIfaceConfig callback mode =
"
+
data
.
mode
);
console
.
log
(
"
getIfaceConfig callback ipAddr =
"
+
data
.
ipAddr
);
console
.
log
(
"
getIfaceConfig callback routeAddr =
"
+
data
.
routeAddr
);
console
.
log
(
"
getIfaceConfig callback gateAddr =
"
+
data
.
gateAddr
);
console
.
log
(
"
getIfaceConfig callback maskAddr =
"
+
data
.
maskAddr
);
console
.
log
(
"
getIfaceConfig callback dns0Addr =
"
+
data
.
dns0Addr
);
console
.
log
(
"
getIfaceConfig callback dns1Addr =
"
+
data
.
dns1Addr
);
}
});
```
## 以太网连接-静态模式
### 开发步骤
...
...
@@ -92,53 +97,55 @@
```
js
// 从@ohos.net.ethernet中导入ethernet命名空间
import
ethernet
from
'
@ohos.net.ethernet
'
// getAllActiveIfaces获取所有活动的网络设备名称
ethernet
.
getAllActiveIfaces
((
error
,
data
)
=>
{
if
(
error
)
{
console
.
log
(
"
getAllActiveIfaces callback error =
"
+
error
);
}
else
{
console
.
log
(
"
getAllActiveIfaces callback data.length =
"
+
data
.
length
);
for
(
let
i
=
0
;
i
<
data
.
length
;
i
++
)
{
console
.
log
(
"
getAllActiveIfaces callback =
"
+
data
[
i
]);
}
}
});
// isIfaceActive判断指定网口是否已激活
ethernet
.
isIfaceActive
(
"
eth0
"
,
(
error
,
data
)
=>
{
if
(
error
)
{
console
.
log
(
"
isIfaceActive callback error =
"
+
error
);
}
else
{
console
.
log
(
"
isIfaceActive callback =
"
+
data
);
}
});
// setIfaceConfig配置指定以太网的网络属性
ethernet
.
setIfaceConfig
(
"
eth0
"
,
{
mode
:
ethernet
.
STATIC
,
ipAddr
:
"
192.168.xx.xx
"
,
routeAddr
:
"
192.168.xx.xx
"
,
gateAddr
:
"
192.168.xx.xx
"
,
maskAddr
:
"
255.255.xx.xx
"
,
dnsAddr0
:
"
1.1.xx.xx
"
,
dnsAddr1
:
"
2.2.xx.xx
"
},(
error
)
=>
{
if
(
error
)
{
console
.
log
(
"
setIfaceConfig callback error =
"
+
error
);
}
else
{
console
.
log
(
"
setIfaceConfig callback ok
"
);
}
});
// getIfaceConfig获取指定以太网的网络属性
ethernet
.
getIfaceConfig
(
"
eth0
"
,
(
error
,
data
)
=>
{
if
(
error
)
{
console
.
log
(
"
getIfaceConfig callback error =
"
+
error
);
}
else
{
console
.
log
(
"
getIfaceConfig callback mode =
"
+
data
.
mode
);
console
.
log
(
"
getIfaceConfig callback ipAddr =
"
+
data
.
ipAddr
);
console
.
log
(
"
getIfaceConfig callback routeAddr =
"
+
data
.
routeAddr
);
console
.
log
(
"
getIfaceConfig callback gateAddr =
"
+
data
.
gateAddr
);
console
.
log
(
"
getIfaceConfig callback maskAddr =
"
+
data
.
maskAddr
);
console
.
log
(
"
getIfaceConfig callback dns0Addr =
"
+
data
.
dns0Addr
);
console
.
log
(
"
getIfaceConfig callback dns1Addr =
"
+
data
.
dns1Addr
);
}
});
import
ethernet
from
'
@ohos.net.ethernet
'
// getAllActiveIfaces获取所有活动的网络设备名称
ethernet
.
getAllActiveIfaces
((
error
,
data
)
=>
{
if
(
error
)
{
console
.
log
(
"
getAllActiveIfaces callback error =
"
+
error
);
}
else
{
console
.
log
(
"
getAllActiveIfaces callback data.length =
"
+
data
.
length
);
for
(
let
i
=
0
;
i
<
data
.
length
;
i
++
)
{
console
.
log
(
"
getAllActiveIfaces callback =
"
+
data
[
i
]);
}
}
});
// isIfaceActive判断指定网口是否已激活
ethernet
.
isIfaceActive
(
"
eth0
"
,
(
error
,
data
)
=>
{
if
(
error
)
{
console
.
log
(
"
isIfaceActive callback error =
"
+
error
);
}
else
{
console
.
log
(
"
isIfaceActive callback =
"
+
data
);
}
});
// setIfaceConfig配置指定以太网的网络属性
ethernet
.
setIfaceConfig
(
"
eth0
"
,
{
mode
:
ethernet
.
STATIC
,
ipAddr
:
"
192.168.xx.xx
"
,
routeAddr
:
"
192.168.xx.xx
"
,
gateAddr
:
"
192.168.xx.xx
"
,
maskAddr
:
"
255.255.xx.xx
"
,
dnsAddr0
:
"
1.1.xx.xx
"
,
dnsAddr1
:
"
2.2.xx.xx
"
},
(
error
)
=>
{
if
(
error
)
{
console
.
log
(
"
setIfaceConfig callback error =
"
+
error
);
}
else
{
console
.
log
(
"
setIfaceConfig callback ok
"
);
}
});
// getIfaceConfig获取指定以太网的网络属性
ethernet
.
getIfaceConfig
(
"
eth0
"
,
(
error
,
data
)
=>
{
if
(
error
)
{
console
.
log
(
"
getIfaceConfig callback error =
"
+
error
);
}
else
{
console
.
log
(
"
getIfaceConfig callback mode =
"
+
data
.
mode
);
console
.
log
(
"
getIfaceConfig callback ipAddr =
"
+
data
.
ipAddr
);
console
.
log
(
"
getIfaceConfig callback routeAddr =
"
+
data
.
routeAddr
);
console
.
log
(
"
getIfaceConfig callback gateAddr =
"
+
data
.
gateAddr
);
console
.
log
(
"
getIfaceConfig callback maskAddr =
"
+
data
.
maskAddr
);
console
.
log
(
"
getIfaceConfig callback dns0Addr =
"
+
data
.
dns0Addr
);
console
.
log
(
"
getIfaceConfig callback dns1Addr =
"
+
data
.
dns1Addr
);
}
});
```
## 监听网络设备接口状态变化
...
...
@@ -152,13 +159,13 @@
```
js
// 从@ohos.net.ethernet中导入ethernet命名空间
import
ethernet
from
'
@ohos.net.ethernet
'
import
ethernet
from
'
@ohos.net.ethernet
'
// 订阅interfaceStateChange事件
ethernet
.
on
(
'
interfaceStateChange
'
,
((
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
data
));
}));
// 订阅interfaceStateChange事件
ethernet
.
on
(
'
interfaceStateChange
'
,
((
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
data
));
}));
// 取消事件订阅
ethernet
.
off
(
'
interfaceStateChange
'
);
// 取消事件订阅
ethernet
.
off
(
'
interfaceStateChange
'
);
```
\ No newline at end of file
zh-cn/application-dev/connectivity/net-sharing.md
浏览文件 @
732ecacf
# 网络共享
## 简介
网络共享管理分享设备已有网络给其他连接设备,支持Wi-Fi热点共享、蓝牙共享和USB共享,同时提供网络共享状态、共享流量查询功能。
> **说明:**
> 为了保证应用的运行效率,大部分API调用都是异步的,对于异步调用的API均提供了callback和Promise两种方式,以下示例均采用callback函数,更多方式可以查阅[API参考](../reference/apis/js-apis-net-sharing.md)。
## 基本概念
-
WIFI共享:通过WIFI热点共享网络。
-
蓝牙共享:通过蓝牙共享网络。
-
USB共享:通过USB共享网络。
-
WIFI共享:通过WIFI热点共享网络。
-
蓝牙共享:通过蓝牙共享网络。
-
USB共享:通过USB共享网络。
## 约束
-
开发语言:C++ JS
-
系统:linux内核
-
本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
-
开发语言:C++ JS
-
系统:linux内核
-
本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
## 场景介绍
网络共享的典型场景有:
-
开启网络共享
-
停止网络共享
-
获取共享网络的数据流量
-
开启网络共享
-
停止网络共享
-
获取共享网络的数据流量
以下分别介绍具体开发方式。
## 接口说明
完整的JS API说明以及实例代码请参考:
[
网络共享
](
../reference/apis/js-apis-net-sharing.md
)
。
| 类型 | 接口 | 功能说明 |
...
...
@@ -54,18 +61,18 @@
```
js
// 从@ohos.net.sharing中导入sharing命名空间
import
sharing
from
'
@ohos.net.sharing
'
// 注册监听共享状态的改变
sharing
.
on
(
'
sharingStateChange
'
,
(
error
,
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
error
));
console
.
log
(
JSON
.
stringify
(
data
));
});
// 调用startSharing方法,来开启指定类型共享
sharing
.
startSharing
(
sharing
.
SharingIfaceType
.
SHARING_WIFI
,
(
error
)
=>
{
console
.
log
(
JSON
.
stringify
(
error
));
});
import
sharing
from
'
@ohos.net.sharing
'
// 注册监听共享状态的改变
sharing
.
on
(
'
sharingStateChange
'
,
(
error
,
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
error
));
console
.
log
(
JSON
.
stringify
(
data
));
});
// 调用startSharing方法,来开启指定类型共享
sharing
.
startSharing
(
sharing
.
SharingIfaceType
.
SHARING_WIFI
,
(
error
)
=>
{
console
.
log
(
JSON
.
stringify
(
error
));
});
```
## 停止网络共享
...
...
@@ -79,18 +86,18 @@
```
js
// 从@ohos.net.sharing中导入sharing命名空间
import
sharing
from
'
@ohos.net.sharing
'
// 注册监听共享状态的改变
sharing
.
on
(
'
sharingStateChange
'
,
(
error
,
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
error
));
console
.
log
(
JSON
.
stringify
(
data
));
});
// 调用stopSharing方法,来停止指定类型共享
sharing
.
stopSharing
(
sharing
.
SharingIfaceType
.
SHARING_WIFI
,
(
error
)
=>
{
console
.
log
(
JSON
.
stringify
(
error
));
});
import
sharing
from
'
@ohos.net.sharing
'
// 注册监听共享状态的改变
sharing
.
on
(
'
sharingStateChange
'
,
(
error
,
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
error
));
console
.
log
(
JSON
.
stringify
(
data
));
});
// 调用stopSharing方法,来停止指定类型共享
sharing
.
stopSharing
(
sharing
.
SharingIfaceType
.
SHARING_WIFI
,
(
error
)
=>
{
console
.
log
(
JSON
.
stringify
(
error
));
});
```
## 获取共享网络的数据流量
...
...
@@ -104,27 +111,27 @@
```
js
// 从@ohos.net.sharing中导入sharing命名空间
import
sharing
from
'
@ohos.net.sharing
'
// 调用startSharing方法,来开启指定类型共享
sharing
.
startSharing
(
sharing
.
SharingIfaceType
.
SHARING_WIFI
,
(
error
)
=>
{
console
.
log
(
JSON
.
stringify
(
error
));
});
// 调用getStatsTotalBytes方法,来获取共享网络数据量
sharing
.
getStatsTotalBytes
((
error
,
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
error
));
console
.
log
(
JSON
.
stringify
(
data
));
});
// 调用stopSharing方法,来停止指定类型共享,共享网络数据量清零
sharing
.
stopSharing
(
sharing
.
SharingIfaceType
.
SHARING_WIFI
,
(
error
)
=>
{
console
.
log
(
JSON
.
stringify
(
error
));
});
// 再次调用getStatsTotalBytes方法,共享网络数据量已清零
sharing
.
getStatsTotalBytes
((
error
,
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
error
));
console
.
log
(
JSON
.
stringify
(
data
));
});
import
sharing
from
'
@ohos.net.sharing
'
// 调用startSharing方法,来开启指定类型共享
sharing
.
startSharing
(
sharing
.
SharingIfaceType
.
SHARING_WIFI
,
(
error
)
=>
{
console
.
log
(
JSON
.
stringify
(
error
));
});
// 调用getStatsTotalBytes方法,来获取共享网络数据量
sharing
.
getStatsTotalBytes
((
error
,
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
error
));
console
.
log
(
JSON
.
stringify
(
data
));
});
// 调用stopSharing方法,来停止指定类型共享,共享网络数据量清零
sharing
.
stopSharing
(
sharing
.
SharingIfaceType
.
SHARING_WIFI
,
(
error
)
=>
{
console
.
log
(
JSON
.
stringify
(
error
));
});
// 再次调用getStatsTotalBytes方法,共享网络数据量已清零
sharing
.
getStatsTotalBytes
((
error
,
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
error
));
console
.
log
(
JSON
.
stringify
(
data
));
});
```
zh-cn/application-dev/connectivity/socket-connection.md
浏览文件 @
732ecacf
...
...
@@ -186,143 +186,144 @@ UDP与TCP流程大体类似,下面以TCP为例:
```
js
import
socket
from
'
@ohos.net.socket
'
// 创建一个(双向认证)TLS Socket连接,返回一个TLS Socket对象。
let
tlsTwoWay
=
socket
.
constructTLSSocketInstance
();
// 订阅TLS Socket相关的订阅事件
tlsTwoWay
.
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
)
});
tlsTwoWay
.
on
(
'
connect
'
,
()
=>
{
console
.
log
(
"
on connect
"
)
});
tlsTwoWay
.
on
(
'
close
'
,
()
=>
{
console
.
log
(
"
on close
"
)
});
// 绑定本地IP地址和端口。
tlsTwoWay
.
bind
({
address
:
'
192.168.xxx.xxx
'
,
port
:
xxxx
,
family
:
1
},
err
=>
{
if
(
err
)
{
console
.
log
(
'
bind fail
'
);
return
;
}
console
.
log
(
'
bind success
'
);
});
// 设置通信过程中使用参数
let
options
=
{
ALPNProtocols
:
[
"
spdy/1
"
,
"
http/1.1
"
],
// 连接到指定的IP地址和端口。
address
:
{
address
:
"
192.168.xx.xxx
"
,
port
:
xxxx
,
// 端口
family
:
1
,
},
// 设置用于通信过程中完成校验的参数。
secureOptions
:
{
key
:
"
xxxx
"
,
// 密钥
cert
:
"
xxxx
"
,
// 数字证书
ca
:
[
"
xxxx
"
],
// CA证书
passwd
:
"
xxxx
"
,
// 生成密钥时的密码
protocols
:
[
socket
.
Protocol
.
TLSv12
],
// 通信协议
useRemoteCipherPrefer
:
true
,
// 是否优先使用对端密码套件
signatureAlgorithms
:
"
rsa_pss_rsae_sha256:ECDSA+SHA256
"
,
// 签名算法
cipherSuite
:
"
AES256-SHA256
"
,
// 密码套件
},
};
// 建立连接
tlsTwoWay
.
connect
(
options
,
(
err
,
data
)
=>
{
console
.
error
(
err
);
console
.
log
(
data
);
});
// 连接使用完毕后,主动关闭。取消相关事件的订阅。
tlsTwoWay
.
close
((
err
)
=>
{
if
(
err
)
{
console
.
log
(
"
close callback error =
"
+
err
);
}
else
{
console
.
log
(
"
close success
"
);
}
tlsTwoWay
.
off
(
'
message
'
);
tlsTwoWay
.
off
(
'
connect
'
);
tlsTwoWay
.
off
(
'
close
'
);
});
// 创建一个(单向认证)TLS Socket连接,返回一个TLS Socket对象。
let
tlsOneWay
=
socket
.
constructTLSSocketInstance
();
// One way authentication
// 订阅TLS Socket相关的订阅事件
tlsTwoWay
.
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
)
});
tlsTwoWay
.
on
(
'
connect
'
,
()
=>
{
console
.
log
(
"
on connect
"
)
});
tlsTwoWay
.
on
(
'
close
'
,
()
=>
{
console
.
log
(
"
on close
"
)
});
// 绑定本地IP地址和端口。
tlsOneWay
.
bind
({
address
:
'
192.168.xxx.xxx
'
,
port
:
xxxx
,
family
:
1
},
err
=>
{
if
(
err
)
{
console
.
log
(
'
bind fail
'
);
return
;
}
console
.
log
(
'
bind success
'
);
});
// 设置通信过程中使用参数
let
oneWayOptions
=
{
address
:
{
address
:
"
192.168.xxx.xxx
"
,
port
:
xxxx
,
family
:
1
,
},
secureOptions
:
{
ca
:
[
"
xxxx
"
,
"
xxxx
"
],
// CA证书
cipherSuite
:
"
AES256-SHA256
"
,
// 密码套件
},
};
// 建立连接
tlsOneWay
.
connect
(
oneWayOptions
,
(
err
,
data
)
=>
{
console
.
error
(
err
);
console
.
log
(
data
);
});
// 连接使用完毕后,主动关闭。取消相关事件的订阅。
tlsTwoWay
.
close
((
err
)
=>
{
if
(
err
)
{
console
.
log
(
"
close callback error =
"
+
err
);
}
else
{
console
.
log
(
"
close success
"
);
}
tlsTwoWay
.
off
(
'
message
'
);
tlsTwoWay
.
off
(
'
connect
'
);
tlsTwoWay
.
off
(
'
close
'
);
});
// 创建一个(双向认证)TLS Socket连接,返回一个TLS Socket对象。
let
tlsTwoWay
=
socket
.
constructTLSSocketInstance
();
// 订阅TLS Socket相关的订阅事件
tlsTwoWay
.
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
)
});
tlsTwoWay
.
on
(
'
connect
'
,
()
=>
{
console
.
log
(
"
on connect
"
)
});
tlsTwoWay
.
on
(
'
close
'
,
()
=>
{
console
.
log
(
"
on close
"
)
});
// 绑定本地IP地址和端口。
tlsTwoWay
.
bind
({
address
:
'
192.168.xxx.xxx
'
,
port
:
xxxx
,
family
:
1
},
err
=>
{
if
(
err
)
{
console
.
log
(
'
bind fail
'
);
return
;
}
console
.
log
(
'
bind success
'
);
});
// 设置通信过程中使用参数
let
options
=
{
ALPNProtocols
:
[
"
spdy/1
"
,
"
http/1.1
"
],
// 连接到指定的IP地址和端口。
address
:
{
address
:
"
192.168.xx.xxx
"
,
port
:
xxxx
,
// 端口
family
:
1
,
},
// 设置用于通信过程中完成校验的参数。
secureOptions
:
{
key
:
"
xxxx
"
,
// 密钥
cert
:
"
xxxx
"
,
// 数字证书
ca
:
[
"
xxxx
"
],
// CA证书
passwd
:
"
xxxx
"
,
// 生成密钥时的密码
protocols
:
[
socket
.
Protocol
.
TLSv12
],
// 通信协议
useRemoteCipherPrefer
:
true
,
// 是否优先使用对端密码套件
signatureAlgorithms
:
"
rsa_pss_rsae_sha256:ECDSA+SHA256
"
,
// 签名算法
cipherSuite
:
"
AES256-SHA256
"
,
// 密码套件
},
};
// 建立连接
tlsTwoWay
.
connect
(
options
,
(
err
,
data
)
=>
{
console
.
error
(
err
);
console
.
log
(
data
);
});
// 连接使用完毕后,主动关闭。取消相关事件的订阅。
tlsTwoWay
.
close
((
err
)
=>
{
if
(
err
)
{
console
.
log
(
"
close callback error =
"
+
err
);
}
else
{
console
.
log
(
"
close success
"
);
}
tlsTwoWay
.
off
(
'
message
'
);
tlsTwoWay
.
off
(
'
connect
'
);
tlsTwoWay
.
off
(
'
close
'
);
});
// 创建一个(单向认证)TLS Socket连接,返回一个TLS Socket对象。
let
tlsOneWay
=
socket
.
constructTLSSocketInstance
();
// One way authentication
// 订阅TLS Socket相关的订阅事件
tlsTwoWay
.
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
)
});
tlsTwoWay
.
on
(
'
connect
'
,
()
=>
{
console
.
log
(
"
on connect
"
)
});
tlsTwoWay
.
on
(
'
close
'
,
()
=>
{
console
.
log
(
"
on close
"
)
});
// 绑定本地IP地址和端口。
tlsOneWay
.
bind
({
address
:
'
192.168.xxx.xxx
'
,
port
:
xxxx
,
family
:
1
},
err
=>
{
if
(
err
)
{
console
.
log
(
'
bind fail
'
);
return
;
}
console
.
log
(
'
bind success
'
);
});
// 设置通信过程中使用参数
let
oneWayOptions
=
{
address
:
{
address
:
"
192.168.xxx.xxx
"
,
port
:
xxxx
,
family
:
1
,
},
secureOptions
:
{
ca
:
[
"
xxxx
"
,
"
xxxx
"
],
// CA证书
cipherSuite
:
"
AES256-SHA256
"
,
// 密码套件
},
};
// 建立连接
tlsOneWay
.
connect
(
oneWayOptions
,
(
err
,
data
)
=>
{
console
.
error
(
err
);
console
.
log
(
data
);
});
// 连接使用完毕后,主动关闭。取消相关事件的订阅。
tlsTwoWay
.
close
((
err
)
=>
{
if
(
err
)
{
console
.
log
(
"
close callback error =
"
+
err
);
}
else
{
console
.
log
(
"
close success
"
);
}
tlsTwoWay
.
off
(
'
message
'
);
tlsTwoWay
.
off
(
'
connect
'
);
tlsTwoWay
.
off
(
'
close
'
);
});
```
## 相关实例
针对Socket连接开发,有以下相关实例可供参考:
-
[
`Socket`:Socket 连接(ArkTS)(API9)
](
https://gitee.com/openharmony/applications_app_samples/tree/master/Network/Socket
)
-
[
使用UDP实现与服务端通信(ArkTS)(API9)
](
https://gitee.com/openharmony/codelabs/tree/master/NetworkManagement/UdpDemoOH
)
-
[
使用TCP实现与服务端通信(ArkTS)(API9)
](
https://gitee.com/openharmony/codelabs/tree/master/NetworkManagement/TcpSocketDemo
)
zh-cn/application-dev/connectivity/websocket-connection.md
浏览文件 @
732ecacf
# WebSocket连接
## 场景介绍
使用WebSocket建立服务器与客户端的双向连接,需要先通过createWebSocket()方法创建WebSocket对象,然后通过connect()方法连接到服务器。当连接成功后,客户端会收到open事件的回调,之后客户端就可以通过send()方法与服务器进行通信。当服务器发信息给客户端时,客户端会收到message事件的回调。当客户端不要此连接时,可以通过调用close()方法主动断开连接,之后客户端会收到close事件的回调。
若在上述任一过程中发生错误,客户端会收到error事件的回调。
## 接口说明
WebSocket连接功能主要由webSocket模块提供。使用该功能需要申请ohos.permission.INTERNET权限。具体接口说明如下表。
...
...
@@ -27,7 +25,6 @@ WebSocket连接功能主要由webSocket模块提供。使用该功能需要申
| on(type: 'error') | 订阅WebSocket的Error事件。 |
| off(type: 'error') | 取消订阅WebSocket的Error事件。 |
## 开发步骤
1.
导入需要的webSocket模块。
...
...
@@ -39,7 +36,7 @@ WebSocket连接功能主要由webSocket模块提供。使用该功能需要申
4.
根据URL地址,发起WebSocket连接。
5.
使用完WebSocket连接之后,主动断开连接。
```
js
import
webSocket
from
'
@ohos.net.webSocket
'
;
...
...
@@ -87,4 +84,5 @@ WebSocket连接功能主要由webSocket模块提供。使用该功能需要申
## 相关实例
针对WebSocket连接的开发,有以下相关实例可供参考:
-
[
`WebSocket`:WebSocket(ArkTS)(API9)
](
https://gitee.com/openharmony/applications_app_samples/tree/master/Network/WebSocket
)
\ No newline at end of file
zh-cn/application-dev/reference/apis/js-apis-http.md
浏览文件 @
732ecacf
...
...
@@ -2,7 +2,7 @@
本模块提供HTTP数据请求能力。应用可以通过HTTP发起一个数据请求,支持常见的GET、POST、OPTIONS、HEAD、PUT、DELETE、TRACE、CONNECT方法。
>**说明:**
>
**说明:**
>
>本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
>
...
...
@@ -24,44 +24,44 @@ let httpRequest = http.createHttp();
// 用于订阅HTTP响应头,此接口会比request请求先返回。可以根据业务需要订阅此消息
// 从API 8开始,使用on('headersReceive', Callback)替代on('headerReceive', AsyncCallback)。 8+
httpRequest
.
on
(
'
headersReceive
'
,
(
header
)
=>
{
console
.
info
(
'
header:
'
+
JSON
.
stringify
(
header
));
console
.
info
(
'
header:
'
+
JSON
.
stringify
(
header
));
});
httpRequest
.
request
(
// 填写HTTP请求的URL地址,可以带参数也可以不带参数。URL地址需要开发者自定义。请求的参数可以在extraData中指定
"
EXAMPLE_URL
"
,
{
method
:
http
.
RequestMethod
.
POST
,
// 可选,默认为http.RequestMethod.GET
// 开发者根据自身业务需要添加header字段
header
:
{
'
Content-Type
'
:
'
application/json
'
},
// 当使用POST请求时此字段用于传递内容
extraData
:
{
"
data
"
:
"
data to send
"
,
},
expectDataType
:
http
.
HttpDataType
.
STRING
,
// 可选,指定返回数据的类型
usingCache
:
true
,
// 可选,默认为true
priority
:
1
,
// 可选,默认为1
connectTimeout
:
60000
,
// 可选,默认为60000ms
readTimeout
:
60000
,
// 可选,默认为60000ms
usingProtocol
:
http
.
HttpProtocol
.
HTTP1_1
,
// 可选,协议类型默认值由系统自动指定
usingProxy
:
false
,
//可选,默认不使用网络代理,自API 10开始支持该属性
},
(
err
,
data
)
=>
{
if
(
!
err
)
{
// data.result为HTTP响应内容,可根据业务需要进行解析
console
.
info
(
'
Result:
'
+
JSON
.
stringify
(
data
.
result
));
console
.
info
(
'
code:
'
+
JSON
.
stringify
(
data
.
responseCode
));
// data.header为HTTP响应头,可根据业务需要进行解析
console
.
info
(
'
header:
'
+
JSON
.
stringify
(
data
.
header
));
console
.
info
(
'
cookies:
'
+
JSON
.
stringify
(
data
.
cookies
));
// 8+
}
else
{
console
.
info
(
'
error:
'
+
JSON
.
stringify
(
err
));
// 取消订阅HTTP响应头事件
httpRequest
.
off
(
'
headersReceive
'
);
// 当该请求使用完毕时,调用destroy方法主动销毁。
httpRequest
.
destroy
();
}
// 填写HTTP请求的URL地址,可以带参数也可以不带参数。URL地址需要开发者自定义。请求的参数可以在extraData中指定
"
EXAMPLE_URL
"
,
{
method
:
http
.
RequestMethod
.
POST
,
// 可选,默认为http.RequestMethod.GET
// 开发者根据自身业务需要添加header字段
header
:
{
'
Content-Type
'
:
'
application/json
'
},
// 当使用POST请求时此字段用于传递内容
extraData
:
{
"
data
"
:
"
data to send
"
,
},
expectDataType
:
http
.
HttpDataType
.
STRING
,
// 可选,指定返回数据的类型
usingCache
:
true
,
// 可选,默认为true
priority
:
1
,
// 可选,默认为1
connectTimeout
:
60000
,
// 可选,默认为60000ms
readTimeout
:
60000
,
// 可选,默认为60000ms
usingProtocol
:
http
.
HttpProtocol
.
HTTP1_1
,
// 可选,协议类型默认值由系统自动指定
usingProxy
:
false
,
//可选,默认不使用网络代理,自API 10开始支持该属性
},
(
err
,
data
)
=>
{
if
(
!
err
)
{
// data.result为HTTP响应内容,可根据业务需要进行解析
console
.
info
(
'
Result:
'
+
JSON
.
stringify
(
data
.
result
));
console
.
info
(
'
code:
'
+
JSON
.
stringify
(
data
.
responseCode
));
// data.header为HTTP响应头,可根据业务需要进行解析
console
.
info
(
'
header:
'
+
JSON
.
stringify
(
data
.
header
));
console
.
info
(
'
cookies:
'
+
JSON
.
stringify
(
data
.
cookies
));
// 8+
}
else
{
console
.
info
(
'
error:
'
+
JSON
.
stringify
(
err
));
// 取消订阅HTTP响应头事件
httpRequest
.
off
(
'
headersReceive
'
);
// 当该请求使用完毕时,调用destroy方法主动销毁。
httpRequest
.
destroy
();
}
}
);
```
...
...
@@ -83,6 +83,7 @@ createHttp(): HttpRequest
```
js
import
http
from
'
@ohos.net.http
'
;
let
httpRequest
=
http
.
createHttp
();
```
...
...
@@ -96,8 +97,8 @@ request(url: string, callback: AsyncCallback\<HttpResponse\>):void
根据URL地址,发起HTTP网络请求,使用callback方式作为异步方法。
>**说明:**
>此接口仅支持数据大小为5M以内的数据传输。
>
**说明:**
>
此接口仅支持数据大小为5M以内的数据传输。
**需要权限**
:ohos.permission.INTERNET
...
...
@@ -122,7 +123,7 @@ request(url: string, callback: AsyncCallback\<HttpResponse\>):void
| 2300052 | Server returned nothing (no headers, no data). |
| 2300999 | Unknown Other Error. |
>**错误码说明:**
>
**错误码说明:**
> 以上错误码的详细介绍参见[HTTP错误码](../errorcodes/errorcode-net-http.md)。
> HTTP 错误码映射关系:2300000 + curl错误码。更多常用错误码,可参考:[curl错误码](https://curl.se/libcurl/c/libcurl-errors.html)
...
...
@@ -130,14 +131,14 @@ request(url: string, callback: AsyncCallback\<HttpResponse\>):void
```
js
httpRequest
.
request
(
"
EXAMPLE_URL
"
,
(
err
,
data
)
=>
{
if
(
!
err
)
{
console
.
info
(
'
Result:
'
+
data
.
result
);
console
.
info
(
'
code:
'
+
data
.
responseCode
);
console
.
info
(
'
header:
'
+
JSON
.
stringify
(
data
.
header
));
console
.
info
(
'
cookies:
'
+
data
.
cookies
);
// 8+
}
else
{
console
.
info
(
'
error:
'
+
JSON
.
stringify
(
err
));
}
if
(
!
err
)
{
console
.
info
(
'
Result:
'
+
data
.
result
);
console
.
info
(
'
code:
'
+
data
.
responseCode
);
console
.
info
(
'
header:
'
+
JSON
.
stringify
(
data
.
header
));
console
.
info
(
'
cookies:
'
+
data
.
cookies
);
// 8+
}
else
{
console
.
info
(
'
error:
'
+
JSON
.
stringify
(
err
));
}
});
```
...
...
@@ -147,8 +148,8 @@ request(url: string, options: HttpRequestOptions, callback: AsyncCallback\<HttpR
根据URL地址和相关配置项,发起HTTP网络请求,使用callback方式作为异步方法。
>**说明:**
>此接口仅支持数据大小为5M以内的数据传输。
>
**说明:**
>
此接口仅支持数据大小为5M以内的数据传输。
**需要权限**
:ohos.permission.INTERNET
...
...
@@ -198,7 +199,7 @@ request(url: string, options: HttpRequestOptions, callback: AsyncCallback\<HttpR
| 2300094 | An authentication function returned an error. |
| 2300999 | Unknown Other Error. |
>**错误码说明:**
>
**错误码说明:**
> 以上错误码的详细介绍参见[HTTP错误码](../errorcodes/errorcode-net-http.md)。
> HTTP 错误码映射关系:2300000 + curl错误码。更多常用错误码,可参考:[curl错误码](https://curl.se/libcurl/c/libcurl-errors.html)
...
...
@@ -206,25 +207,25 @@ request(url: string, options: HttpRequestOptions, callback: AsyncCallback\<HttpR
```
js
httpRequest
.
request
(
"
EXAMPLE_URL
"
,
{
{
method
:
http
.
RequestMethod
.
GET
,
header
:
{
'
Content-Type
'
:
'
application/json
'
'
Content-Type
'
:
'
application/json
'
},
readTimeout
:
60000
,
connectTimeout
:
60000
},
(
err
,
data
)
=>
{
},
(
err
,
data
)
=>
{
if
(
!
err
)
{
console
.
info
(
'
Result:
'
+
data
.
result
);
console
.
info
(
'
code:
'
+
data
.
responseCode
);
console
.
info
(
'
header:
'
+
JSON
.
stringify
(
data
.
header
));
console
.
info
(
'
cookies:
'
+
data
.
cookies
);
// 8+
console
.
info
(
'
header.Content-Type:
'
+
data
.
header
[
'
Content-Type
'
]);
console
.
info
(
'
header.Status-Line:
'
+
data
.
header
[
'
Status-Line
'
]);
console
.
info
(
'
Result:
'
+
data
.
result
);
console
.
info
(
'
code:
'
+
data
.
responseCode
);
console
.
info
(
'
header:
'
+
JSON
.
stringify
(
data
.
header
));
console
.
info
(
'
cookies:
'
+
data
.
cookies
);
// 8+
console
.
info
(
'
header.Content-Type:
'
+
data
.
header
[
'
Content-Type
'
]);
console
.
info
(
'
header.Status-Line:
'
+
data
.
header
[
'
Status-Line
'
]);
}
else
{
console
.
info
(
'
error:
'
+
JSON
.
stringify
(
err
));
console
.
info
(
'
error:
'
+
JSON
.
stringify
(
err
));
}
});
});
```
### request
...
...
@@ -233,8 +234,8 @@ request(url: string, options? : HttpRequestOptions): Promise\<HttpResponse\>
根据URL地址,发起HTTP网络请求,使用Promise方式作为异步方法。
>**说明:**
>此接口仅支持数据大小为5M以内的数据传输。
>
**说明:**
>
此接口仅支持数据大小为5M以内的数据传输。
**需要权限**
:ohos.permission.INTERNET
...
...
@@ -289,30 +290,30 @@ request(url: string, options? : HttpRequestOptions): Promise\<HttpResponse\>
| 2300094 | An authentication function returned an error. |
| 2300999 | Unknown Other Error. |
>**错误码说明:**
>
**错误码说明:**
> 以上错误码的详细介绍参见[HTTP错误码](../errorcodes/errorcode-net-http.md)。
> HTTP 错误码映射关系:2300000 + curl错误码。更多常用错误码,可参考:[curl错误码](https://curl.se/libcurl/c/libcurl-errors.html)
> HTTP 错误码映射关系:2300000 + curl错误码。更多常用错误码,可参考:[curl错误码](https://curl.se/libcurl/c/libcurl-errors.html)
**示例:**
```
js
let
promise
=
httpRequest
.
request
(
"
EXAMPLE_URL
"
,
{
method
:
http
.
RequestMethod
.
GET
,
connectTimeout
:
60000
,
readTimeout
:
60000
,
header
:
{
'
Content-Type
'
:
'
application/json
'
}
method
:
http
.
RequestMethod
.
GET
,
connectTimeout
:
60000
,
readTimeout
:
60000
,
header
:
{
'
Content-Type
'
:
'
application/json
'
}
});
promise
.
then
((
data
)
=>
{
console
.
info
(
'
Result:
'
+
data
.
result
);
console
.
info
(
'
code:
'
+
data
.
responseCode
);
console
.
info
(
'
header:
'
+
JSON
.
stringify
(
data
.
header
));
console
.
info
(
'
cookies:
'
+
data
.
cookies
);
// 8+
console
.
info
(
'
header.Content-Type:
'
+
data
.
header
[
'
Content-Type
'
]);
console
.
info
(
'
header.Status-Line:
'
+
data
.
header
[
'
Status-Line
'
]);
console
.
info
(
'
Result:
'
+
data
.
result
);
console
.
info
(
'
code:
'
+
data
.
responseCode
);
console
.
info
(
'
header:
'
+
JSON
.
stringify
(
data
.
header
));
console
.
info
(
'
cookies:
'
+
data
.
cookies
);
// 8+
console
.
info
(
'
header.Content-Type:
'
+
data
.
header
[
'
Content-Type
'
]);
console
.
info
(
'
header.Status-Line:
'
+
data
.
header
[
'
Status-Line
'
]);
}).
catch
((
err
)
=>
{
console
.
info
(
'
error:
'
+
JSON
.
stringify
(
err
));
console
.
info
(
'
error:
'
+
JSON
.
stringify
(
err
));
});
```
...
...
@@ -359,7 +360,7 @@ request2(url: string, callback: AsyncCallback\<number\>): void
| 2300052 | Server returned nothing (no headers, no data). |
| 2300999 | Unknown Other Error. |
>**错误码说明:**
>
**错误码说明:**
> 以上错误码的详细介绍参见[HTTP错误码](../errorcodes/errorcode-net-http.md)。
> HTTP 错误码映射关系:2300000 + curl错误码。更多常用错误码,可参考:[curl错误码](https://curl.se/libcurl/c/libcurl-errors.html)
...
...
@@ -367,11 +368,11 @@ request2(url: string, callback: AsyncCallback\<number\>): void
```
js
httpRequest
.
request2
(
"
EXAMPLE_URL
"
,
(
err
,
data
)
=>
{
if
(
!
err
)
{
console
.
info
(
"
request2 OK! ResponseCode is
"
+
JSON
.
stringify
(
data
));
}
else
{
console
.
info
(
"
request2 ERROR : err =
"
+
JSON
.
stringify
(
err
));
}
if
(
!
err
)
{
console
.
info
(
"
request2 OK! ResponseCode is
"
+
JSON
.
stringify
(
data
));
}
else
{
console
.
info
(
"
request2 ERROR : err =
"
+
JSON
.
stringify
(
err
));
}
})
```
...
...
@@ -429,7 +430,7 @@ request2(url: string, options: HttpRequestOptions, callback: AsyncCallback\<numb
| 2300094 | An authentication function returned an error. |
| 2300999 | Unknown Other Error. |
>**错误码说明:**
>
**错误码说明:**
> 以上错误码的详细介绍参见[HTTP错误码](../errorcodes/errorcode-net-http.md)。
> HTTP 错误码映射关系:2300000 + curl错误码。更多常用错误码,可参考:[curl错误码](https://curl.se/libcurl/c/libcurl-errors.html)
...
...
@@ -437,21 +438,22 @@ request2(url: string, options: HttpRequestOptions, callback: AsyncCallback\<numb
```
js
httpRequest
.
request2
(
"
EXAMPLE_URL
"
,
{
{
method
:
http
.
RequestMethod
.
GET
,
header
:
{
'
Content-Type
'
:
'
application/json
'
'
Content-Type
'
:
'
application/json
'
},
readTimeout
:
60000
,
connectTimeout
:
60000
},
(
err
,
data
)
=>
{
},
(
err
,
data
)
=>
{
if
(
!
err
)
{
console
.
info
(
"
request2 OK! ResponseCode is
"
+
JSON
.
stringify
(
data
));
console
.
info
(
"
request2 OK! ResponseCode is
"
+
JSON
.
stringify
(
data
));
}
else
{
console
.
info
(
"
request2 ERROR : err =
"
+
JSON
.
stringify
(
err
));
console
.
info
(
"
request2 ERROR : err =
"
+
JSON
.
stringify
(
err
));
}
})
})
```
### request2<sup>10+</sup>
request2(url: string, options? : HttpRequestOptions): Promise
\<
number
\>
...
...
@@ -511,7 +513,7 @@ request2(url: string, options? : HttpRequestOptions): Promise\<number\>
| 2300094 | An authentication function returned an error. |
| 2300999 | Unknown Other Error. |
>**错误码说明:**
>
**错误码说明:**
> 以上错误码的详细介绍参见[HTTP错误码](../errorcodes/errorcode-net-http.md)。
> HTTP 错误码映射关系:2300000 + curl错误码。更多常用错误码,可参考:
...
...
@@ -519,17 +521,17 @@ request2(url: string, options? : HttpRequestOptions): Promise\<number\>
```
js
let
promise
=
httpRequest
.
request2
(
"
EXAMPLE_URL
"
,
{
method
:
http
.
RequestMethod
.
GET
,
connectTimeout
:
60000
,
readTimeout
:
60000
,
header
:
{
'
Content-Type
'
:
'
application/json
'
}
method
:
http
.
RequestMethod
.
GET
,
connectTimeout
:
60000
,
readTimeout
:
60000
,
header
:
{
'
Content-Type
'
:
'
application/json
'
}
});
promise
.
then
((
data
)
=>
{
console
.
info
(
"
request2 OK!
"
+
JSON
.
stringify
(
data
));
console
.
info
(
"
request2 OK!
"
+
JSON
.
stringify
(
data
));
}).
catch
((
err
)
=>
{
console
.
info
(
"
request2 ERROR : err =
"
+
JSON
.
stringify
(
err
));
console
.
info
(
"
request2 ERROR : err =
"
+
JSON
.
stringify
(
err
));
});
```
...
...
@@ -539,8 +541,8 @@ on(type: 'headerReceive', callback: AsyncCallback\<Object\>): void
订阅HTTP Response Header 事件。
>**说明:**
>此接口已废弃,建议使用[on('headersReceive')<sup>8+</sup>](#onheadersreceive8)替代。
>
**说明:**
>
此接口已废弃,建议使用[on('headersReceive')<sup>8+</sup>](#onheadersreceive8)替代。
**系统能力**
:SystemCapability.Communication.NetStack
...
...
@@ -555,7 +557,7 @@ on(type: 'headerReceive', callback: AsyncCallback\<Object\>): void
```
js
httpRequest
.
on
(
'
headerReceive
'
,
(
data
)
=>
{
console
.
info
(
'
error:
'
+
JSON
.
stringify
(
data
));
console
.
info
(
'
error:
'
+
JSON
.
stringify
(
data
));
});
```
...
...
@@ -565,7 +567,7 @@ off(type: 'headerReceive', callback?: AsyncCallback\<Object\>): void
取消订阅HTTP Response Header 事件。
>**说明:**
>
**说明:**
>
>1. 此接口已废弃,建议使用[off('headersReceive')<sup>8+</sup>](#offheadersreceive8)替代。
>
...
...
@@ -605,7 +607,7 @@ on(type: 'headersReceive', callback: Callback\<Object\>): void
```
js
httpRequest
.
on
(
'
headersReceive
'
,
(
header
)
=>
{
console
.
info
(
'
header:
'
+
JSON
.
stringify
(
header
));
console
.
info
(
'
header:
'
+
JSON
.
stringify
(
header
));
});
```
...
...
@@ -615,8 +617,8 @@ off(type: 'headersReceive', callback?: Callback\<Object\>): void
取消订阅HTTP Response Header 事件。
>**说明:**
>可以指定传入on中的callback取消一个订阅,也可以不指定callback清空所有订阅。
>
**说明:**
>
可以指定传入on中的callback取消一个订阅,也可以不指定callback清空所有订阅。
**系统能力**
:SystemCapability.Communication.NetStack
...
...
@@ -652,9 +654,10 @@ once(type: 'headersReceive', callback: Callback\<Object\>): void
```
js
httpRequest
.
once
(
'
headersReceive
'
,
(
header
)
=>
{
console
.
info
(
'
header:
'
+
JSON
.
stringify
(
header
));
console
.
info
(
'
header:
'
+
JSON
.
stringify
(
header
));
});
```
### on('dataReceive')<sup>10+</sup>
on(type: 'dataReceive', callback: Callback
\<
ArrayBuffer
\>
): void
...
...
@@ -674,7 +677,7 @@ on(type: 'dataReceive', callback: Callback\<ArrayBuffer\>): void
```
js
httpRequest
.
on
(
'
dataReceive
'
,
(
data
)
=>
{
console
.
info
(
'
dataReceive length:
'
+
JSON
.
stringify
(
data
.
byteLength
));
console
.
info
(
'
dataReceive length:
'
+
JSON
.
stringify
(
data
.
byteLength
));
});
```
...
...
@@ -684,8 +687,8 @@ off(type: 'dataReceive', callback?: Callback\<ArrayBuffer\>): void
取消订阅HTTP流式响应数据接收事件。
>**说明:**
>可以指定传入on中的callback取消一个订阅,也可以不指定callback清空所有订阅。
>
**说明:**
>
可以指定传入on中的callback取消一个订阅,也可以不指定callback清空所有订阅。
**系统能力**
:SystemCapability.Communication.NetStack
...
...
@@ -721,7 +724,7 @@ on(type: 'dataEnd', callback: Callback\<void\>): void
```
js
httpRequest
.
on
(
'
dataEnd
'
,
()
=>
{
console
.
info
(
'
Receive dataEnd !
'
);
console
.
info
(
'
Receive dataEnd !
'
);
});
```
...
...
@@ -731,8 +734,8 @@ off(type: 'dataEnd', callback?: Callback\<void\>): void
取消订阅HTTP流式响应数据接收完毕事件。
>**说明:**
>可以指定传入on中的callback取消一个订阅,也可以不指定callback清空所有订阅。
>
**说明:**
>
可以指定传入on中的callback取消一个订阅,也可以不指定callback清空所有订阅。
**系统能力**
:SystemCapability.Communication.NetStack
...
...
@@ -751,7 +754,7 @@ httpRequest.off('dataEnd');
### on('dataProgress')<sup>10+</sup>
on(type: 'dataProgress', callback: AsyncCallback
\<
{ receiveSize: number, totalSize: number }
\>
): void
on(type: 'dataProgress', callback: AsyncCallback
\<
{ receiveSize: number, totalSize: number }
\>
): void
订阅HTTP流式响应数据接收进度事件。
...
...
@@ -768,7 +771,7 @@ httpRequest.off('dataEnd');
```
js
httpRequest
.
on
(
'
dataProgress
'
,
(
data
)
=>
{
console
.
info
(
'
dataProgress:
'
+
JSON
.
stringify
(
data
));
console
.
info
(
'
dataProgress:
'
+
JSON
.
stringify
(
data
));
});
```
...
...
@@ -778,8 +781,8 @@ off(type: 'dataProgress', callback?: Callback\<{ receiveSize: number, totalSize:
取消订阅HTTP流式响应数据接收进度事件。
>**说明:**
>可以指定传入on中的callback取消一个订阅,也可以不指定callback清空所有订阅。
>
**说明:**
>
可以指定传入on中的callback取消一个订阅,也可以不指定callback清空所有订阅。
**系统能力**
:SystemCapability.Communication.NetStack
...
...
@@ -795,6 +798,7 @@ off(type: 'dataProgress', callback?: Callback\<{ receiveSize: number, totalSize:
```
js
httpRequest
.
off
(
'
dataProgress
'
);
```
## HttpRequestOptions
发起请求可选参数的类型和取值范围。
...
...
@@ -804,7 +808,7 @@ httpRequest.off('dataProgress');
| 名称 | 类型 | 必填 | 说明 |
| -------------- | --------------------------------------------- | ---- | ------------------------------------------------------------ |
| method |
[
RequestMethod
](
#requestmethod
)
| 否 | 请求方式,默认为GET。 |
| extraData | string
\|
Object
\|
ArrayBuffer
<sup>
6+
</sup>
| 否 | 发送请求的额外数据,默认无此字段。
<br
/>
- 当HTTP请求为POST、PUT等方法时,此字段为HTTP请求的content,以UTF-8编码形式作为请求体。
<br
/>
- 当HTTP请求为GET、OPTIONS、DELETE、TRACE、CONNECT等方法时,此字段为HTTP请求参数的补充。开发者需传入Encode编码后的string类型参数,Object类型的参数无需预编码,参数内容会拼接到URL中进行发送;ArrayBuffer类型的参数不会做拼接处理。
<sup>
6+
</sup>
|
| extraData | string
\|
Object
\|
ArrayBuffer
<sup>
6+
</sup>
| 否 | 发送请求的额外数据,默认无此字段。
<br
/>
- 当HTTP请求为POST、PUT等方法时,此字段为HTTP请求的content,以UTF-8编码形式作为请求体。
<sup>
6+
</sup>
<br
/>
- 当HTTP请求为GET、OPTIONS、DELETE、TRACE、CONNECT等方法时,此字段为HTTP请求参数的补充。开发者需传入Encode编码后的string类型参数,Object类型的参数无需预编码,参数内容会拼接到URL中进行发送;ArrayBuffer类型的参数不会做拼接处理。
<sup>
6+
</sup>
|
| expectDataType
<sup>
9+
</sup>
|
[
HttpDataType
](
#httpdatatype9
)
| 否 | 指定返回数据的类型,默认无此字段。如果设置了此参数,系统将优先返回指定的类型。 |
| usingCache
<sup>
9+
</sup>
| boolean | 否 | 是否使用缓存,默认为true。 |
| priority
<sup>
9+
</sup>
| number | 否 | 优先级,范围
\[
0,1000],默认是0。 |
...
...
@@ -913,6 +917,7 @@ createHttpResponseCache(cacheSize?: number): HttpResponseCache
```
js
import
http
from
'
@ohos.net.http
'
;
let
httpResponseCache
=
http
.
createHttpResponseCache
();
```
...
...
@@ -938,11 +943,11 @@ flush(callback: AsyncCallback\<void\>): void
```
js
httpResponseCache
.
flush
(
err
=>
{
if
(
err
)
{
console
.
info
(
'
flush fail
'
);
return
;
}
console
.
info
(
'
flush success
'
);
if
(
err
)
{
console
.
info
(
'
flush fail
'
);
return
;
}
console
.
info
(
'
flush success
'
);
});
```
...
...
@@ -964,9 +969,9 @@ flush(): Promise\<void\>
```
js
httpResponseCache
.
flush
().
then
(()
=>
{
console
.
info
(
'
flush success
'
);
console
.
info
(
'
flush success
'
);
}).
catch
(
err
=>
{
console
.
info
(
'
flush fail
'
);
console
.
info
(
'
flush fail
'
);
});
```
...
...
@@ -988,13 +993,14 @@ delete(callback: AsyncCallback\<void\>): void
```
js
httpResponseCache
.
delete
(
err
=>
{
if
(
err
)
{
console
.
info
(
'
delete fail
'
);
return
;
}
console
.
info
(
'
delete success
'
);
if
(
err
)
{
console
.
info
(
'
delete fail
'
);
return
;
}
console
.
info
(
'
delete success
'
);
});
```
### delete<sup>9+</sup>
delete(): Promise
\<
void
\>
...
...
@@ -1013,9 +1019,9 @@ delete(): Promise\<void\>
```
js
httpResponseCache
.
delete
().
then
(()
=>
{
console
.
info
(
'
delete success
'
);
console
.
info
(
'
delete success
'
);
}).
catch
(
err
=>
{
console
.
info
(
'
delete fail
'
);
console
.
info
(
'
delete fail
'
);
});
```
...
...
@@ -1025,11 +1031,8 @@ http的数据类型。
**系统能力**
:SystemCapability.Communication.NetStack
| 名称 | 值 | 说明 |
| ------------------ | -- | ----------- |
| STRING | 0 | 字符串类型。 |
| OBJECT | 1 | 对象类型。 |
| ARRAY_BUFFER | 2 | 二进制数组类型。|
| 名称 | 值 | 说明 | | ------------------ | -- | ----------- | | STRING | 0 | 字符串类型。 | | OBJECT | 1 | 对象类型。 | | ARRAY_BUFFER
| 2 | 二进制数组类型。|
## HttpProtocol<sup>9+</sup>
...
...
zh-cn/application-dev/reference/apis/js-apis-net-connection.md
浏览文件 @
732ecacf
...
...
@@ -10,6 +10,7 @@
```
js
import
connection
from
'
@ohos.net.connection
'
```
## connection.createNetConnection
createNetConnection(netSpecifier?: NetSpecifier, timeout?: number): NetConnection
...
...
@@ -39,9 +40,9 @@ let netConnection = connection.createNetConnection()
// 关注蜂窝网络,需要传入相关网络特征,timeout参数未传入说明未使用超时时间,此时timeout为0
let
netConnectionCellular
=
connection
.
createNetConnection
({
netCapabilities
:
{
bearerTypes
:
[
connection
.
NetBearType
.
BEARER_CELLULAR
]
}
netCapabilities
:
{
bearerTypes
:
[
connection
.
NetBearType
.
BEARER_CELLULAR
]
}
})
```
...
...
@@ -73,8 +74,8 @@ getDefaultNet(callback: AsyncCallback\<NetHandle>): void
```
js
connection
.
getDefaultNet
(
function
(
error
,
data
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
})
```
...
...
@@ -106,7 +107,7 @@ getDefaultNet(): Promise\<NetHandle>
```
js
connection
.
getDefaultNet
().
then
(
function
(
data
)
{
console
.
log
(
JSON
.
stringify
(
data
))
console
.
log
(
JSON
.
stringify
(
data
))
})
```
...
...
@@ -167,8 +168,8 @@ getGlobalHttpProxy(callback: AsyncCallback\<HttpProxy>): void
```
js
connection
.
getGlobalHttpProxy
((
error
,
data
)
=>
{
console
.
info
(
JSON
.
stringify
(
error
));
console
.
info
(
JSON
.
stringify
(
data
));
console
.
info
(
JSON
.
stringify
(
error
));
console
.
info
(
JSON
.
stringify
(
data
));
})
```
...
...
@@ -199,9 +200,9 @@ getGlobalHttpProxy(): Promise\<HttpProxy>;
```
js
connection
.
getGlobalHttpProxy
().
then
((
data
)
=>
{
console
.
info
(
JSON
.
stringify
(
data
));
console
.
info
(
JSON
.
stringify
(
data
));
}).
catch
(
error
=>
{
console
.
info
(
JSON
.
stringify
(
error
));
console
.
info
(
JSON
.
stringify
(
error
));
})
```
...
...
@@ -240,12 +241,12 @@ setGlobalHttpProxy(httpProxy: HttpProxy, callback: AsyncCallback\<void>): void
let
exclusionStr
=
"
192.168,baidu.com
"
let
exclusionArray
=
exclusionStr
.
split
(
'
,
'
);
let
httpProxy
=
{
host
:
"
192.168.xx.xxx
"
,
port
:
8080
,
exclusionList
:
exclusionArray
host
:
"
192.168.xx.xxx
"
,
port
:
8080
,
exclusionList
:
exclusionArray
}
connection
.
setGlobalHttpProxy
(
httpProxy
,
(
error
)
=>
{
console
.
info
(
JSON
.
stringify
(
error
));
console
.
info
(
JSON
.
stringify
(
error
));
});
```
...
...
@@ -289,14 +290,14 @@ setGlobalHttpProxy(httpProxy: HttpProxy): Promise\<void>;
let
exclusionStr
=
"
192.168,baidu.com
"
let
exclusionArray
=
exclusionStr
.
split
(
'
,
'
);
let
httpProxy
=
{
host
:
"
192.168.xx.xxx
"
,
port
:
8080
,
exclusionList
:
exclusionArray
host
:
"
192.168.xx.xxx
"
,
port
:
8080
,
exclusionList
:
exclusionArray
}
connection
.
setGlobalHttpProxy
(
httpProxy
).
then
(()
=>
{
console
.
info
(
"
success
"
);
console
.
info
(
"
success
"
);
}).
catch
(
error
=>
{
console
.
info
(
JSON
.
stringify
(
error
));
console
.
info
(
JSON
.
stringify
(
error
));
})
```
...
...
@@ -325,8 +326,8 @@ getAppNet(callback: AsyncCallback\<NetHandle>): void
```
js
connection
.
getAppNet
(
function
(
error
,
data
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
})
```
...
...
@@ -355,9 +356,9 @@ getAppNet(): Promise\<NetHandle>;
```
js
connection
.
getAppNet
().
then
((
data
)
=>
{
console
.
info
(
JSON
.
stringify
(
data
));
console
.
info
(
JSON
.
stringify
(
data
));
}).
catch
(
error
=>
{
console
.
info
(
JSON
.
stringify
(
error
));
console
.
info
(
JSON
.
stringify
(
error
));
})
```
...
...
@@ -392,10 +393,10 @@ setAppNet(netHandle: NetHandle, callback: AsyncCallback\<void>): void
```
js
connection
.
getDefaultNet
(
function
(
error
,
netHandle
)
{
connection
.
setAppNet
(
netHandle
,
(
error
,
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
});
connection
.
setAppNet
(
netHandle
,
(
error
,
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
});
})
```
...
...
@@ -435,11 +436,11 @@ setAppNet(netHandle: NetHandle): Promise\<void>;
```
js
connection
.
getDefaultNet
().
then
(
function
(
netHandle
)
{
connection
.
setAppNet
(
netHandle
).
then
(()
=>
{
console
.
log
(
"
success
"
)
}).
catch
(
error
=>
{
console
.
log
(
JSON
.
stringify
(
error
))
})
connection
.
setAppNet
(
netHandle
).
then
(()
=>
{
console
.
log
(
"
success
"
)
}).
catch
(
error
=>
{
console
.
log
(
JSON
.
stringify
(
error
))
})
})
```
...
...
@@ -471,8 +472,8 @@ getAllNets(callback: AsyncCallback<Array<NetHandle>>): void
```
js
connection
.
getAllNets
(
function
(
error
,
data
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
});
```
...
...
@@ -504,7 +505,7 @@ getAllNets(): Promise<Array<NetHandle>>
```
js
connection
.
getAllNets
().
then
(
function
(
data
)
{
console
.
log
(
JSON
.
stringify
(
data
))
console
.
log
(
JSON
.
stringify
(
data
))
});
```
...
...
@@ -539,10 +540,10 @@ getConnectionProperties(netHandle: NetHandle, callback: AsyncCallback\<Connectio
```
js
connection
.
getDefaultNet
().
then
(
function
(
netHandle
)
{
connection
.
getConnectionProperties
(
netHandle
,
function
(
error
,
data
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
})
connection
.
getConnectionProperties
(
netHandle
,
function
(
error
,
data
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
})
})
```
...
...
@@ -582,9 +583,9 @@ getConnectionProperties(netHandle: NetHandle): Promise\<ConnectionProperties>
```
js
connection
.
getDefaultNet
().
then
(
function
(
netHandle
)
{
connection
.
getConnectionProperties
(
netHandle
).
then
(
function
(
data
)
{
console
.
log
(
JSON
.
stringify
(
data
))
})
connection
.
getConnectionProperties
(
netHandle
).
then
(
function
(
data
)
{
console
.
log
(
JSON
.
stringify
(
data
))
})
})
```
...
...
@@ -619,10 +620,10 @@ getNetCapabilities(netHandle: NetHandle, callback: AsyncCallback\<NetCapabilitie
```
js
connection
.
getDefaultNet
().
then
(
function
(
netHandle
)
{
connection
.
getNetCapabilities
(
netHandle
,
function
(
error
,
data
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
})
connection
.
getNetCapabilities
(
netHandle
,
function
(
error
,
data
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
})
})
```
...
...
@@ -662,9 +663,9 @@ getNetCapabilities(netHandle: NetHandle): Promise\<NetCapabilities>
```
js
connection
.
getDefaultNet
().
then
(
function
(
netHandle
)
{
connection
.
getNetCapabilities
(
netHandle
).
then
(
function
(
data
)
{
console
.
log
(
JSON
.
stringify
(
data
))
})
connection
.
getNetCapabilities
(
netHandle
).
then
(
function
(
data
)
{
console
.
log
(
JSON
.
stringify
(
data
))
})
})
```
...
...
@@ -696,8 +697,8 @@ isDefaultNetMetered(callback: AsyncCallback\<boolean>): void
```
js
connection
.
isDefaultNetMetered
(
function
(
error
,
data
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
'
data:
'
+
data
)
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
'
data:
'
+
data
)
})
```
...
...
@@ -729,7 +730,7 @@ isDefaultNetMetered(): Promise\<boolean>
```
js
connection
.
isDefaultNetMetered
().
then
(
function
(
data
)
{
console
.
log
(
'
data:
'
+
data
)
console
.
log
(
'
data:
'
+
data
)
})
```
...
...
@@ -761,8 +762,8 @@ hasDefaultNet(callback: AsyncCallback\<boolean>): void
```
js
connection
.
hasDefaultNet
(
function
(
error
,
data
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
'
data:
'
+
data
)
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
'
data:
'
+
data
)
})
```
...
...
@@ -794,7 +795,7 @@ hasDefaultNet(): Promise\<boolean>
```
js
connection
.
hasDefaultNet
().
then
(
function
(
data
)
{
console
.
log
(
'
data:
'
+
data
)
console
.
log
(
'
data:
'
+
data
)
})
```
...
...
@@ -827,7 +828,7 @@ enableAirplaneMode(callback: AsyncCallback\<void>): void
```
js
connection
.
enableAirplaneMode
(
function
(
error
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
error
))
})
```
...
...
@@ -860,7 +861,7 @@ enableAirplaneMode(): Promise\<void>
```
js
connection
.
enableAirplaneMode
().
then
(
function
(
error
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
error
))
})
```
...
...
@@ -893,7 +894,7 @@ disableAirplaneMode(callback: AsyncCallback\<void>): void
```
js
connection
.
disableAirplaneMode
(
function
(
error
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
error
))
})
```
...
...
@@ -926,7 +927,7 @@ disableAirplaneMode(): Promise\<void>
```
js
connection
.
disableAirplaneMode
().
then
(
function
(
error
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
error
))
})
```
...
...
@@ -961,9 +962,9 @@ reportNetConnected(netHandle: NetHandle, callback: AsyncCallback<void>): v
```
js
connection
.
getDefaultNet
().
then
(
function
(
netHandle
)
{
connection
.
reportNetConnected
(
netHandle
,
function
(
error
)
{
console
.
log
(
JSON
.
stringify
(
error
))
});
connection
.
reportNetConnected
(
netHandle
,
function
(
error
)
{
console
.
log
(
JSON
.
stringify
(
error
))
});
});
```
...
...
@@ -984,9 +985,7 @@ reportNetConnected(netHandle: NetHandle): Promise<void>
| netHandle |
[
NetHandle
](
#nethandle
)
| 是 | 数据网络的句柄,参考
[
NetHandle
](
#nethandle
)
。 |
**返回值:**
| 类型 | 说明 |
| -------- | -------- |
| Promise
<
void
>
| 无返回值的Promise对象。 |
| 类型 | 说明 | | -------- | -------- | | Promise
<
void
>
| 无返回值的Promise对象。 |
**错误码:**
...
...
@@ -1002,9 +1001,9 @@ reportNetConnected(netHandle: NetHandle): Promise<void>
```
js
connection
.
getDefaultNet
().
then
(
function
(
netHandle
)
{
connection
.
reportNetConnected
(
netHandle
).
then
(
function
()
{
console
.
log
(
`report success`
)
});
connection
.
reportNetConnected
(
netHandle
).
then
(
function
()
{
console
.
log
(
`report success`
)
});
});
```
...
...
@@ -1012,8 +1011,7 @@ connection.getDefaultNet().then(function (netHandle) {
reportNetDisconnected(netHandle: NetHandle, callback: AsyncCallback
<
void
>
): void
向网络管理报告网络处于不可用状态,调用此接口说明应用程序认为网络的可用性(ohos.net.connection.NetCap.NET_CAPABILITY_VAILDATED)与网络管理不一致。
使用callback方式作为异步方法。
向网络管理报告网络处于不可用状态,调用此接口说明应用程序认为网络的可用性(ohos.net.connection.NetCap.NET_CAPABILITY_VAILDATED)与网络管理不一致。 使用callback方式作为异步方法。
**需要权限**
:ohos.permission.GET_NETWORK_INFO 和 ohos.permission.INTERNET
...
...
@@ -1040,9 +1038,9 @@ reportNetDisconnected(netHandle: NetHandle, callback: AsyncCallback<void>)
```
js
connection
.
getDefaultNet
().
then
(
function
(
netHandle
)
{
connection
.
reportNetDisconnected
(
netHandle
,
function
(
error
)
{
console
.
log
(
JSON
.
stringify
(
error
))
});
connection
.
reportNetDisconnected
(
netHandle
,
function
(
error
)
{
console
.
log
(
JSON
.
stringify
(
error
))
});
});
```
...
...
@@ -1050,8 +1048,7 @@ connection.getDefaultNet().then(function (netHandle) {
reportNetDisconnected(netHandle: NetHandle): Promise
<
void
>
向网络管理报告网络处于不可用状态,调用此接口说明应用程序认为网络的可用性(ohos.net.connection.NetCap.NET_CAPABILITY_VAILDATED)与网络管理不一致。
使用Promise方式作为异步方法。
向网络管理报告网络处于不可用状态,调用此接口说明应用程序认为网络的可用性(ohos.net.connection.NetCap.NET_CAPABILITY_VAILDATED)与网络管理不一致。 使用Promise方式作为异步方法。
**需要权限**
:ohos.permission.GET_NETWORK_INFO 和 ohos.permission.INTERNET
...
...
@@ -1064,9 +1061,7 @@ reportNetDisconnected(netHandle: NetHandle): Promise<void>
| netHandle |
[
NetHandle
](
#nethandle
)
| 是 | 数据网络的句柄,参考
[
NetHandle
](
#nethandle
)
。 |
**返回值:**
| 类型 | 说明 |
| -------- | -------- |
| Promise
<
void
>
| 无返回值的Promise对象。 |
| 类型 | 说明 | | -------- | -------- | | Promise
<
void
>
| 无返回值的Promise对象。 |
**错误码:**
...
...
@@ -1082,9 +1077,9 @@ reportNetDisconnected(netHandle: NetHandle): Promise<void>
```
js
connection
.
getDefaultNet
().
then
(
function
(
netHandle
)
{
connection
.
reportNetDisconnected
(
netHandle
).
then
(
function
()
{
console
.
log
(
`report success`
)
});
connection
.
reportNetDisconnected
(
netHandle
).
then
(
function
()
{
console
.
log
(
`report success`
)
});
});
```
...
...
@@ -1120,8 +1115,8 @@ getAddressesByName(host: string, callback: AsyncCallback\<Array\<NetAddress>>):
```
js
let
host
=
"
xxxx
"
;
connection
.
getAddressesByName
(
host
,
function
(
error
,
data
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
})
```
...
...
@@ -1162,7 +1157,7 @@ getAddressesByName(host: string): Promise\<Array\<NetAddress>>
```
js
let
host
=
"
xxxx
"
;
connection
.
getAddressesByName
(
host
).
then
(
function
(
data
)
{
console
.
log
(
JSON
.
stringify
(
data
))
console
.
log
(
JSON
.
stringify
(
data
))
})
```
...
...
@@ -1196,12 +1191,11 @@ register(callback: AsyncCallback\<void>): void
| 2101008 | The callback is not exists. |
| 2101022 | The number of requests exceeded the maximum. |
**示例:**
```
js
netConnection
.
register
(
function
(
error
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
error
))
})
```
...
...
@@ -1231,7 +1225,7 @@ unregister(callback: AsyncCallback\<void>): void
```
js
netConnection
.
unregister
(
function
(
error
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
error
))
})
```
...
...
@@ -1260,17 +1254,17 @@ let netCon = connection.createNetConnection()
// 先使用register接口注册订阅事件
netCon
.
register
(
function
(
error
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
error
))
})
// 订阅网络可用事件。调用register后,才能接收到此事件通知
netCon
.
on
(
'
netAvailable
'
,
function
(
data
)
{
console
.
log
(
JSON
.
stringify
(
data
))
console
.
log
(
JSON
.
stringify
(
data
))
})
// 使用unregister接口取消订阅
netCon
.
unregister
(
function
(
error
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
error
))
})
```
...
...
@@ -1299,17 +1293,17 @@ let netCon = connection.createNetConnection()
// 先使用register接口注册订阅事件
netCon
.
register
(
function
(
error
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
error
))
})
// 订阅网络阻塞状态事件。调用register后,才能接收到此事件通知
netCon
.
on
(
'
netBlockStatusChange
'
,
function
(
data
)
{
console
.
log
(
JSON
.
stringify
(
data
))
console
.
log
(
JSON
.
stringify
(
data
))
})
// 使用unregister接口取消订阅
netCon
.
unregister
(
function
(
error
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
error
))
})
```
...
...
@@ -1338,23 +1332,24 @@ let netCon = connection.createNetConnection()
// 先使用register接口注册订阅事件
netCon
.
register
(
function
(
error
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
error
))
})
// 订阅网络能力变化事件。调用register后,才能接收到此事件通知
netCon
.
on
(
'
netCapabilitiesChange
'
,
function
(
data
)
{
console
.
log
(
JSON
.
stringify
(
data
))
console
.
log
(
JSON
.
stringify
(
data
))
})
// 使用unregister接口取消订阅
netCon
.
unregister
(
function
(
error
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
error
))
})
```
### on('netConnectionPropertiesChange')
on(type: 'netConnectionPropertiesChange', callback: Callback
<
{
netHandle:
NetHandle
,
connectionProperties:
ConnectionProperties
}
>
): void
on(type: 'netConnectionPropertiesChange', callback: Callback<{ netHandle: NetHandle, connectionProperties:
ConnectionProperties }>): void
订阅网络连接信息变化事件。
...
...
@@ -1377,17 +1372,17 @@ let netCon = connection.createNetConnection()
// 先使用register接口注册订阅事件
netCon
.
register
(
function
(
error
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
error
))
})
// 订阅网络连接信息变化事件。调用register后,才能接收到此事件通知
netCon
.
on
(
'
netConnectionPropertiesChange
'
,
function
(
data
)
{
console
.
log
(
JSON
.
stringify
(
data
))
console
.
log
(
JSON
.
stringify
(
data
))
})
// 使用unregister接口取消订阅
netCon
.
unregister
(
function
(
error
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
error
))
})
```
...
...
@@ -1416,17 +1411,17 @@ let netCon = connection.createNetConnection()
// 先使用register接口注册订阅事件
netCon
.
register
(
function
(
error
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
error
))
})
// 订阅网络丢失事件。调用register后,才能接收到此事件通知
netCon
.
on
(
'
netLost
'
,
function
(
data
)
{
console
.
log
(
JSON
.
stringify
(
data
))
console
.
log
(
JSON
.
stringify
(
data
))
})
// 使用unregister接口取消订阅
netCon
.
unregister
(
function
(
error
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
error
))
})
```
...
...
@@ -1455,17 +1450,17 @@ let netCon = connection.createNetConnection()
// 先使用register接口注册订阅事件
netCon
.
register
(
function
(
error
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
error
))
})
// 订阅网络不可用事件。调用register后,才能接收到此事件通知
netCon
.
on
(
'
netUnavailable
'
,
function
(
data
)
{
console
.
log
(
JSON
.
stringify
(
data
))
console
.
log
(
JSON
.
stringify
(
data
))
})
// 使用unregister接口取消订阅
netCon
.
unregister
(
function
(
error
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
error
))
})
```
...
...
@@ -1511,50 +1506,51 @@ bindSocket(socketParam: TCPSocket \| UDPSocket, callback: AsyncCallback\<void>):
```
js
import
socket
from
"
@ohos.net.socket
"
;
connection
.
getDefaultNet
().
then
((
netHandle
)
=>
{
var
tcp
=
socket
.
constructTCPSocketInstance
();
var
udp
=
socket
.
constructUDPSocketInstance
();
let
socketType
=
"
TCPSocket
"
;
if
(
socketType
==
"
TCPSocket
"
)
{
tcp
.
bind
({
address
:
'
192.168.xx.xxx
'
,
port
:
8080
,
family
:
1
},
error
=>
{
if
(
error
)
{
console
.
log
(
'
bind fail
'
);
return
;
}
netHandle
.
bindSocket
(
tcp
,
(
error
,
data
)
=>
{
if
(
error
)
{
console
.
log
(
JSON
.
stringify
(
error
));
}
else
{
console
.
log
(
JSON
.
stringify
(
data
));
}
})
})
}
else
{
let
callback
=
value
=>
{
console
.
log
(
"
on message, message:
"
+
value
.
message
+
"
, remoteInfo:
"
+
value
.
remoteInfo
);
var
tcp
=
socket
.
constructTCPSocketInstance
();
var
udp
=
socket
.
constructUDPSocketInstance
();
let
socketType
=
"
TCPSocket
"
;
if
(
socketType
==
"
TCPSocket
"
)
{
tcp
.
bind
({
address
:
'
192.168.xx.xxx
'
,
port
:
8080
,
family
:
1
},
error
=>
{
if
(
error
)
{
console
.
log
(
'
bind fail
'
);
return
;
}
netHandle
.
bindSocket
(
tcp
,
(
error
,
data
)
=>
{
if
(
error
)
{
console
.
log
(
JSON
.
stringify
(
error
));
}
else
{
console
.
log
(
JSON
.
stringify
(
data
));
}
udp
.
on
(
'
message
'
,
callback
);
udp
.
bind
({
address
:
'
192.168.xx.xxx
'
,
port
:
8080
,
family
:
1
},
error
=>
{
if
(
error
)
{
console
.
log
(
'
bind fail
'
);
return
;
}
udp
.
on
(
'
message
'
,
(
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
data
))
});
netHandle
.
bindSocket
(
udp
,
(
error
,
data
)
=>
{
if
(
error
)
{
console
.
log
(
JSON
.
stringify
(
error
));
}
else
{
console
.
log
(
JSON
.
stringify
(
data
));
}
})
})
})
})
}
else
{
let
callback
=
value
=>
{
console
.
log
(
"
on message, message:
"
+
value
.
message
+
"
, remoteInfo:
"
+
value
.
remoteInfo
);
}
udp
.
on
(
'
message
'
,
callback
);
udp
.
bind
({
address
:
'
192.168.xx.xxx
'
,
port
:
8080
,
family
:
1
},
error
=>
{
if
(
error
)
{
console
.
log
(
'
bind fail
'
);
return
;
}
udp
.
on
(
'
message
'
,
(
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
data
))
});
netHandle
.
bindSocket
(
udp
,
(
error
,
data
)
=>
{
if
(
error
)
{
console
.
log
(
JSON
.
stringify
(
error
));
}
else
{
console
.
log
(
JSON
.
stringify
(
data
));
}
})
})
}
})
```
...
...
@@ -1591,46 +1587,47 @@ bindSocket(socketParam: TCPSocket \| UDPSocket): Promise\<void>;
```
js
import
socket
from
"
@ohos.net.socket
"
;
connection
.
getDefaultNet
().
then
((
netHandle
)
=>
{
var
tcp
=
socket
.
constructTCPSocketInstance
();
var
udp
=
socket
.
constructUDPSocketInstance
();
let
socketType
=
"
TCPSocket
"
;
if
(
socketType
==
"
TCPSocket
"
)
{
tcp
.
bind
({
address
:
'
192.168.xx.xxx
'
,
port
:
8080
,
family
:
1
},
error
=>
{
if
(
error
)
{
console
.
log
(
'
bind fail
'
);
return
;
}
netHandle
.
bindSocket
(
tcp
).
then
((
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
data
));
}).
catch
(
error
=>
{
console
.
log
(
JSON
.
stringify
(
error
));
})
})
}
else
{
let
callback
=
value
=>
{
console
.
log
(
"
on message, message:
"
+
value
.
message
+
"
, remoteInfo:
"
+
value
.
remoteInfo
);
}
udp
.
on
(
'
message
'
,
callback
);
udp
.
bind
({
address
:
'
192.168.xx.xxx
'
,
port
:
8080
,
family
:
1
},
error
=>
{
if
(
error
)
{
console
.
log
(
'
bind fail
'
);
return
;
}
udp
.
on
(
'
message
'
,
(
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
data
));
})
netHandle
.
bindSocket
(
udp
).
then
((
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
data
));
}).
catch
(
error
=>
{
console
.
log
(
JSON
.
stringify
(
error
));
})
})
var
tcp
=
socket
.
constructTCPSocketInstance
();
var
udp
=
socket
.
constructUDPSocketInstance
();
let
socketType
=
"
TCPSocket
"
;
if
(
socketType
==
"
TCPSocket
"
)
{
tcp
.
bind
({
address
:
'
192.168.xx.xxx
'
,
port
:
8080
,
family
:
1
},
error
=>
{
if
(
error
)
{
console
.
log
(
'
bind fail
'
);
return
;
}
netHandle
.
bindSocket
(
tcp
).
then
((
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
data
));
}).
catch
(
error
=>
{
console
.
log
(
JSON
.
stringify
(
error
));
})
})
}
else
{
let
callback
=
value
=>
{
console
.
log
(
"
on message, message:
"
+
value
.
message
+
"
, remoteInfo:
"
+
value
.
remoteInfo
);
}
udp
.
on
(
'
message
'
,
callback
);
udp
.
bind
({
address
:
'
192.168.xx.xxx
'
,
port
:
8080
,
family
:
1
},
error
=>
{
if
(
error
)
{
console
.
log
(
'
bind fail
'
);
return
;
}
udp
.
on
(
'
message
'
,
(
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
data
));
})
netHandle
.
bindSocket
(
udp
).
then
((
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
data
));
}).
catch
(
error
=>
{
console
.
log
(
JSON
.
stringify
(
error
));
})
})
}
})
```
...
...
@@ -1665,11 +1662,11 @@ getAddressesByName(host: string, callback: AsyncCallback\<Array\<NetAddress>>):
```
js
connection
.
getDefaultNet
().
then
(
function
(
netHandle
)
{
let
host
=
"
xxxx
"
;
netHandle
.
getAddressesByName
(
host
,
function
(
error
,
data
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
})
let
host
=
"
xxxx
"
;
netHandle
.
getAddressesByName
(
host
,
function
(
error
,
data
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
})
})
```
...
...
@@ -1709,10 +1706,10 @@ getAddressesByName(host: string): Promise\<Array\<NetAddress>>
```
js
connection
.
getDefaultNet
().
then
(
function
(
netHandle
)
{
let
host
=
"
xxxx
"
;
netHandle
.
getAddressesByName
(
host
).
then
(
function
(
data
)
{
console
.
log
(
JSON
.
stringify
(
data
))
})
let
host
=
"
xxxx
"
;
netHandle
.
getAddressesByName
(
host
).
then
(
function
(
data
)
{
console
.
log
(
JSON
.
stringify
(
data
))
})
})
```
...
...
@@ -1747,11 +1744,11 @@ getAddressByName(host: string, callback: AsyncCallback\<NetAddress>): void
```
js
connection
.
getDefaultNet
().
then
(
function
(
netHandle
)
{
let
host
=
"
xxxx
"
;
netHandle
.
getAddressByName
(
host
,
function
(
error
,
data
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
})
let
host
=
"
xxxx
"
;
netHandle
.
getAddressByName
(
host
,
function
(
error
,
data
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
})
})
```
...
...
@@ -1791,10 +1788,10 @@ getAddressByName(host: string): Promise\<NetAddress>
```
js
connection
.
getDefaultNet
().
then
(
function
(
netHandle
)
{
let
host
=
"
xxxx
"
;
netHandle
.
getAddressByName
(
host
).
then
(
function
(
data
)
{
console
.
log
(
JSON
.
stringify
(
data
))
})
let
host
=
"
xxxx
"
;
netHandle
.
getAddressByName
(
host
).
then
(
function
(
data
)
{
console
.
log
(
JSON
.
stringify
(
data
))
})
})
```
...
...
@@ -1906,8 +1903,5 @@ connection.getDefaultNet().then(function (netHandle) {
**系统能力**
:SystemCapability.Communication.NetManager.Core
| 名称 | 类型 | 必填 | 说明 |
| ------- | ------ | -- |------------------------------ |
| address | string | 是 |地址。 |
| family | number | 否 |IPv4 = 1,IPv6 = 2,默认IPv4。 |
| port | number | 否 |端口,取值范围
\[
0, 65535]。 |
| 名称 | 类型 | 必填 | 说明 | | ------- | ------ | -- |------------------------------ | | address | string | 是 |地址。 | | family |
number | 否 |IPv4 = 1,IPv6 = 2,默认IPv4。 | | port | number | 否 |端口,取值范围
\[
0, 65535]。 |
zh-cn/application-dev/reference/apis/js-apis-net-ethernet.md
浏览文件 @
732ecacf
...
...
@@ -48,19 +48,19 @@ setIfaceConfig(iface: string, ic: InterfaceConfiguration, callback: AsyncCallbac
```
js
ethernet
.
setIfaceConfig
(
"
eth0
"
,
{
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
"
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
"
},
(
error
)
=>
{
if
(
error
)
{
console
.
log
(
"
setIfaceConfig callback error =
"
+
JSON
.
stringify
(
error
));
}
else
{
console
.
log
(
"
setIfaceConfig callback ok
"
);
}
if
(
error
)
{
console
.
log
(
"
setIfaceConfig callback error =
"
+
JSON
.
stringify
(
error
));
}
else
{
console
.
log
(
"
setIfaceConfig callback ok
"
);
}
});
```
...
...
@@ -106,17 +106,17 @@ setIfaceConfig(iface: string, ic: InterfaceConfiguration): Promise\<void>
```
js
ethernet
.
setIfaceConfig
(
"
eth0
"
,
{
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
"
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 promise ok
"
);
console
.
log
(
"
setIfaceConfig promise ok
"
);
}).
catch
(
error
=>
{
console
.
log
(
"
setIfaceConfig promise error =
"
+
JSON
.
stringify
(
error
));
console
.
log
(
"
setIfaceConfig promise error =
"
+
JSON
.
stringify
(
error
));
});
```
...
...
@@ -154,17 +154,17 @@ getIfaceConfig(iface: string, callback: AsyncCallback\<InterfaceConfiguration>):
```
js
ethernet
.
getIfaceConfig
(
"
eth0
"
,
(
error
,
value
)
=>
{
if
(
error
)
{
console
.
log
(
"
getIfaceConfig callback error =
"
+
JSON
.
stringify
(
error
));
}
else
{
console
.
log
(
"
getIfaceConfig callback mode =
"
+
JSON
.
stringify
(
value
.
mode
));
console
.
log
(
"
getIfaceConfig callback ipAddr =
"
+
JSON
.
stringify
(
value
.
ipAddr
));
console
.
log
(
"
getIfaceConfig callback route =
"
+
JSON
.
stringify
(
value
.
route
));
console
.
log
(
"
getIfaceConfig callback gateway =
"
+
JSON
.
stringify
(
value
.
gateway
));
console
.
log
(
"
getIfaceConfig callback netMask =
"
+
JSON
.
stringify
(
value
.
netMask
));
console
.
log
(
"
getIfaceConfig callback dnsServers =
"
+
JSON
.
stringify
(
value
.
dnsServers
));
console
.
log
(
"
getIfaceConfig callback domain =
"
+
JSON
.
stringify
(
value
.
domain
));
}
if
(
error
)
{
console
.
log
(
"
getIfaceConfig callback error =
"
+
JSON
.
stringify
(
error
));
}
else
{
console
.
log
(
"
getIfaceConfig callback mode =
"
+
JSON
.
stringify
(
value
.
mode
));
console
.
log
(
"
getIfaceConfig callback ipAddr =
"
+
JSON
.
stringify
(
value
.
ipAddr
));
console
.
log
(
"
getIfaceConfig callback route =
"
+
JSON
.
stringify
(
value
.
route
));
console
.
log
(
"
getIfaceConfig callback gateway =
"
+
JSON
.
stringify
(
value
.
gateway
));
console
.
log
(
"
getIfaceConfig callback netMask =
"
+
JSON
.
stringify
(
value
.
netMask
));
console
.
log
(
"
getIfaceConfig callback dnsServers =
"
+
JSON
.
stringify
(
value
.
dnsServers
));
console
.
log
(
"
getIfaceConfig callback domain =
"
+
JSON
.
stringify
(
value
.
domain
));
}
});
```
...
...
@@ -207,15 +207,15 @@ getIfaceConfig(iface: string): Promise\<InterfaceConfiguration>
```
js
ethernet
.
getIfaceConfig
(
"
eth0
"
).
then
((
data
)
=>
{
console
.
log
(
"
getIfaceConfig promise mode =
"
+
JSON
.
stringify
(
data
.
mode
));
console
.
log
(
"
getIfaceConfig promise ipAddr =
"
+
JSON
.
stringify
(
data
.
ipAddr
));
console
.
log
(
"
getIfaceConfig promise route =
"
+
JSON
.
stringify
(
data
.
route
));
console
.
log
(
"
getIfaceConfig promise gateway =
"
+
JSON
.
stringify
(
data
.
gateway
));
console
.
log
(
"
getIfaceConfig promise netMask =
"
+
JSON
.
stringify
(
data
.
netMask
));
console
.
log
(
"
getIfaceConfig promise dnsServers =
"
+
JSON
.
stringify
(
data
.
dnsServers
));
console
.
log
(
"
getIfaceConfig promise domain =
"
+
JSON
.
stringify
(
data
.
domain
));
console
.
log
(
"
getIfaceConfig promise mode =
"
+
JSON
.
stringify
(
data
.
mode
));
console
.
log
(
"
getIfaceConfig promise ipAddr =
"
+
JSON
.
stringify
(
data
.
ipAddr
));
console
.
log
(
"
getIfaceConfig promise route =
"
+
JSON
.
stringify
(
data
.
route
));
console
.
log
(
"
getIfaceConfig promise gateway =
"
+
JSON
.
stringify
(
data
.
gateway
));
console
.
log
(
"
getIfaceConfig promise netMask =
"
+
JSON
.
stringify
(
data
.
netMask
));
console
.
log
(
"
getIfaceConfig promise dnsServers =
"
+
JSON
.
stringify
(
data
.
dnsServers
));
console
.
log
(
"
getIfaceConfig promise domain =
"
+
JSON
.
stringify
(
data
.
domain
));
}).
catch
(
error
=>
{
console
.
log
(
"
getIfaceConfig promise error =
"
+
JSON
.
stringify
(
error
));
console
.
log
(
"
getIfaceConfig promise error =
"
+
JSON
.
stringify
(
error
));
});
```
...
...
@@ -253,11 +253,11 @@ isIfaceActive(iface: string, callback: AsyncCallback\<number>): void
```
js
ethernet
.
isIfaceActive
(
"
eth0
"
,
(
error
,
value
)
=>
{
if
(
error
)
{
console
.
log
(
"
whether2Activate callback error =
"
+
JSON
.
stringify
(
error
));
}
else
{
console
.
log
(
"
whether2Activate callback =
"
+
JSON
.
stringify
(
value
));
}
if
(
error
)
{
console
.
log
(
"
whether2Activate callback error =
"
+
JSON
.
stringify
(
error
));
}
else
{
console
.
log
(
"
whether2Activate callback =
"
+
JSON
.
stringify
(
value
));
}
});
```
...
...
@@ -300,9 +300,9 @@ isIfaceActive(iface: string): Promise\<number>
```
js
ethernet
.
isIfaceActive
(
"
eth0
"
).
then
((
data
)
=>
{
console
.
log
(
"
isIfaceActive promise =
"
+
JSON
.
stringify
(
data
));
console
.
log
(
"
isIfaceActive promise =
"
+
JSON
.
stringify
(
data
));
}).
catch
(
error
=>
{
console
.
log
(
"
isIfaceActive promise error =
"
+
JSON
.
stringify
(
error
));
console
.
log
(
"
isIfaceActive promise error =
"
+
JSON
.
stringify
(
error
));
});
```
...
...
@@ -336,14 +336,14 @@ getAllActiveIfaces(callback: AsyncCallback\<Array\<string>>): void
```
js
ethernet
.
getAllActiveIfaces
((
error
,
value
)
=>
{
if
(
error
)
{
console
.
log
(
"
getAllActiveIfaces callback error =
"
+
JSON
.
stringify
(
error
));
}
else
{
console
.
log
(
"
getAllActiveIfaces callback value.length =
"
+
JSON
.
stringify
(
value
.
length
));
for
(
let
i
=
0
;
i
<
value
.
length
;
i
++
)
{
console
.
log
(
"
getAllActiveIfaces callback =
"
+
JSON
.
stringify
(
value
[
i
]));
}
if
(
error
)
{
console
.
log
(
"
getAllActiveIfaces callback error =
"
+
JSON
.
stringify
(
error
));
}
else
{
console
.
log
(
"
getAllActiveIfaces callback value.length =
"
+
JSON
.
stringify
(
value
.
length
));
for
(
let
i
=
0
;
i
<
value
.
length
;
i
++
)
{
console
.
log
(
"
getAllActiveIfaces callback =
"
+
JSON
.
stringify
(
value
[
i
]));
}
}
});
```
...
...
@@ -377,16 +377,16 @@ getAllActiveIfaces(): Promise\<Array\<string>>
```
js
ethernet
.
getAllActiveIfaces
().
then
((
data
)
=>
{
console
.
log
(
"
getAllActiveIfaces promise data.length =
"
+
JSON
.
stringify
(
data
.
length
));
for
(
let
i
=
0
;
i
<
data
.
length
;
i
++
)
{
console
.
log
(
"
getAllActiveIfaces promise =
"
+
JSON
.
stringify
(
data
[
i
]));
}
console
.
log
(
"
getAllActiveIfaces promise data.length =
"
+
JSON
.
stringify
(
data
.
length
));
for
(
let
i
=
0
;
i
<
data
.
length
;
i
++
)
{
console
.
log
(
"
getAllActiveIfaces promise =
"
+
JSON
.
stringify
(
data
[
i
]));
}
}).
catch
(
error
=>
{
console
.
log
(
"
getAllActiveIfaces promise error =
"
+
JSON
.
stringify
(
error
));
console
.
log
(
"
getAllActiveIfaces promise error =
"
+
JSON
.
stringify
(
error
));
});
```
##
sharing
.on('interfaceStateChange')<sup>10+</sup>
##
ethernet
.on('interfaceStateChange')<sup>10+</sup>
on(type: 'interfaceStateChange', callback: Callback
\<
{ iface: string, active: boolean }
\>
): void
...
...
@@ -417,7 +417,7 @@ on(type: 'interfaceStateChange', callback: Callback\<{ iface: string, active: bo
```
js
ethernet
.
on
(
'
interfaceStateChange
'
,
(
data
)
=>
{
console
.
log
(
'
on interfaceSharingStateChange:
'
+
JSON
.
stringify
(
data
));
console
.
log
(
'
on interfaceSharingStateChange:
'
+
JSON
.
stringify
(
data
));
});
```
...
...
zh-cn/application-dev/reference/apis/js-apis-net-mdns.md
浏览文件 @
732ecacf
...
...
@@ -10,6 +10,7 @@ MDNS即多播DNS(Multicast DNS),提供局域网内的本地服务添加、
```
js
import
mdns
from
'
@ohos.net.mdns
'
```
## mdns.addLocalService
addLocalService(context: Context, serviceInfo: LocalServiceInfo, callback: AsyncCallback
\<
LocalServiceInfo>): void
...
...
@@ -37,28 +38,28 @@ addLocalService(context: Context, serviceInfo: LocalServiceInfo, callback: Async
| 2204008 | Service instance duplicated. |
| 2204010 | Send packet failed. |
>**错误码说明:**
>
**错误码说明:**
> 以上错误码的详细介绍参见[MDNS错误码](../errorcodes/errorcode-net-mdns.md)。
**示例:**
```
js
let
localServiceInfo
=
{
serviceType
:
"
_print._tcp
"
,
serviceName
:
"
servicename
"
,
port
:
5555
,
host
:
{
address
:
"
10.14.**.***
"
,
},
serviceAttribute
:
[{
key
:
"
111
"
,
value
:
[
1
]
}]
serviceType
:
"
_print._tcp
"
,
serviceName
:
"
servicename
"
,
port
:
5555
,
host
:
{
address
:
"
10.14.**.***
"
,
},
serviceAttribute
:
[{
key
:
"
111
"
,
value
:
[
1
]
}]
}
mdns
.
addLocalService
(
context
,
localServiceInfo
,
function
(
error
,
data
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
});
```
...
...
@@ -94,27 +95,27 @@ addLocalService(context: Context, serviceInfo: LocalServiceInfo): Promise\<Local
| 2204008 | Service instance duplicated. |
| 2204010 | Send packet failed. |
>**错误码说明:**
>
**错误码说明:**
> 以上错误码的详细介绍参见[MDNS错误码](../errorcodes/errorcode-net-mdns.md)。
**示例:**
```
js
let
localServiceInfo
=
{
serviceType
:
"
_print._tcp
"
,
serviceName
:
"
servicename
"
,
port
:
5555
,
host
:
{
address
:
"
10.14.**.***
"
,
},
serviceAttribute
:
[{
key
:
"
111
"
,
value
:
[
1
]
}]
serviceType
:
"
_print._tcp
"
,
serviceName
:
"
servicename
"
,
port
:
5555
,
host
:
{
address
:
"
10.14.**.***
"
,
},
serviceAttribute
:
[{
key
:
"
111
"
,
value
:
[
1
]
}]
}
mdns
.
addLocalService
(
context
,
localServiceInfo
).
then
(
function
(
data
)
{
console
.
log
(
JSON
.
stringify
(
data
))
console
.
log
(
JSON
.
stringify
(
data
))
});
```
...
...
@@ -145,28 +146,28 @@ removeLocalService(context: Context, serviceInfo: LocalServiceInfo, callback: As
| 2204008 | Service instance duplicated. |
| 2204010 | Send packet failed. |
>**错误码说明:**
>
**错误码说明:**
> 以上错误码的详细介绍参见[MDNS错误码](../errorcodes/errorcode-net-mdns.md)。
**示例:**
```
js
let
localServiceInfo
=
{
serviceType
:
"
_print._tcp
"
,
serviceName
:
"
servicename
"
,
port
:
5555
,
host
:
{
address
:
"
10.14.**.***
"
,
},
serviceAttribute
:
[{
key
:
"
111
"
,
value
:
[
1
]
}]
serviceType
:
"
_print._tcp
"
,
serviceName
:
"
servicename
"
,
port
:
5555
,
host
:
{
address
:
"
10.14.**.***
"
,
},
serviceAttribute
:
[{
key
:
"
111
"
,
value
:
[
1
]
}]
}
mdns
.
removeLocalService
(
context
,
localServiceInfo
,
function
(
error
,
data
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
});
```
...
...
@@ -202,27 +203,27 @@ removeLocalService(context: Context, serviceInfo: LocalServiceInfo): Promise\<Lo
| 2204008 | Service instance duplicated. |
| 2204010 | Send packet failed. |
>**错误码说明:**
>
**错误码说明:**
> 以上错误码的详细介绍参见[MDNS错误码](../errorcodes/errorcode-net-mdns.md)。
**示例:**
```
js
let
localServiceInfo
=
{
serviceType
:
"
_print._tcp
"
,
serviceName
:
"
servicename
"
,
port
:
5555
,
host
:
{
address
:
"
10.14.**.***
"
,
},
serviceAttribute
:
[{
key
:
"
111
"
,
value
:
[
1
]
}]
serviceType
:
"
_print._tcp
"
,
serviceName
:
"
servicename
"
,
port
:
5555
,
host
:
{
address
:
"
10.14.**.***
"
,
},
serviceAttribute
:
[{
key
:
"
111
"
,
value
:
[
1
]
}]
}
mdns
.
removeLocalService
(
context
,
localServiceInfo
).
then
(
function
(
data
)
{
console
.
log
(
JSON
.
stringify
(
data
))
console
.
log
(
JSON
.
stringify
(
data
))
});
```
...
...
@@ -282,28 +283,28 @@ resolveLocalService(context: Context, serviceInfo: LocalServiceInfo, callback: A
| 2204006 | Request timeout. |
| 2204010 | Send packet failed. |
>**错误码说明:**
>
**错误码说明:**
> 以上错误码的详细介绍参见[MDNS错误码](../errorcodes/errorcode-net-mdns.md)。
**示例:**
```
js
let
localServiceInfo
=
{
serviceType
:
"
_print._tcp
"
,
serviceName
:
"
servicename
"
,
port
:
5555
,
host
:
{
address
:
"
10.14.**.***
"
,
},
serviceAttribute
:
[{
key
:
"
111
"
,
value
:
[
1
]
}]
serviceType
:
"
_print._tcp
"
,
serviceName
:
"
servicename
"
,
port
:
5555
,
host
:
{
address
:
"
10.14.**.***
"
,
},
serviceAttribute
:
[{
key
:
"
111
"
,
value
:
[
1
]
}]
}
mdns
.
resolveLocalService
(
context
,
localServiceInfo
,
function
(
error
,
data
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
});
```
...
...
@@ -339,27 +340,27 @@ resolveLocalService(context: Context, serviceInfo: LocalServiceInfo): Promise\<L
| 2204006 | Request timeout. |
| 2204010 | Send packet failed. |
>**错误码说明:**
>
**错误码说明:**
> 以上错误码的详细介绍参见[MDNS错误码](../errorcodes/errorcode-net-mdns.md)。
**示例:**
```
js
let
localServiceInfo
=
{
serviceType
:
"
_print._tcp
"
,
serviceName
:
"
servicename
"
,
port
:
5555
,
host
:
{
address
:
"
10.14.**.***
"
,
},
serviceAttribute
:
[{
key
:
"
111
"
,
value
:
[
1
]
}]
serviceType
:
"
_print._tcp
"
,
serviceName
:
"
servicename
"
,
port
:
5555
,
host
:
{
address
:
"
10.14.**.***
"
,
},
serviceAttribute
:
[{
key
:
"
111
"
,
value
:
[
1
]
}]
}
mdns
.
resolveLocalService
(
context
,
localServiceInfo
).
then
(
function
(
data
)
{
console
.
log
(
JSON
.
stringify
(
data
));
console
.
log
(
JSON
.
stringify
(
data
));
})
```
...
...
@@ -420,7 +421,7 @@ let discoveryService = mdns.createDiscoveryService(context, serviceType);
discoveryService
.
startSearchingMDNS
();
discoveryService
.
on
(
'
discoveryStart
'
,
(
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
data
));
console
.
log
(
JSON
.
stringify
(
data
));
});
discoveryService
.
stopSearchingMDNS
();
...
...
@@ -449,7 +450,7 @@ let discoveryService = mdns.createDiscoveryService(context, serviceType);
discoveryService
.
startSearchingMDNS
();
discoveryService
.
on
(
'
discoveryStop
'
,
(
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
data
));
console
.
log
(
JSON
.
stringify
(
data
));
});
discoveryService
.
stopSearchingMDNS
();
...
...
@@ -478,7 +479,7 @@ let discoveryService = mdns.createDiscoveryService(context, serviceType);
discoveryService
.
startSearchingMDNS
();
discoveryService
.
on
(
'
serviceFound
'
,
(
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
data
));
console
.
log
(
JSON
.
stringify
(
data
));
});
discoveryService
.
stopSearchingMDNS
();
...
...
@@ -507,7 +508,7 @@ let discoveryService = mdns.createDiscoveryService(context, serviceType);
discoveryService
.
startSearchingMDNS
();
discoveryService
.
on
(
'
serviceLost
'
,
(
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
data
));
console
.
log
(
JSON
.
stringify
(
data
));
});
discoveryService
.
stopSearchingMDNS
();
...
...
zh-cn/application-dev/reference/apis/js-apis-net-policy.md
浏览文件 @
732ecacf
...
...
@@ -43,10 +43,12 @@ setBackgroundPolicy(isAllowed: boolean, callback: AsyncCallback\<void>): void
```
js
policy
.
setBackgroundPolicy
(
Boolean
(
Number
.
parseInt
(
this
.
isBoolean
))),
(
error
,
data
)
=>
{
this
.
callBack
(
error
,
data
);
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
});
this
.
callBack
(
error
,
data
);
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
}
)
;
```
## policy.setBackgroundPolicy
...
...
@@ -84,9 +86,9 @@ setBackgroundPolicy(isAllowed: boolean): Promise\<void>
**示例:**
```
js
policy
.
setBackgroundPolicy
(
Boolean
(
Number
.
parseInt
(
this
.
isBoolean
))).
then
(
function
(
error
,
data
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
policy
.
setBackgroundPolicy
(
Boolean
(
Number
.
parseInt
(
this
.
isBoolean
))).
then
(
function
(
error
,
data
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
})
```
...
...
@@ -118,9 +120,9 @@ isBackgroundAllowed(callback: AsyncCallback\<boolean>): void
```
js
policy
.
isBackgroundAllowed
((
error
,
data
)
=>
{
this
.
callBack
(
error
,
data
);
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
this
.
callBack
(
error
,
data
);
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
});
```
...
...
@@ -151,9 +153,9 @@ isBackgroundAllowed(): Promise\<boolean>;
**示例:**
```
js
policy
.
isBackgroundAllowed
().
then
(
function
(
error
,
data
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
policy
.
isBackgroundAllowed
().
then
(
function
(
error
,
data
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
})
```
...
...
@@ -190,10 +192,10 @@ setPolicyByUid(uid: number, policy: NetUidPolicy, callback: AsyncCallback\<void>
```
js
let
param
=
{
uid
:
Number
.
parseInt
(
this
.
firstParam
),
policy
:
Number
.
parseInt
(
this
.
currentNetUidPolicy
)
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
);
this
.
callBack
(
error
,
data
);
});
```
...
...
@@ -234,11 +236,11 @@ setPolicyByUid(uid: number, policy: NetUidPolicy): Promise\<void>;
```
js
let
param
=
{
uid
:
Number
.
parseInt
(
this
.
firstParam
),
policy
:
Number
.
parseInt
(
this
.
currentNetUidPolicy
)
uid
:
Number
.
parseInt
(
this
.
firstParam
),
policy
:
Number
.
parseInt
(
this
.
currentNetUidPolicy
)
}
policy
.
setPolicyByUid
(
Number
.
parseInt
(
this
.
firstParam
),
Number
.
parseInt
(
this
.
currentNetUidPolicy
)).
then
(
function
(
error
,
data
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
policy
.
setPolicyByUid
(
Number
.
parseInt
(
this
.
firstParam
),
Number
.
parseInt
(
this
.
currentNetUidPolicy
)).
then
(
function
(
error
,
data
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
})
```
...
...
@@ -274,7 +276,7 @@ getPolicyByUid(uid: number, callback: AsyncCallback\<NetUidPolicy>): void
```
js
policy
.
getPolicyByUid
(
Number
.
parseInt
(
this
.
firstParam
),
(
error
,
data
)
=>
{
this
.
callBack
(
error
,
data
);
this
.
callBack
(
error
,
data
);
});
```
...
...
@@ -313,9 +315,9 @@ getPolicyByUid(uid: number): Promise\<NetUidPolicy>;
**示例:**
```
js
policy
.
getPolicyByUid
(
Number
.
parseInt
(
this
.
firstParam
)).
then
(
function
(
error
,
data
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
policy
.
getPolicyByUid
(
Number
.
parseInt
(
this
.
firstParam
)).
then
(
function
(
error
,
data
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
})
```
...
...
@@ -351,7 +353,7 @@ getUidsByPolicy(policy: NetUidPolicy, callback: AsyncCallback\<Array\<number>>):
```
js
policy
.
getUidsByPolicy
(
Number
.
parseInt
(
this
.
currentNetUidPolicy
),
(
error
,
data
)
=>
{
this
.
callBack
(
error
,
data
);
this
.
callBack
(
error
,
data
);
});
```
...
...
@@ -390,9 +392,9 @@ function getUidsByPolicy(policy: NetUidPolicy): Promise\<Array\<number>>;
**示例:**
```
js
policy
.
getUidsByPolicy
(
Number
.
parseInt
(
this
.
firstParam
)).
then
(
function
(
error
,
data
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
policy
.
getUidsByPolicy
(
Number
.
parseInt
(
this
.
firstParam
)).
then
(
function
(
error
,
data
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
})
```
...
...
@@ -425,7 +427,7 @@ getNetQuotaPolicies(callback: AsyncCallback\<Array\<NetQuotaPolicy>>): void
```
js
policy
.
getNetQuotaPolicies
((
error
,
data
)
=>
{
this
.
callBack
(
error
,
data
);
this
.
callBack
(
error
,
data
);
});
```
...
...
@@ -456,9 +458,9 @@ getNetQuotaPolicies(): Promise\<Array\<NetQuotaPolicy>>;
**示例:**
```
js
policy
.
getNetQuotaPolicies
().
then
(
function
(
error
,
data
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
policy
.
getNetQuotaPolicies
().
then
(
function
(
error
,
data
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
})
```
...
...
@@ -493,12 +495,22 @@ setNetQuotaPolicies(quotaPolicies: Array\<NetQuotaPolicy>, callback: AsyncCallba
**示例:**
```
js
let
param
=
{
netType
:
Number
.
parseInt
(
this
.
netType
),
iccid
:
this
.
iccid
,
ident
:
this
.
ident
,
periodDuration
:
this
.
periodDuration
,
warningBytes
:
Number
.
parseInt
(
this
.
warningBytes
),
limitBytes
:
Number
.
parseInt
(
this
.
limitBytes
),
lastWarningRemind
:
this
.
lastWarningRemind
,
lastLimitRemind
:
this
.
lastLimitRemind
,
metered
:
Boolean
(
Number
.
parseInt
(
this
.
metered
)),
limitAction
:
this
.
limitAction
};
let
param
=
{
netType
:
Number
.
parseInt
(
this
.
netType
),
iccid
:
this
.
iccid
,
ident
:
this
.
ident
,
periodDuration
:
this
.
periodDuration
,
warningBytes
:
Number
.
parseInt
(
this
.
warningBytes
),
limitBytes
:
Number
.
parseInt
(
this
.
limitBytes
),
lastWarningRemind
:
this
.
lastWarningRemind
,
lastLimitRemind
:
this
.
lastLimitRemind
,
metered
:
Boolean
(
Number
.
parseInt
(
this
.
metered
)),
limitAction
:
this
.
limitAction
};
this
.
netQuotaPolicyList
.
push
(
param
);
policy
.
setNetQuotaPolicies
(
this
.
netQuotaPolicyList
,
(
error
,
data
)
=>
{
this
.
callBack
(
error
,
data
);
this
.
callBack
(
error
,
data
);
});
```
...
...
@@ -537,13 +549,23 @@ setNetQuotaPolicies(quotaPolicies: Array\<NetQuotaPolicy>): Promise\<void>;
**示例:**
```
js
let
param
=
{
netType
:
Number
.
parseInt
(
this
.
netType
),
iccid
:
this
.
iccid
,
ident
:
this
.
ident
,
periodDuration
:
this
.
periodDuration
,
warningBytes
:
Number
.
parseInt
(
this
.
warningBytes
),
limitBytes
:
Number
.
parseInt
(
this
.
limitBytes
),
lastWarningRemind
:
this
.
lastWarningRemind
,
lastLimitRemind
:
this
.
lastLimitRemind
,
metered
:
Boolean
(
Number
.
parseInt
(
this
.
metered
)),
limitAction
:
this
.
limitAction
};
let
param
=
{
netType
:
Number
.
parseInt
(
this
.
netType
),
iccid
:
this
.
iccid
,
ident
:
this
.
ident
,
periodDuration
:
this
.
periodDuration
,
warningBytes
:
Number
.
parseInt
(
this
.
warningBytes
),
limitBytes
:
Number
.
parseInt
(
this
.
limitBytes
),
lastWarningRemind
:
this
.
lastWarningRemind
,
lastLimitRemind
:
this
.
lastLimitRemind
,
metered
:
Boolean
(
Number
.
parseInt
(
this
.
metered
)),
limitAction
:
this
.
limitAction
};
this
.
netQuotaPolicyList
.
push
(
param
);
policy
.
setNetQuotaPolicies
(
this
.
netQuotaPolicyList
).
then
(
function
(
error
,
data
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
policy
.
setNetQuotaPolicies
(
this
.
netQuotaPolicyList
).
then
(
function
(
error
,
data
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
})
```
...
...
@@ -579,7 +601,7 @@ restoreAllPolicies(iccid: string, callback: AsyncCallback\<void>): void
```
js
this
.
firstParam
=
iccid
;
policy
.
restoreAllPolicies
(
this
.
firstParam
,
(
error
,
data
)
=>
{
this
.
callBack
(
error
,
data
);
this
.
callBack
(
error
,
data
);
});
```
...
...
@@ -619,9 +641,9 @@ restoreAllPolicies(iccid: string): Promise\<void>;
```
js
this
.
firstParam
=
iccid
;
policy
.
restoreAllPolicies
(
this
.
firstParam
).
then
(
function
(
error
,
data
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
policy
.
restoreAllPolicies
(
this
.
firstParam
).
then
(
function
(
error
,
data
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
})
```
...
...
@@ -659,10 +681,10 @@ isUidNetAllowed(uid: number, isMetered: boolean, callback: AsyncCallback\<boolea
```
js
let
param
=
{
uid
:
Number
.
parseInt
(
this
.
firstParam
),
isMetered
:
Boolean
(
Number
.
parseInt
(
this
.
isBoolean
))
uid
:
Number
.
parseInt
(
this
.
firstParam
),
isMetered
:
Boolean
(
Number
.
parseInt
(
this
.
isBoolean
))
}
policy
.
isUidNetAllowed
(
Number
.
parseInt
(
this
.
firstParam
),
Boolean
(
Number
.
parseInt
(
this
.
isBoolean
)),
(
error
,
data
)
=>
{
this
.
callBack
(
error
,
data
);
this
.
callBack
(
error
,
data
);
});
```
...
...
@@ -704,11 +726,11 @@ isUidNetAllowed(uid: number, isMetered: boolean): Promise\<boolean>;
```
js
let
param
=
{
uid
:
Number
.
parseInt
(
this
.
firstParam
),
isMetered
:
Boolean
(
Number
.
parseInt
(
this
.
isBoolean
))
uid
:
Number
.
parseInt
(
this
.
firstParam
),
isMetered
:
Boolean
(
Number
.
parseInt
(
this
.
isBoolean
))
}
policy
.
isUidNetAllowed
(
Number
.
parseInt
(
this
.
firstParam
),
Boolean
(
Number
.
parseInt
(
this
.
isBoolean
))).
then
(
function
(
error
,
data
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
policy
.
isUidNetAllowed
(
Number
.
parseInt
(
this
.
firstParam
),
Boolean
(
Number
.
parseInt
(
this
.
isBoolean
))).
then
(
function
(
error
,
data
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
})
```
...
...
@@ -746,10 +768,10 @@ isUidNetAllowed(uid: number, iface: string, callback: AsyncCallback\<boolean>):
```
js
let
param
=
{
uid
:
Number
.
parseInt
(
this
.
firstParam
),
iface
:
this
.
secondParam
uid
:
Number
.
parseInt
(
this
.
firstParam
),
iface
:
this
.
secondParam
}
policy
.
isUidNetAllowed
(
Number
.
parseInt
(
this
.
firstParam
),
this
.
secondParam
,
(
error
,
data
)
=>
{
this
.
callBack
(
error
,
data
);
this
.
callBack
(
error
,
data
);
});
```
...
...
@@ -790,11 +812,11 @@ isUidNetAllowed(uid: number, iface: string): Promise\<boolean>;
```
js
let
param
=
{
uid
:
Number
.
parseInt
(
this
.
firstParam
),
iface
:
this
.
secondParam
uid
:
Number
.
parseInt
(
this
.
firstParam
),
iface
:
this
.
secondParam
}
policy
.
isUidNetAllowed
(
Number
.
parseInt
(
this
.
firstParam
),
this
.
secondParam
).
then
(
function
(
error
,
data
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
policy
.
isUidNetAllowed
(
Number
.
parseInt
(
this
.
firstParam
),
this
.
secondParam
).
then
(
function
(
error
,
data
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
})
```
...
...
@@ -831,10 +853,10 @@ setDeviceIdleAllowList(uid: number, isAllowed: boolean, callback: AsyncCallback\
```
js
let
param
=
{
uid
:
Number
.
parseInt
(
this
.
firstParam
),
isAllowed
:
Boolean
(
Number
.
parseInt
(
this
.
isBoolean
))
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
);
this
.
callBack
(
error
,
data
);
});
```
...
...
@@ -875,11 +897,11 @@ setDeviceIdleAllowList(uid: number, isAllowed: boolean): Promise\<void>;
```
js
let
param
=
{
uid
:
Number
.
parseInt
(
this
.
firstParam
),
isAllowed
:
Boolean
(
Number
.
parseInt
(
this
.
isBoolean
))
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
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
policy
.
setDeviceIdleAllowList
(
Number
.
parseInt
(
this
.
firstParam
),
Boolean
(
Number
.
parseInt
(
this
.
isBoolean
))).
then
(
function
(
error
,
data
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
})
```
...
...
@@ -912,7 +934,7 @@ getDeviceIdleAllowList(callback: AsyncCallback\<Array\<number>>): void
```
js
policy
.
getDeviceIdleAllowList
((
error
,
data
)
=>
{
this
.
callBack
(
error
,
data
);
this
.
callBack
(
error
,
data
);
});
```
...
...
@@ -943,9 +965,9 @@ getDeviceIdleAllowList(): Promise\<Array\<number>>;
**示例:**
```
js
policy
.
getDeviceIdleAllowList
().
then
(
function
(
error
,
data
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
policy
.
getDeviceIdleAllowList
().
then
(
function
(
error
,
data
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
})
```
...
...
@@ -981,7 +1003,7 @@ getBackgroundPolicyByUid(uid: number, callback: AsyncCallback\<NetBackgroundPoli
```
js
this
.
firstParam
=
uid
policy
.
getBackgroundPolicyByUid
(
Number
.
parseInt
(
this
.
firstParam
),
(
error
,
data
)
=>
{
this
.
callBack
(
error
,
data
);
this
.
callBack
(
error
,
data
);
});
```
...
...
@@ -1021,9 +1043,9 @@ getBackgroundPolicyByUid(uid: number): Promise\<NetBackgroundPolicy>;
```
js
this
.
firstParam
=
uid
policy
.
getBackgroundPolicyByUid
(
Number
.
parseInt
(
this
.
firstParam
)).
then
(
function
(
error
,
data
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
policy
.
getBackgroundPolicyByUid
(
Number
.
parseInt
(
this
.
firstParam
)).
then
(
function
(
error
,
data
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
})
```
...
...
@@ -1059,7 +1081,7 @@ resetPolicies(iccid: string, callback: AsyncCallback\<void>): void
```
js
this
.
firstParam
=
iccid
policy
.
resetPolicies
(
this
.
firstParam
,
(
error
,
data
)
=>
{
this
.
callBack
(
error
,
data
);
this
.
callBack
(
error
,
data
);
});
```
...
...
@@ -1098,13 +1120,13 @@ resetPolicies(iccid: string): Promise\<void>;
**示例:**
```
js
policy
.
getUidsByPolicy
(
Number
.
parseInt
(
this
.
firstParam
)).
then
(
function
(
error
,
data
)
{
policy
.
getUidsByPolicy
(
Number
.
parseInt
(
this
.
firstParam
)).
then
(
function
(
error
,
data
)
{
})
this
.
firstParam
=
iccid
policy
.
resetPolicies
(
this
.
firstParam
).
then
(
function
(
error
,
data
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
policy
.
resetPolicies
(
this
.
firstParam
).
then
(
function
(
error
,
data
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
})
```
...
...
@@ -1142,10 +1164,10 @@ updateRemindPolicy(netType: NetBearType, iccid: string, remindType: RemindType,
```
js
let
param
=
{
netType
:
Number
.
parseInt
(
this
.
netType
),
iccid
:
this
.
firstParam
,
remindType
:
this
.
currentRemindType
netType
:
Number
.
parseInt
(
this
.
netType
),
iccid
:
this
.
firstParam
,
remindType
:
this
.
currentRemindType
}
policy
.
updateRemindPolicy
(
Number
.
parseInt
(
this
.
netType
),
this
.
firstParam
,
Number
.
parseInt
(
this
.
currentRemindType
),
(
error
,
data
)
=>
{
this
.
callBack
(
error
,
data
);
this
.
callBack
(
error
,
data
);
});
```
...
...
@@ -1187,11 +1209,11 @@ updateRemindPolicy(netType: NetBearType, iccid: string, remindType: RemindType):
```
js
let
param
=
{
netType
:
Number
.
parseInt
(
this
.
netType
),
iccid
:
this
.
firstParam
,
remindType
:
this
.
currentRemindType
netType
:
Number
.
parseInt
(
this
.
netType
),
iccid
:
this
.
firstParam
,
remindType
:
this
.
currentRemindType
}
policy
.
updateRemindPolicy
(
Number
.
parseInt
(
this
.
netType
),
this
.
firstParam
,
Number
.
parseInt
(
this
.
currentRemindType
)).
then
(
function
(
error
,
data
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
policy
.
updateRemindPolicy
(
Number
.
parseInt
(
this
.
netType
),
this
.
firstParam
,
Number
.
parseInt
(
this
.
currentRemindType
)).
then
(
function
(
error
,
data
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
})
```
...
...
@@ -1228,10 +1250,10 @@ setPowerSaveAllowList(uid: number, isAllowed: boolean, callback: AsyncCallback\<
```
js
let
param
=
{
uid
:
Number
.
parseInt
(
this
.
firstParam
),
isAllowed
:
Boolean
(
Number
.
parseInt
(
this
.
isBoolean
))
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
);
this
.
callBack
(
error
,
data
);
});
```
...
...
@@ -1272,11 +1294,11 @@ setPowerSaveAllowList(uid: number, isAllowed: boolean): Promise\<void>;
```
js
let
param
=
{
uid
:
Number
.
parseInt
(
this
.
firstParam
),
isAllowed
:
Boolean
(
Number
.
parseInt
(
this
.
isBoolean
))
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
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
policy
.
setPowerSaveAllowList
(
Number
.
parseInt
(
this
.
firstParam
),
Boolean
(
Number
.
parseInt
(
this
.
isBoolean
))).
then
(
function
(
error
,
data
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
})
```
...
...
@@ -1309,7 +1331,7 @@ getPowerSaveAllowList(callback: AsyncCallback\<Array\<number>>): void
```
js
policy
.
getPowerSaveAllowList
((
error
,
data
)
=>
{
this
.
callBack
(
error
,
data
);
this
.
callBack
(
error
,
data
);
});
```
...
...
@@ -1340,9 +1362,9 @@ getPowerSaveAllowList(): Promise\<Array\<number>>;
**示例:**
```
js
policy
.
getPowerSaveAllowList
().
then
(
function
(
error
,
data
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
policy
.
getPowerSaveAllowList
().
then
(
function
(
error
,
data
)
{
console
.
log
(
JSON
.
stringify
(
error
))
console
.
log
(
JSON
.
stringify
(
data
))
})
```
...
...
@@ -1371,7 +1393,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
));
})
```
...
...
@@ -1396,7 +1418,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
));
})
```
...
...
@@ -1421,7 +1443,7 @@ on(type: "netMeteredIfacesChange", callback: Callback\<Array\<string>>): void
```
js
policy
.
on
(
'
netMeteredIfacesChange
'
,
(
data
)
=>
{
this
.
log
(
'
on netMeteredIfacesChange:
'
+
JSON
.
stringify
(
data
));
this
.
log
(
'
on netMeteredIfacesChange:
'
+
JSON
.
stringify
(
data
));
})
```
...
...
@@ -1446,7 +1468,7 @@ on(type: "netQuotaPolicyChange", callback: Callback\<Array\<NetQuotaPolicy>>): v
```
js
policy
.
on
(
'
netQuotaPolicyChange
'
,
(
data
)
=>
{
this
.
log
(
'
on netQuotaPolicyChange:
'
+
JSON
.
stringify
(
data
));
this
.
log
(
'
on netQuotaPolicyChange:
'
+
JSON
.
stringify
(
data
));
})
```
...
...
@@ -1471,7 +1493,7 @@ on(type: "netBackgroundPolicyChange", callback: Callback\<boolean>): void
```
js
policy
.
on
(
'
netBackgroundPolicyChange
'
,
(
data
)
=>
{
this
.
log
(
'
on netBackgroundPolicyChange:
'
+
JSON
.
stringify
(
data
));
this
.
log
(
'
on netBackgroundPolicyChange:
'
+
JSON
.
stringify
(
data
));
})
```
...
...
@@ -1540,10 +1562,8 @@ policy.on('netBackgroundPolicyChange', (data) => {
**系统能力**
:SystemCapability.Communication.NetManager.Core
| 参数名 | 值 | 说明 |
| ---------------------- | - | ------- |
| REMIND_TYPE_WARNING | 1 | 警告提醒 |
| REMIND_TYPE_LIMIT | 2 | 限制提醒 |
| 参数名 | 值 | 说明 | | ---------------------- | - | ------- | | REMIND_TYPE_WARNING | 1 | 警告提醒 | | REMIND_TYPE_LIMIT | 2 |
限制提醒 |
## NetUidPolicy
...
...
zh-cn/application-dev/reference/apis/js-apis-net-sharing.md
浏览文件 @
732ecacf
...
...
@@ -43,8 +43,8 @@ isSharingSupported(callback: AsyncCallback\<boolean>): void
```
js
sharing
.
isSharingSupported
((
error
,
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
error
));
console
.
log
(
JSON
.
stringify
(
data
));
console
.
log
(
JSON
.
stringify
(
error
));
console
.
log
(
JSON
.
stringify
(
data
));
});
```
...
...
@@ -79,9 +79,9 @@ isSharingSupported(): Promise\<boolean>
```
js
sharing
.
isSharingSupported
().
then
(
data
=>
{
console
.
log
(
JSON
.
stringify
(
data
));
console
.
log
(
JSON
.
stringify
(
data
));
}).
catch
(
error
=>
{
console
.
log
(
JSON
.
stringify
(
error
));
console
.
log
(
JSON
.
stringify
(
error
));
});
```
...
...
@@ -115,8 +115,8 @@ isSharing(callback: AsyncCallback\<boolean>): void
```
js
sharing
.
isSharing
((
error
,
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
error
));
console
.
log
(
JSON
.
stringify
(
data
));
console
.
log
(
JSON
.
stringify
(
error
));
console
.
log
(
JSON
.
stringify
(
data
));
});
```
...
...
@@ -150,9 +150,9 @@ isSharing(): Promise\<boolean>
```
js
sharing
.
isSharing
().
then
(
data
=>
{
console
.
log
(
JSON
.
stringify
(
data
));
console
.
log
(
JSON
.
stringify
(
data
));
}).
catch
(
error
=>
{
console
.
log
(
JSON
.
stringify
(
error
));
console
.
log
(
JSON
.
stringify
(
error
));
});
```
...
...
@@ -194,9 +194,10 @@ startSharing(type: SharingIfaceType, callback: AsyncCallback\<void>): void
```
js
import
SharingIfaceType
from
'
@ohos.net.sharing
'
let
SHARING_WIFI
=
0
;
sharing
.
startSharing
(
SHARING_WIFI
,
(
error
)
=>
{
console
.
log
(
JSON
.
stringify
(
error
));
console
.
log
(
JSON
.
stringify
(
error
));
});
```
...
...
@@ -243,11 +244,12 @@ startSharing(type: SharingIfaceType): Promise\<void>
```
js
import
SharingIfaceType
from
'
@ohos.net.sharing
'
let
SHARING_WIFI
=
0
;
sharing
.
startSharing
(
SHARING_WIFI
).
then
(()
=>
{
console
.
log
(
"
start wifi sharing successful
"
);
console
.
log
(
"
start wifi sharing successful
"
);
}).
catch
(
error
=>
{
console
.
log
(
"
start wifi sharing failed
"
);
console
.
log
(
"
start wifi sharing failed
"
);
});
```
...
...
@@ -287,9 +289,10 @@ stopSharing(type: SharingIfaceType, callback: AsyncCallback\<void>): void
```
js
import
SharingIfaceType
from
'
@ohos.net.sharing
'
let
SHARING_WIFI
=
0
;
sharing
.
stopSharing
(
SHARING_WIFI
,
(
error
)
=>
{
console
.
log
(
JSON
.
stringify
(
error
));
console
.
log
(
JSON
.
stringify
(
error
));
});
```
...
...
@@ -334,11 +337,12 @@ stopSharing(type: SharingIfaceType): Promise\<void>
```
js
import
SharingIfaceType
from
'
@ohos.net.sharing
'
let
SHARING_WIFI
=
0
;
sharing
.
stopSharing
(
SHARING_WIFI
).
then
(()
=>
{
console
.
log
(
"
stop wifi sharing successful
"
);
console
.
log
(
"
stop wifi sharing successful
"
);
}).
catch
(
error
=>
{
console
.
log
(
"
stop wifi sharing failed
"
);
console
.
log
(
"
stop wifi sharing failed
"
);
});
```
...
...
@@ -372,8 +376,8 @@ getStatsRxBytes(callback: AsyncCallback\<number>): void
```
js
sharing
.
getStatsRxBytes
((
error
,
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
error
));
console
.
log
(
JSON
.
stringify
(
data
));
console
.
log
(
JSON
.
stringify
(
error
));
console
.
log
(
JSON
.
stringify
(
data
));
});
```
...
...
@@ -407,9 +411,9 @@ getStatsRxBytes(): Promise\<number>
```
js
sharing
.
getStatsRxBytes
().
then
(
data
=>
{
console
.
log
(
JSON
.
stringify
(
data
));
console
.
log
(
JSON
.
stringify
(
data
));
}).
catch
(
error
=>
{
console
.
log
(
JSON
.
stringify
(
error
));
console
.
log
(
JSON
.
stringify
(
error
));
});
```
...
...
@@ -443,8 +447,8 @@ getStatsTxBytes(callback: AsyncCallback\<number>): void
```
js
sharing
.
getStatsTxBytes
((
error
,
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
error
));
console
.
log
(
JSON
.
stringify
(
data
));
console
.
log
(
JSON
.
stringify
(
error
));
console
.
log
(
JSON
.
stringify
(
data
));
});
```
...
...
@@ -478,9 +482,9 @@ getStatsTxBytes(): Promise\<number>
```
js
sharing
.
getStatsTxBytes
().
then
(
data
=>
{
console
.
log
(
JSON
.
stringify
(
data
));
console
.
log
(
JSON
.
stringify
(
data
));
}).
catch
(
error
=>
{
console
.
log
(
JSON
.
stringify
(
error
));
console
.
log
(
JSON
.
stringify
(
error
));
});
```
...
...
@@ -514,8 +518,8 @@ getStatsTotalBytes(callback: AsyncCallback\<number>): void
```
js
sharing
.
getStatsTotalBytes
((
error
,
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
error
));
console
.
log
(
JSON
.
stringify
(
data
));
console
.
log
(
JSON
.
stringify
(
error
));
console
.
log
(
JSON
.
stringify
(
data
));
});
```
...
...
@@ -549,9 +553,9 @@ getStatsTotalBytes(): Promise\<number>
```
js
sharing
.
getStatsTotalBytes
().
then
(
data
=>
{
console
.
log
(
JSON
.
stringify
(
data
));
console
.
log
(
JSON
.
stringify
(
data
));
}).
catch
(
error
=>
{
console
.
log
(
JSON
.
stringify
(
error
));
console
.
log
(
JSON
.
stringify
(
error
));
});
```
...
...
@@ -588,10 +592,11 @@ getSharingIfaces(state: SharingIfaceState, callback: AsyncCallback\<Array\<strin
```
js
import
SharingIfaceState
from
'
@ohos.net.sharing
'
let
SHARING_BLUETOOTH
=
2
;
sharing
.
getSharingIfaces
(
SHARING_BLUETOOTH
,
(
error
,
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
error
));
console
.
log
(
JSON
.
stringify
(
data
));
console
.
log
(
JSON
.
stringify
(
error
));
console
.
log
(
JSON
.
stringify
(
data
));
});
```
...
...
@@ -633,11 +638,12 @@ getSharingIfaces(state: SharingIfaceState): Promise\<Array\<string>>
```
js
import
SharingIfaceState
from
'
@ohos.net.sharing
'
let
SHARING_BLUETOOTH
=
2
;
sharing
.
getSharingIfaces
(
SHARING_BLUETOOTH
).
then
(
data
=>
{
console
.
log
(
JSON
.
stringify
(
data
));
console
.
log
(
JSON
.
stringify
(
data
));
}).
catch
(
error
=>
{
console
.
log
(
JSON
.
stringify
(
error
));
console
.
log
(
JSON
.
stringify
(
error
));
});
```
...
...
@@ -674,10 +680,11 @@ getSharingState(type: SharingIfaceType, callback: AsyncCallback\<SharingIfaceSta
```
js
import
SharingIfaceType
from
'
@ohos.net.sharing
'
let
SHARING_WIFI
=
0
;
sharing
.
getSharingState
(
SHARING_WIFI
,
(
error
,
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
error
));
console
.
log
(
JSON
.
stringify
(
data
));
console
.
log
(
JSON
.
stringify
(
error
));
console
.
log
(
JSON
.
stringify
(
data
));
});
```
...
...
@@ -719,11 +726,12 @@ getSharingState(type: SharingIfaceType): Promise\<SharingIfaceState>
```
js
import
SharingIfaceType
from
'
@ohos.net.sharing
'
let
SHARING_WIFI
=
0
;
sharing
.
getSharingState
(
SHARING_WIFI
).
then
(
data
=>
{
console
.
log
(
JSON
.
stringify
(
data
));
console
.
log
(
JSON
.
stringify
(
data
));
}).
catch
(
error
=>
{
console
.
log
(
JSON
.
stringify
(
error
));
console
.
log
(
JSON
.
stringify
(
error
));
});
```
...
...
@@ -760,10 +768,11 @@ getSharableRegexes(type: SharingIfaceType, callback: AsyncCallback\<Array\<strin
```
js
import
SharingIfaceType
from
'
@ohos.net.sharing
'
let
SHARING_WIFI
=
0
;
sharing
.
getSharableRegexes
(
SHARING_WIFI
,
(
error
,
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
error
));
console
.
log
(
JSON
.
stringify
(
data
));
console
.
log
(
JSON
.
stringify
(
error
));
console
.
log
(
JSON
.
stringify
(
data
));
});
```
...
...
@@ -805,11 +814,12 @@ getSharableRegexes(type: SharingIfaceType): Promise\<Array\<string>>
```
js
import
SharingIfaceType
from
'
@ohos.net.sharing
'
let
SHARING_WIFI
=
0
;
sharing
.
getSharableRegexes
(
SHARING_WIFI
).
then
(
data
=>
{
console
.
log
(
JSON
.
stringify
(
data
));
console
.
log
(
JSON
.
stringify
(
data
));
}).
catch
(
error
=>
{
console
.
log
(
JSON
.
stringify
(
error
));
console
.
log
(
JSON
.
stringify
(
error
));
});
```
...
...
@@ -843,7 +853,7 @@ on(type: 'sharingStateChange', callback: Callback\<boolean>): void
```
js
sharing
.
on
(
'
sharingStateChange
'
,
(
data
)
=>
{
console
.
log
(
'
on sharingStateChange:
'
+
JSON
.
stringify
(
data
));
console
.
log
(
'
on sharingStateChange:
'
+
JSON
.
stringify
(
data
));
});
```
...
...
@@ -877,13 +887,14 @@ off(type: 'sharingStateChange', callback?: Callback\<boolean>): void
```
js
sharing
.
off
(
'
sharingStateChange
'
,
(
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
data
));
console
.
log
(
JSON
.
stringify
(
data
));
});
```
## sharing.on('interfaceSharingStateChange')
on(type: 'interfaceSharingStateChange', callback: Callback
\<
{ type: SharingIfaceType, iface: string, state: SharingIfaceState }>): void
on(type: 'interfaceSharingStateChange', callback: Callback
\<
{ type: SharingIfaceType, iface: string, state:
SharingIfaceState }>): void
注册网卡网络共享状态变化事件,使用callback方式作为异步方法。
...
...
@@ -911,13 +922,14 @@ on(type: 'interfaceSharingStateChange', callback: Callback\<{ type: SharingIface
```
js
sharing
.
on
(
'
interfaceSharingStateChange
'
,
(
data
)
=>
{
console
.
log
(
'
on interfaceSharingStateChange:
'
+
JSON
.
stringify
(
data
));
console
.
log
(
'
on interfaceSharingStateChange:
'
+
JSON
.
stringify
(
data
));
});
```
## sharing.off('interfaceSharingStateChange')
off(type: 'interfaceSharingStateChange', callback?: Callback
\<
{ type: SharingIfaceType, iface: string, state: SharingIfaceState }>): void
off(type: 'interfaceSharingStateChange', callback?: Callback
\<
{ type: SharingIfaceType, iface: string, state:
SharingIfaceState }>): void
注销网卡网络共享状态变化事件,使用callback方式作为异步方法。
...
...
@@ -945,7 +957,7 @@ off(type: 'interfaceSharingStateChange', callback?: Callback\<{ type: SharingIfa
```
js
sharing
.
off
(
'
interfaceSharingStateChange
'
,
(
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
data
));
console
.
log
(
JSON
.
stringify
(
data
));
});
```
...
...
@@ -979,7 +991,7 @@ on(type: 'sharingUpstreamChange', callback: Callback\<NetHandle>): void
```
js
sharing
.
on
(
'
sharingUpstreamChange
'
,
(
data
)
=>
{
console
.
log
(
'
on sharingUpstreamChange:
'
+
JSON
.
stringify
(
data
));
console
.
log
(
'
on sharingUpstreamChange:
'
+
JSON
.
stringify
(
data
));
});
```
...
...
@@ -1013,7 +1025,7 @@ off(type: 'sharingUpstreamChange', callback?: Callback\<NetHandle>): void
```
js
sharing
.
off
(
'
sharingUpstreamChange
'
,
(
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
data
));
console
.
log
(
JSON
.
stringify
(
data
));
});
```
...
...
zh-cn/application-dev/reference/apis/js-apis-socket.md
浏览文件 @
732ecacf
...
...
@@ -65,11 +65,11 @@ bind(address: NetAddress, callback: AsyncCallback\<void\>): void
```
js
let
udp
=
socket
.
constructUDPSocketInstance
();
udp
.
bind
({
address
:
'
192.168.xx.xxx
'
,
port
:
xxxx
,
family
:
1
},
err
=>
{
if
(
err
)
{
console
.
log
(
'
bind fail
'
);
return
;
}
console
.
log
(
'
bind success
'
);
if
(
err
)
{
console
.
log
(
'
bind fail
'
);
return
;
}
console
.
log
(
'
bind success
'
);
})
```
...
...
@@ -108,9 +108,9 @@ bind(address: NetAddress): Promise\<void\>
let
udp
=
socket
.
constructUDPSocketInstance
();
let
promise
=
udp
.
bind
({
address
:
'
192.168.xx.xxx
'
,
port
:
8080
,
family
:
1
});
promise
.
then
(()
=>
{
console
.
log
(
'
bind success
'
);
console
.
log
(
'
bind success
'
);
}).
catch
(
err
=>
{
console
.
log
(
'
bind fail
'
);
console
.
log
(
'
bind fail
'
);
});
```
...
...
@@ -145,18 +145,18 @@ send(options: UDPSendOptions, callback: AsyncCallback\<void\>): void
```
js
let
udp
=
socket
.
constructUDPSocketInstance
();
udp
.
send
({
data
:
'
Hello, server!
'
,
address
:
{
address
:
'
192.168.xx.xxx
'
,
port
:
xxxx
,
family
:
1
}
data
:
'
Hello, server!
'
,
address
:
{
address
:
'
192.168.xx.xxx
'
,
port
:
xxxx
,
family
:
1
}
},
err
=>
{
if
(
err
)
{
console
.
log
(
'
send fail
'
);
return
;
}
console
.
log
(
'
send success
'
);
if
(
err
)
{
console
.
log
(
'
send fail
'
);
return
;
}
console
.
log
(
'
send success
'
);
})
```
...
...
@@ -196,17 +196,17 @@ send(options: UDPSendOptions): Promise\<void\>
```
js
let
udp
=
socket
.
constructUDPSocketInstance
();
let
promise
=
udp
.
send
({
data
:
'
Hello, server!
'
,
address
:
{
address
:
'
192.168.xx.xxx
'
,
port
:
xxxx
,
family
:
1
}
data
:
'
Hello, server!
'
,
address
:
{
address
:
'
192.168.xx.xxx
'
,
port
:
xxxx
,
family
:
1
}
});
promise
.
then
(()
=>
{
console
.
log
(
'
send success
'
);
console
.
log
(
'
send success
'
);
}).
catch
(
err
=>
{
console
.
log
(
'
send fail
'
);
console
.
log
(
'
send fail
'
);
});
```
...
...
@@ -231,11 +231,11 @@ close(callback: AsyncCallback\<void\>): void
```
js
let
udp
=
socket
.
constructUDPSocketInstance
();
udp
.
close
(
err
=>
{
if
(
err
)
{
console
.
log
(
'
close fail
'
);
return
;
}
console
.
log
(
'
close success
'
);
if
(
err
)
{
console
.
log
(
'
close fail
'
);
return
;
}
console
.
log
(
'
close success
'
);
})
```
...
...
@@ -261,9 +261,9 @@ close(): Promise\<void\>
let
udp
=
socket
.
constructUDPSocketInstance
();
let
promise
=
udp
.
close
();
promise
.
then
(()
=>
{
console
.
log
(
'
close success
'
);
console
.
log
(
'
close success
'
);
}).
catch
(
err
=>
{
console
.
log
(
'
close fail
'
);
console
.
log
(
'
close fail
'
);
});
```
...
...
@@ -297,18 +297,18 @@ getState(callback: AsyncCallback\<SocketStateBase\>): void
```
js
let
udp
=
socket
.
constructUDPSocketInstance
();
udp
.
bind
({
address
:
'
192.168.xx.xxx
'
,
port
:
xxxx
,
family
:
1
},
err
=>
{
if
(
err
)
{
console
.
log
(
'
bind fail
'
);
return
;
}
console
.
log
(
'
bind success
'
);
udp
.
getState
((
err
,
data
)
=>
{
if
(
err
)
{
console
.
log
(
'
bind
fail
'
);
return
;
console
.
log
(
'
getState
fail
'
);
return
;
}
console
.
log
(
'
bind success
'
);
udp
.
getState
((
err
,
data
)
=>
{
if
(
err
)
{
console
.
log
(
'
getState fail
'
);
return
;
}
console
.
log
(
'
getState success:
'
+
JSON
.
stringify
(
data
));
})
console
.
log
(
'
getState success:
'
+
JSON
.
stringify
(
data
));
})
})
```
...
...
@@ -337,17 +337,17 @@ getState(): Promise\<SocketStateBase\>
let
udp
=
socket
.
constructUDPSocketInstance
();
let
promise
=
udp
.
bind
({
address
:
'
192.168.xx.xxx
'
,
port
:
xxxx
,
family
:
1
});
promise
.
then
(
err
=>
{
if
(
err
)
{
console
.
log
(
'
bind fail
'
);
return
;
}
console
.
log
(
'
bind success
'
);
let
promise
=
udp
.
getState
();
promise
.
then
(
data
=>
{
console
.
log
(
'
getState success:
'
+
JSON
.
stringify
(
data
));
}).
catch
(
err
=>
{
console
.
log
(
'
getState fail
'
);
});
if
(
err
)
{
console
.
log
(
'
bind fail
'
);
return
;
}
console
.
log
(
'
bind success
'
);
let
promise
=
udp
.
getState
();
promise
.
then
(
data
=>
{
console
.
log
(
'
getState success:
'
+
JSON
.
stringify
(
data
));
}).
catch
(
err
=>
{
console
.
log
(
'
getState fail
'
);
});
});
```
...
...
@@ -383,24 +383,24 @@ setExtraOptions(options: UDPExtraOptions, callback: AsyncCallback\<void\>): void
```
js
let
udp
=
socket
.
constructUDPSocketInstance
();
udp
.
bind
({
address
:
'
192.168.xx.xxx
'
,
port
:
xxxx
,
family
:
1
},
err
=>
{
if
(
err
)
{
console
.
log
(
'
bind fail
'
);
return
;
}
console
.
log
(
'
bind success
'
);
udp
.
setExtraOptions
({
receiveBufferSize
:
1000
,
sendBufferSize
:
1000
,
reuseAddress
:
false
,
socketTimeout
:
6000
,
broadcast
:
true
},
err
=>
{
if
(
err
)
{
console
.
log
(
'
bind
fail
'
);
return
;
console
.
log
(
'
setExtraOptions
fail
'
);
return
;
}
console
.
log
(
'
bind success
'
);
udp
.
setExtraOptions
({
receiveBufferSize
:
1000
,
sendBufferSize
:
1000
,
reuseAddress
:
false
,
socketTimeout
:
6000
,
broadcast
:
true
},
err
=>
{
if
(
err
)
{
console
.
log
(
'
setExtraOptions fail
'
);
return
;
}
console
.
log
(
'
setExtraOptions success
'
);
})
console
.
log
(
'
setExtraOptions success
'
);
})
})
```
...
...
@@ -442,21 +442,21 @@ setExtraOptions(options: UDPExtraOptions): Promise\<void\>
let
udp
=
socket
.
constructUDPSocketInstance
();
let
promise
=
udp
.
bind
({
address
:
'
192.168.xx.xxx
'
,
port
:
xxxx
,
family
:
1
});
promise
.
then
(()
=>
{
console
.
log
(
'
bind success
'
);
let
promise1
=
udp
.
setExtraOptions
({
receiveBufferSize
:
1000
,
sendBufferSize
:
1000
,
reuseAddress
:
false
,
socketTimeout
:
6000
,
broadcast
:
true
});
promise1
.
then
(()
=>
{
console
.
log
(
'
setExtraOptions success
'
);
}).
catch
(
err
=>
{
console
.
log
(
'
setExtraOptions fail
'
);
});
console
.
log
(
'
bind success
'
);
let
promise1
=
udp
.
setExtraOptions
({
receiveBufferSize
:
1000
,
sendBufferSize
:
1000
,
reuseAddress
:
false
,
socketTimeout
:
6000
,
broadcast
:
true
});
promise1
.
then
(()
=>
{
console
.
log
(
'
setExtraOptions success
'
);
}).
catch
(
err
=>
{
console
.
log
(
'
setExtraOptions fail
'
);
});
}).
catch
(
err
=>
{
console
.
log
(
'
bind fail
'
);
console
.
log
(
'
bind fail
'
);
});
```
...
...
@@ -480,7 +480,7 @@ on(type: 'message', callback: Callback\<{message: ArrayBuffer, remoteInfo: Socke
```
js
let
udp
=
socket
.
constructUDPSocketInstance
();
udp
.
on
(
'
message
'
,
value
=>
{
console
.
log
(
"
on message, message:
"
+
value
.
message
+
"
, remoteInfo:
"
+
value
.
remoteInfo
);
console
.
log
(
"
on message, message:
"
+
value
.
message
+
"
, remoteInfo:
"
+
value
.
remoteInfo
);
});
```
...
...
@@ -507,7 +507,7 @@ off(type: 'message', callback?: Callback\<{message: ArrayBuffer, remoteInfo: Soc
```
js
let
udp
=
socket
.
constructUDPSocketInstance
();
let
callback
=
value
=>
{
console
.
log
(
"
on message, message:
"
+
value
.
message
+
"
, remoteInfo:
"
+
value
.
remoteInfo
);
console
.
log
(
"
on message, message:
"
+
value
.
message
+
"
, remoteInfo:
"
+
value
.
remoteInfo
);
}
udp
.
on
(
'
message
'
,
callback
);
// 可以指定传入on中的callback取消一个订阅,也可以不指定callback清空所有订阅。
...
...
@@ -535,10 +535,10 @@ on(type: 'listening' | 'close', callback: Callback\<void\>): void
```
js
let
udp
=
socket
.
constructUDPSocketInstance
();
udp
.
on
(
'
listening
'
,
()
=>
{
console
.
log
(
"
on listening success
"
);
console
.
log
(
"
on listening success
"
);
});
udp
.
on
(
'
close
'
,
()
=>
{
console
.
log
(
"
on close success
"
);
console
.
log
(
"
on close success
"
);
});
```
...
...
@@ -565,14 +565,14 @@ off(type: 'listening' | 'close', callback?: Callback\<void\>): void
```
js
let
udp
=
socket
.
constructUDPSocketInstance
();
let
callback1
=
()
=>
{
console
.
log
(
"
on listening, success
"
);
console
.
log
(
"
on listening, success
"
);
}
udp
.
on
(
'
listening
'
,
callback1
);
// 可以指定传入on中的callback取消一个订阅,也可以不指定callback清空所有订阅。
udp
.
off
(
'
listening
'
,
callback1
);
udp
.
off
(
'
listening
'
);
let
callback2
=
()
=>
{
console
.
log
(
"
on close, success
"
);
console
.
log
(
"
on close, success
"
);
}
udp
.
on
(
'
close
'
,
callback2
);
// 可以指定传入on中的callback取消一个订阅,也可以不指定callback清空所有订阅。
...
...
@@ -600,7 +600,7 @@ on(type: 'error', callback: ErrorCallback): void
```
js
let
udp
=
socket
.
constructUDPSocketInstance
();
udp
.
on
(
'
error
'
,
err
=>
{
console
.
log
(
"
on error, err:
"
+
JSON
.
stringify
(
err
))
console
.
log
(
"
on error, err:
"
+
JSON
.
stringify
(
err
))
});
```
...
...
@@ -627,7 +627,7 @@ off(type: 'error', callback?: ErrorCallback): void
```
js
let
udp
=
socket
.
constructUDPSocketInstance
();
let
callback
=
err
=>
{
console
.
log
(
"
on error, err:
"
+
JSON
.
stringify
(
err
));
console
.
log
(
"
on error, err:
"
+
JSON
.
stringify
(
err
));
}
udp
.
on
(
'
error
'
,
callback
);
// 可以指定传入on中的callback取消一个订阅,也可以不指定callback清空所有订阅。
...
...
@@ -756,11 +756,11 @@ bind(address: NetAddress, callback: AsyncCallback\<void\>): void
```
js
let
tcp
=
socket
.
constructTCPSocketInstance
();
tcp
.
bind
({
address
:
'
192.168.xx.xxx
'
,
port
:
xxxx
,
family
:
1
},
err
=>
{
if
(
err
)
{
console
.
log
(
'
bind fail
'
);
return
;
}
console
.
log
(
'
bind success
'
);
if
(
err
)
{
console
.
log
(
'
bind fail
'
);
return
;
}
console
.
log
(
'
bind success
'
);
})
```
...
...
@@ -799,9 +799,9 @@ bind(address: NetAddress): Promise\<void\>
let
tcp
=
socket
.
constructTCPSocketInstance
();
let
promise
=
tcp
.
bind
({
address
:
'
192.168.xx.xxx
'
,
port
:
xxxx
,
family
:
1
});
promise
.
then
(()
=>
{
console
.
log
(
'
bind success
'
);
console
.
log
(
'
bind success
'
);
}).
catch
(
err
=>
{
console
.
log
(
'
bind fail
'
);
console
.
log
(
'
bind fail
'
);
});
```
...
...
@@ -837,11 +837,11 @@ connect(options: TCPConnectOptions, callback: AsyncCallback\<void\>): void
```
js
let
tcp
=
socket
.
constructTCPSocketInstance
();
tcp
.
connect
({
address
:
{
address
:
'
192.168.xx.xxx
'
,
port
:
xxxx
,
family
:
1
},
timeout
:
6000
},
err
=>
{
if
(
err
)
{
console
.
log
(
'
connect fail
'
);
return
;
}
console
.
log
(
'
connect success
'
);
if
(
err
)
{
console
.
log
(
'
connect fail
'
);
return
;
}
console
.
log
(
'
connect success
'
);
})
```
...
...
@@ -880,9 +880,9 @@ connect(options: TCPConnectOptions): Promise\<void\>
let
tcp
=
socket
.
constructTCPSocketInstance
();
let
promise
=
tcp
.
connect
({
address
:
{
address
:
'
192.168.xx.xxx
'
,
port
:
xxxx
,
family
:
1
},
timeout
:
6000
});
promise
.
then
(()
=>
{
console
.
log
(
'
connect success
'
)
console
.
log
(
'
connect success
'
)
}).
catch
(
err
=>
{
console
.
log
(
'
connect fail
'
);
console
.
log
(
'
connect fail
'
);
});
```
...
...
@@ -918,17 +918,17 @@ send(options: TCPSendOptions, callback: AsyncCallback\<void\>): void
```
js
let
tcp
=
socket
.
constructTCPSocketInstance
();
tcp
.
connect
({
address
:
{
address
:
'
192.168.xx.xxx
'
,
port
:
xxxx
,
family
:
1
},
timeout
:
6000
},
()
=>
{
console
.
log
(
'
connect success
'
);
tcp
.
send
({
data
:
'
Hello, server!
'
//此处省略encoding, 默认为utf-8编码格式
},
err
=>
{
if
(
err
)
{
console
.
log
(
'
send fail
'
);
return
;
}
console
.
log
(
'
send success
'
);
})
console
.
log
(
'
connect success
'
);
tcp
.
send
({
data
:
'
Hello, server!
'
//此处省略encoding, 默认为utf-8编码格式
},
err
=>
{
if
(
err
)
{
console
.
log
(
'
send fail
'
);
return
;
}
console
.
log
(
'
send success
'
);
})
})
```
...
...
@@ -970,17 +970,17 @@ send(options: TCPSendOptions): Promise\<void\>
let
tcp
=
socket
.
constructTCPSocketInstance
();
let
promise1
=
tcp
.
connect
({
address
:
{
address
:
'
192.168.xx.xxx
'
,
port
:
xxxx
,
family
:
1
},
timeout
:
6000
});
promise1
.
then
(()
=>
{
console
.
log
(
'
connect success
'
);
let
promise2
=
tcp
.
send
({
data
:
'
Hello, server!
'
});
promise2
.
then
(()
=>
{
console
.
log
(
'
send success
'
);
}).
catch
(
err
=>
{
console
.
log
(
'
send fail
'
);
});
console
.
log
(
'
connect success
'
);
let
promise2
=
tcp
.
send
({
data
:
'
Hello, server!
'
});
promise2
.
then
(()
=>
{
console
.
log
(
'
send success
'
);
}).
catch
(
err
=>
{
console
.
log
(
'
send fail
'
);
});
}).
catch
(
err
=>
{
console
.
log
(
'
connect fail
'
);
console
.
log
(
'
connect fail
'
);
});
```
...
...
@@ -1011,11 +1011,11 @@ close(callback: AsyncCallback\<void\>): void
```
js
let
tcp
=
socket
.
constructTCPSocketInstance
();
tcp
.
close
(
err
=>
{
if
(
err
)
{
console
.
log
(
'
close fail
'
);
return
;
}
console
.
log
(
'
close success
'
);
if
(
err
)
{
console
.
log
(
'
close fail
'
);
return
;
}
console
.
log
(
'
close success
'
);
})
```
...
...
@@ -1047,9 +1047,9 @@ close(): Promise\<void\>
let
tcp
=
socket
.
constructTCPSocketInstance
();
let
promise
=
tcp
.
close
();
promise
.
then
(()
=>
{
console
.
log
(
'
close success
'
);
console
.
log
(
'
close success
'
);
}).
catch
(
err
=>
{
console
.
log
(
'
close fail
'
);
console
.
log
(
'
close fail
'
);
});
```
...
...
@@ -1083,14 +1083,14 @@ getRemoteAddress(callback: AsyncCallback\<NetAddress\>): void
```
js
let
tcp
=
socket
.
constructTCPSocketInstance
();
tcp
.
connect
({
address
:
{
address
:
'
192.168.xx.xxx
'
,
port
:
xxxx
,
family
:
1
},
timeout
:
6000
},
()
=>
{
console
.
log
(
'
connect success
'
);
tcp
.
getRemoteAddress
((
err
,
data
)
=>
{
if
(
err
)
{
console
.
log
(
'
getRemoteAddressfail
'
);
return
;
}
console
.
log
(
'
getRemoteAddresssuccess:
'
+
JSON
.
stringify
(
data
));
})
console
.
log
(
'
connect success
'
);
tcp
.
getRemoteAddress
((
err
,
data
)
=>
{
if
(
err
)
{
console
.
log
(
'
getRemoteAddressfail
'
);
return
;
}
console
.
log
(
'
getRemoteAddresssuccess:
'
+
JSON
.
stringify
(
data
));
})
});
```
...
...
@@ -1125,15 +1125,15 @@ getRemoteAddress(): Promise\<NetAddress\>
let
tcp
=
socket
.
constructTCPSocketInstance
();
let
promise1
=
tcp
.
connect
({
address
:
{
address
:
'
192.168.xx.xxx
'
,
port
:
xxxx
,
family
:
1
},
timeout
:
6000
});
promise1
.
then
(()
=>
{
console
.
log
(
'
connect success
'
);
let
promise2
=
tcp
.
getRemoteAddress
();
promise2
.
then
(()
=>
{
console
.
log
(
'
getRemoteAddress success
'
);
}).
catch
(
err
=>
{
console
.
log
(
'
getRemoteAddressfail
'
);
});
console
.
log
(
'
connect success
'
);
let
promise2
=
tcp
.
getRemoteAddress
();
promise2
.
then
(()
=>
{
console
.
log
(
'
getRemoteAddress success
'
);
}).
catch
(
err
=>
{
console
.
log
(
'
getRemoteAddressfail
'
);
});
}).
catch
(
err
=>
{
console
.
log
(
'
connect fail
'
);
console
.
log
(
'
connect fail
'
);
});
```
...
...
@@ -1167,14 +1167,14 @@ getState(callback: AsyncCallback\<SocketStateBase\>): void
```
js
let
tcp
=
socket
.
constructTCPSocketInstance
();
let
promise
=
tcp
.
connect
({
address
:
{
address
:
'
192.168.xx.xxx
'
,
port
:
xxxx
,
family
:
1
},
timeout
:
6000
},
()
=>
{
console
.
log
(
'
connect success
'
);
tcp
.
getState
((
err
,
data
)
=>
{
if
(
err
)
{
console
.
log
(
'
getState fail
'
);
return
;
}
console
.
log
(
'
getState success:
'
+
JSON
.
stringify
(
data
));
});
console
.
log
(
'
connect success
'
);
tcp
.
getState
((
err
,
data
)
=>
{
if
(
err
)
{
console
.
log
(
'
getState fail
'
);
return
;
}
console
.
log
(
'
getState success:
'
+
JSON
.
stringify
(
data
));
});
});
```
...
...
@@ -1209,15 +1209,15 @@ getState(): Promise\<SocketStateBase\>
let
tcp
=
socket
.
constructTCPSocketInstance
();
let
promise
=
tcp
.
connect
({
address
:
{
address
:
'
192.168.xx.xxx
'
,
port
:
xxxx
,
family
:
1
},
timeout
:
6000
});
promise
.
then
(()
=>
{
console
.
log
(
'
connect success
'
);
let
promise1
=
tcp
.
getState
();
promise1
.
then
(()
=>
{
console
.
log
(
'
getState success
'
);
}).
catch
(
err
=>
{
console
.
log
(
'
getState fail
'
);
});
console
.
log
(
'
connect success
'
);
let
promise1
=
tcp
.
getState
();
promise1
.
then
(()
=>
{
console
.
log
(
'
getState success
'
);
}).
catch
(
err
=>
{
console
.
log
(
'
getState fail
'
);
});
}).
catch
(
err
=>
{
console
.
log
(
'
connect fail
'
);
console
.
log
(
'
connect fail
'
);
});
```
...
...
@@ -1253,23 +1253,23 @@ setExtraOptions(options: TCPExtraOptions, callback: AsyncCallback\<void\>): void
```
js
let
tcp
=
socket
.
constructTCPSocketInstance
();
let
promise
=
tcp
.
connect
({
address
:
{
address
:
'
192.168.xx.xxx
'
,
port
:
xxxx
,
family
:
1
},
timeout
:
6000
},
()
=>
{
console
.
log
(
'
connect success
'
);
tcp
.
setExtraOptions
({
keepAlive
:
true
,
OOBInline
:
true
,
TCPNoDelay
:
true
,
socketLinger
:
{
on
:
true
,
linger
:
10
},
receiveBufferSize
:
1000
,
sendBufferSize
:
1000
,
reuseAddress
:
true
,
socketTimeout
:
3000
,
},
err
=>
{
if
(
err
)
{
console
.
log
(
'
setExtraOptions fail
'
);
return
;
}
console
.
log
(
'
setExtraOptions success
'
);
});
console
.
log
(
'
connect success
'
);
tcp
.
setExtraOptions
({
keepAlive
:
true
,
OOBInline
:
true
,
TCPNoDelay
:
true
,
socketLinger
:
{
on
:
true
,
linger
:
10
},
receiveBufferSize
:
1000
,
sendBufferSize
:
1000
,
reuseAddress
:
true
,
socketTimeout
:
3000
,
},
err
=>
{
if
(
err
)
{
console
.
log
(
'
setExtraOptions fail
'
);
return
;
}
console
.
log
(
'
setExtraOptions success
'
);
});
});
```
...
...
@@ -1311,24 +1311,24 @@ setExtraOptions(options: TCPExtraOptions): Promise\<void\>
let
tcp
=
socket
.
constructTCPSocketInstance
();
let
promise
=
tcp
.
connect
({
address
:
{
address
:
'
192.168.xx.xxx
'
,
port
:
xxxx
,
family
:
1
},
timeout
:
6000
});
promise
.
then
(()
=>
{
console
.
log
(
'
connect success
'
);
let
promise1
=
tcp
.
setExtraOptions
({
keepAlive
:
true
,
OOBInline
:
true
,
TCPNoDelay
:
true
,
socketLinger
:
{
on
:
true
,
linger
:
10
},
receiveBufferSize
:
1000
,
sendBufferSize
:
1000
,
reuseAddress
:
true
,
socketTimeout
:
3000
,
});
promise1
.
then
(()
=>
{
console
.
log
(
'
setExtraOptions success
'
);
}).
catch
(
err
=>
{
console
.
log
(
'
setExtraOptions fail
'
);
});
console
.
log
(
'
connect success
'
);
let
promise1
=
tcp
.
setExtraOptions
({
keepAlive
:
true
,
OOBInline
:
true
,
TCPNoDelay
:
true
,
socketLinger
:
{
on
:
true
,
linger
:
10
},
receiveBufferSize
:
1000
,
sendBufferSize
:
1000
,
reuseAddress
:
true
,
socketTimeout
:
3000
,
});
promise1
.
then
(()
=>
{
console
.
log
(
'
setExtraOptions success
'
);
}).
catch
(
err
=>
{
console
.
log
(
'
setExtraOptions fail
'
);
});
}).
catch
(
err
=>
{
console
.
log
(
'
connect fail
'
);
console
.
log
(
'
connect fail
'
);
});
```
...
...
@@ -1352,7 +1352,7 @@ on(type: 'message', callback: Callback<{message: ArrayBuffer, remoteInfo: Socket
```
js
let
tcp
=
socket
.
constructTCPSocketInstance
();
tcp
.
on
(
'
message
'
,
value
=>
{
console
.
log
(
"
on message, message:
"
+
value
.
message
+
"
, remoteInfo:
"
+
value
.
remoteInfo
)
console
.
log
(
"
on message, message:
"
+
value
.
message
+
"
, remoteInfo:
"
+
value
.
remoteInfo
)
});
```
...
...
@@ -1379,7 +1379,7 @@ off(type: 'message', callback?: Callback<{message: ArrayBuffer, remoteInfo: Sock
```
js
let
tcp
=
socket
.
constructTCPSocketInstance
();
let
callback
=
value
=>
{
console
.
log
(
"
on message, message:
"
+
value
.
message
+
"
, remoteInfo:
"
+
value
.
remoteInfo
);
console
.
log
(
"
on message, message:
"
+
value
.
message
+
"
, remoteInfo:
"
+
value
.
remoteInfo
);
}
tcp
.
on
(
'
message
'
,
callback
);
// 可以指定传入on中的callback取消一个订阅,也可以不指定callback清空所有订阅。
...
...
@@ -1407,10 +1407,10 @@ on(type: 'connect' | 'close', callback: Callback\<void\>): void
```
js
let
tcp
=
socket
.
constructTCPSocketInstance
();
tcp
.
on
(
'
connect
'
,
()
=>
{
console
.
log
(
"
on connect success
"
)
console
.
log
(
"
on connect success
"
)
});
tcp
.
on
(
'
close
'
,
data
=>
{
console
.
log
(
"
on close success
"
)
console
.
log
(
"
on close success
"
)
});
```
...
...
@@ -1437,14 +1437,14 @@ off(type: 'connect' | 'close', callback?: Callback\<void\>): void
```
js
let
tcp
=
socket
.
constructTCPSocketInstance
();
let
callback1
=
()
=>
{
console
.
log
(
"
on connect success
"
);
console
.
log
(
"
on connect success
"
);
}
tcp
.
on
(
'
connect
'
,
callback1
);
// 可以指定传入on中的callback取消一个订阅,也可以不指定callback清空所有订阅。
tcp
.
off
(
'
connect
'
,
callback1
);
tcp
.
off
(
'
connect
'
);
let
callback2
=
()
=>
{
console
.
log
(
"
on close success
"
);
console
.
log
(
"
on close success
"
);
}
tcp
.
on
(
'
close
'
,
callback2
);
// 可以指定传入on中的callback取消一个订阅,也可以不指定callback清空所有订阅。
...
...
@@ -1472,7 +1472,7 @@ on(type: 'error', callback: ErrorCallback): void
```
js
let
tcp
=
socket
.
constructTCPSocketInstance
();
tcp
.
on
(
'
error
'
,
err
=>
{
console
.
log
(
"
on error, err:
"
+
JSON
.
stringify
(
err
))
console
.
log
(
"
on error, err:
"
+
JSON
.
stringify
(
err
))
});
```
...
...
@@ -1499,7 +1499,7 @@ off(type: 'error', callback?: ErrorCallback): void
```
js
let
tcp
=
socket
.
constructTCPSocketInstance
();
let
callback
=
err
=>
{
console
.
log
(
"
on error, err:
"
+
JSON
.
stringify
(
err
));
console
.
log
(
"
on error, err:
"
+
JSON
.
stringify
(
err
));
}
tcp
.
on
(
'
error
'
,
callback
);
// 可以指定传入on中的callback取消一个订阅,也可以不指定callback清空所有订阅。
...
...
@@ -1606,11 +1606,11 @@ bind(address: NetAddress, callback: AsyncCallback\<void\>): void
```
js
tls
.
bind
({
address
:
'
192.168.xx.xxx
'
,
port
:
xxxx
,
family
:
1
},
err
=>
{
if
(
err
)
{
console
.
log
(
'
bind fail
'
);
return
;
}
console
.
log
(
'
bind success
'
);
if
(
err
)
{
console
.
log
(
'
bind fail
'
);
return
;
}
console
.
log
(
'
bind success
'
);
});
```
...
...
@@ -1650,9 +1650,9 @@ bind(address: NetAddress): Promise\<void\>
```
js
let
promise
=
tls
.
bind
({
address
:
'
192.168.xx.xxx
'
,
port
:
xxxx
,
family
:
1
});
promise
.
then
(()
=>
{
console
.
log
(
'
bind success
'
);
console
.
log
(
'
bind success
'
);
}).
catch
(
err
=>
{
console
.
log
(
'
bind fail
'
);
console
.
log
(
'
bind fail
'
);
});
```
...
...
@@ -1681,18 +1681,18 @@ getState(callback: AsyncCallback\<SocketStateBase\>): void
```
js
let
promise
=
tls
.
bind
({
address
:
'
192.168.xx.xxx
'
,
port
:
xxxx
,
family
:
1
},
err
=>
{
if
(
err
)
{
console
.
log
(
'
bind fail
'
);
return
;
}
console
.
log
(
'
bind success
'
);
if
(
err
)
{
console
.
log
(
'
bind fail
'
);
return
;
}
console
.
log
(
'
bind success
'
);
});
tls
.
getState
((
err
,
data
)
=>
{
if
(
err
)
{
console
.
log
(
'
getState fail
'
);
return
;
}
console
.
log
(
'
getState success:
'
+
JSON
.
stringify
(
data
));
if
(
err
)
{
console
.
log
(
'
getState fail
'
);
return
;
}
console
.
log
(
'
getState success:
'
+
JSON
.
stringify
(
data
));
});
```
...
...
@@ -1722,15 +1722,15 @@ getState(): Promise\<SocketStateBase\>
```
js
let
promiseBind
=
tls
.
bind
({
address
:
'
192.168.xx.xxx
'
,
port
:
xxxx
,
family
:
1
});
promiseBind
.
then
(()
=>
{
console
.
log
(
'
bind success
'
);
console
.
log
(
'
bind success
'
);
}).
catch
((
err
)
=>
{
console
.
log
(
'
bind fail
'
);
console
.
log
(
'
bind fail
'
);
});
let
promise
=
tls
.
getState
();
promise
.
then
(()
=>
{
console
.
log
(
'
getState success
'
);
console
.
log
(
'
getState success
'
);
}).
catch
(
err
=>
{
console
.
log
(
'
getState fail
'
);
console
.
log
(
'
getState fail
'
);
});
```
...
...
@@ -1761,28 +1761,28 @@ setExtraOptions(options: TCPExtraOptions, callback: AsyncCallback\<void\>): void
```
js
tls
.
bind
({
address
:
'
192.168.xx.xxx
'
,
port
:
xxxx
,
family
:
1
},
err
=>
{
if
(
err
)
{
console
.
log
(
'
bind fail
'
);
return
;
}
console
.
log
(
'
bind success
'
);
if
(
err
)
{
console
.
log
(
'
bind fail
'
);
return
;
}
console
.
log
(
'
bind success
'
);
});
tls
.
setExtraOptions
({
keepAlive
:
true
,
OOBInline
:
true
,
TCPNoDelay
:
true
,
socketLinger
:
{
on
:
true
,
linger
:
10
},
receiveBufferSize
:
1000
,
sendBufferSize
:
1000
,
reuseAddress
:
true
,
socketTimeout
:
3000
,
keepAlive
:
true
,
OOBInline
:
true
,
TCPNoDelay
:
true
,
socketLinger
:
{
on
:
true
,
linger
:
10
},
receiveBufferSize
:
1000
,
sendBufferSize
:
1000
,
reuseAddress
:
true
,
socketTimeout
:
3000
,
},
err
=>
{
if
(
err
)
{
console
.
log
(
'
setExtraOptions fail
'
);
return
;
}
console
.
log
(
'
setExtraOptions success
'
);
if
(
err
)
{
console
.
log
(
'
setExtraOptions fail
'
);
return
;
}
console
.
log
(
'
setExtraOptions success
'
);
});
```
...
...
@@ -1819,26 +1819,26 @@ setExtraOptions(options: TCPExtraOptions): Promise\<void\>
```
js
tls
.
bind
({
address
:
'
192.168.xx.xxx
'
,
port
:
xxxx
,
family
:
1
},
err
=>
{
if
(
err
)
{
console
.
log
(
'
bind fail
'
);
return
;
}
console
.
log
(
'
bind success
'
);
if
(
err
)
{
console
.
log
(
'
bind fail
'
);
return
;
}
console
.
log
(
'
bind success
'
);
});
let
promise
=
tls
.
setExtraOptions
({
keepAlive
:
true
,
OOBInline
:
true
,
TCPNoDelay
:
true
,
socketLinger
:
{
on
:
true
,
linger
:
10
},
receiveBufferSize
:
1000
,
sendBufferSize
:
1000
,
reuseAddress
:
true
,
socketTimeout
:
3000
,
keepAlive
:
true
,
OOBInline
:
true
,
TCPNoDelay
:
true
,
socketLinger
:
{
on
:
true
,
linger
:
10
},
receiveBufferSize
:
1000
,
sendBufferSize
:
1000
,
reuseAddress
:
true
,
socketTimeout
:
3000
,
});
promise
.
then
(()
=>
{
console
.
log
(
'
setExtraOptions success
'
);
console
.
log
(
'
setExtraOptions success
'
);
}).
catch
(
err
=>
{
console
.
log
(
'
setExtraOptions fail
'
);
console
.
log
(
'
setExtraOptions fail
'
);
});
```
...
...
@@ -1882,57 +1882,57 @@ connect(options: TLSConnectOptions, callback: AsyncCallback\<void\>): void
```
js
let
tlsTwoWay
=
socket
.
constructTLSSocketInstance
();
// Two way authentication
tlsTwoWay
.
bind
({
address
:
'
192.168.xxx.xxx
'
,
port
:
8080
,
family
:
1
},
err
=>
{
if
(
err
)
{
console
.
log
(
'
bind fail
'
);
return
;
}
console
.
log
(
'
bind success
'
);
if
(
err
)
{
console
.
log
(
'
bind fail
'
);
return
;
}
console
.
log
(
'
bind success
'
);
});
let
options
=
{
ALPNProtocols
:
[
"
spdy/1
"
,
"
http/1.1
"
],
address
:
{
address
:
"
192.168.xx.xxx
"
,
port
:
8080
,
family
:
1
,
},
secureOptions
:
{
key
:
"
xxxx
"
,
cert
:
"
xxxx
"
,
ca
:
[
"
xxxx
"
],
password
:
"
xxxx
"
,
protocols
:
[
socket
.
Protocol
.
TLSv12
],
useRemoteCipherPrefer
:
true
,
signatureAlgorithms
:
"
rsa_pss_rsae_sha256:ECDSA+SHA256
"
,
cipherSuite
:
"
AES256-SHA256
"
,
},
ALPNProtocols
:
[
"
spdy/1
"
,
"
http/1.1
"
],
address
:
{
address
:
"
192.168.xx.xxx
"
,
port
:
8080
,
family
:
1
,
},
secureOptions
:
{
key
:
"
xxxx
"
,
cert
:
"
xxxx
"
,
ca
:
[
"
xxxx
"
],
password
:
"
xxxx
"
,
protocols
:
[
socket
.
Protocol
.
TLSv12
],
useRemoteCipherPrefer
:
true
,
signatureAlgorithms
:
"
rsa_pss_rsae_sha256:ECDSA+SHA256
"
,
cipherSuite
:
"
AES256-SHA256
"
,
},
};
tlsTwoWay
.
connect
(
options
,
(
err
,
data
)
=>
{
console
.
error
(
"
connect callback error
"
+
err
);
console
.
log
(
JSON
.
stringify
(
data
));
console
.
error
(
"
connect callback error
"
+
err
);
console
.
log
(
JSON
.
stringify
(
data
));
});
let
tlsOneWay
=
socket
.
constructTLSSocketInstance
();
// One way authentication
tlsOneWay
.
bind
({
address
:
'
192.168.xxx.xxx
'
,
port
:
8080
,
family
:
1
},
err
=>
{
if
(
err
)
{
console
.
log
(
'
bind fail
'
);
return
;
}
console
.
log
(
'
bind success
'
);
if
(
err
)
{
console
.
log
(
'
bind fail
'
);
return
;
}
console
.
log
(
'
bind success
'
);
});
let
oneWayOptions
=
{
address
:
{
address
:
"
192.168.xxx.xxx
"
,
port
:
8080
,
family
:
1
,
},
secureOptions
:
{
ca
:
[
"
xxxx
"
,
"
xxxx
"
],
cipherSuite
:
"
AES256-SHA256
"
,
},
address
:
{
address
:
"
192.168.xxx.xxx
"
,
port
:
8080
,
family
:
1
,
},
secureOptions
:
{
ca
:
[
"
xxxx
"
,
"
xxxx
"
],
cipherSuite
:
"
AES256-SHA256
"
,
},
};
tlsOneWay
.
connect
(
oneWayOptions
,
(
err
,
data
)
=>
{
console
.
error
(
"
connect callback error
"
+
err
);
console
.
log
(
JSON
.
stringify
(
data
));
console
.
error
(
"
connect callback error
"
+
err
);
console
.
log
(
JSON
.
stringify
(
data
));
});
```
...
...
@@ -1981,59 +1981,59 @@ connect(options: TLSConnectOptions): Promise\<void\>
```
js
let
tlsTwoWay
=
socket
.
constructTLSSocketInstance
();
// Two way authentication
tlsTwoWay
.
bind
({
address
:
'
192.168.xxx.xxx
'
,
port
:
8080
,
family
:
1
},
err
=>
{
if
(
err
)
{
console
.
log
(
'
bind fail
'
);
return
;
}
console
.
log
(
'
bind success
'
);
if
(
err
)
{
console
.
log
(
'
bind fail
'
);
return
;
}
console
.
log
(
'
bind success
'
);
});
let
options
=
{
ALPNProtocols
:
[
"
spdy/1
"
,
"
http/1.1
"
],
address
:
{
address
:
"
xxxx
"
,
port
:
8080
,
family
:
1
,
},
secureOptions
:
{
key
:
"
xxxx
"
,
cert
:
"
xxxx
"
,
ca
:
[
"
xxxx
"
],
password
:
"
xxxx
"
,
protocols
:
[
socket
.
Protocol
.
TLSv12
],
useRemoteCipherPrefer
:
true
,
signatureAlgorithms
:
"
rsa_pss_rsae_sha256:ECDSA+SHA256
"
,
cipherSuite
:
"
AES256-SHA256
"
,
},
ALPNProtocols
:
[
"
spdy/1
"
,
"
http/1.1
"
],
address
:
{
address
:
"
xxxx
"
,
port
:
8080
,
family
:
1
,
},
secureOptions
:
{
key
:
"
xxxx
"
,
cert
:
"
xxxx
"
,
ca
:
[
"
xxxx
"
],
password
:
"
xxxx
"
,
protocols
:
[
socket
.
Protocol
.
TLSv12
],
useRemoteCipherPrefer
:
true
,
signatureAlgorithms
:
"
rsa_pss_rsae_sha256:ECDSA+SHA256
"
,
cipherSuite
:
"
AES256-SHA256
"
,
},
};
tlsTwoWay
.
connect
(
options
).
then
(
data
=>
{
console
.
log
(
JSON
.
stringify
(
data
));
console
.
log
(
JSON
.
stringify
(
data
));
}).
catch
(
err
=>
{
console
.
error
(
err
);
console
.
error
(
err
);
});
let
tlsOneWay
=
socket
.
constructTLSSocketInstance
();
// One way authentication
tlsOneWay
.
bind
({
address
:
'
192.168.xxx.xxx
'
,
port
:
8080
,
family
:
1
},
err
=>
{
if
(
err
)
{
console
.
log
(
'
bind fail
'
);
return
;
}
console
.
log
(
'
bind success
'
);
if
(
err
)
{
console
.
log
(
'
bind fail
'
);
return
;
}
console
.
log
(
'
bind success
'
);
});
let
oneWayOptions
=
{
address
:
{
address
:
"
192.168.xxx.xxx
"
,
port
:
8080
,
family
:
1
,
},
secureOptions
:
{
ca
:
[
"
xxxx
"
,
"
xxxx
"
],
cipherSuite
:
"
AES256-SHA256
"
,
},
address
:
{
address
:
"
192.168.xxx.xxx
"
,
port
:
8080
,
family
:
1
,
},
secureOptions
:
{
ca
:
[
"
xxxx
"
,
"
xxxx
"
],
cipherSuite
:
"
AES256-SHA256
"
,
},
};
tlsOneWay
.
connect
(
oneWayOptions
).
then
(
data
=>
{
console
.
log
(
JSON
.
stringify
(
data
));
console
.
log
(
JSON
.
stringify
(
data
));
}).
catch
(
err
=>
{
console
.
error
(
err
);
console
.
error
(
err
);
});
```
...
...
@@ -2062,11 +2062,11 @@ getRemoteAddress(callback: AsyncCallback\<NetAddress\>): void
```
js
tls
.
getRemoteAddress
((
err
,
data
)
=>
{
if
(
err
)
{
console
.
log
(
'
getRemoteAddress fail
'
);
return
;
}
console
.
log
(
'
getRemoteAddress success:
'
+
JSON
.
stringify
(
data
));
if
(
err
)
{
console
.
log
(
'
getRemoteAddress fail
'
);
return
;
}
console
.
log
(
'
getRemoteAddress success:
'
+
JSON
.
stringify
(
data
));
});
```
...
...
@@ -2096,9 +2096,9 @@ getRemoteAddress(): Promise\<NetAddress\>
```
js
let
promise
=
tls
.
getRemoteAddress
();
promise
.
then
(()
=>
{
console
.
log
(
'
getRemoteAddress success
'
);
console
.
log
(
'
getRemoteAddress success
'
);
}).
catch
(
err
=>
{
console
.
log
(
'
getRemoteAddress fail
'
);
console
.
log
(
'
getRemoteAddress fail
'
);
});
```
...
...
@@ -2128,11 +2128,11 @@ getCertificate(callback: AsyncCallback\<[X509CertRawData](#x509certrawdata9)\>):
```
js
tls
.
getCertificate
((
err
,
data
)
=>
{
if
(
err
)
{
console
.
log
(
"
getCertificate callback error =
"
+
err
);
}
else
{
console
.
log
(
"
getCertificate callback =
"
+
data
);
}
if
(
err
)
{
console
.
log
(
"
getCertificate callback error =
"
+
err
);
}
else
{
console
.
log
(
"
getCertificate callback =
"
+
data
);
}
});
```
...
...
@@ -2162,9 +2162,9 @@ getCertificate():Promise\<[X509CertRawData](#x509certrawdata9)\>
```
js
tls
.
getCertificate
().
then
(
data
=>
{
console
.
log
(
data
);
console
.
log
(
data
);
}).
catch
(
err
=>
{
console
.
error
(
err
);
console
.
error
(
err
);
});
```
...
...
@@ -2193,11 +2193,11 @@ getRemoteCertificate(callback: AsyncCallback\<[X509CertRawData](#x509certrawdata
```
js
tls
.
getRemoteCertificate
((
err
,
data
)
=>
{
if
(
err
)
{
console
.
log
(
"
getRemoteCertificate callback error =
"
+
err
);
}
else
{
console
.
log
(
"
getRemoteCertificate callback =
"
+
data
);
}
if
(
err
)
{
console
.
log
(
"
getRemoteCertificate callback error =
"
+
err
);
}
else
{
console
.
log
(
"
getRemoteCertificate callback =
"
+
data
);
}
});
```
...
...
@@ -2226,9 +2226,9 @@ getRemoteCertificate():Promise\<[X509CertRawData](#x509certrawdata9)\>
```
js
tls
.
getRemoteCertificate
().
then
(
data
=>
{
console
.
log
(
data
);
console
.
log
(
data
);
}).
catch
(
err
=>
{
console
.
error
(
err
);
console
.
error
(
err
);
});
```
...
...
@@ -2258,11 +2258,11 @@ getProtocol(callback: AsyncCallback\<string\>): void
```
js
tls
.
getProtocol
((
err
,
data
)
=>
{
if
(
err
)
{
console
.
log
(
"
getProtocol callback error =
"
+
err
);
}
else
{
console
.
log
(
"
getProtocol callback =
"
+
data
);
}
if
(
err
)
{
console
.
log
(
"
getProtocol callback error =
"
+
err
);
}
else
{
console
.
log
(
"
getProtocol callback =
"
+
data
);
}
});
```
...
...
@@ -2292,9 +2292,9 @@ getProtocol():Promise\<string\>
```
js
tls
.
getProtocol
().
then
(
data
=>
{
console
.
log
(
data
);
console
.
log
(
data
);
}).
catch
(
err
=>
{
console
.
error
(
err
);
console
.
error
(
err
);
});
```
...
...
@@ -2325,11 +2325,11 @@ getCipherSuite(callback: AsyncCallback\<Array\<string\>\>): void
```
js
tls
.
getCipherSuite
((
err
,
data
)
=>
{
if
(
err
)
{
console
.
log
(
"
getCipherSuite callback error =
"
+
err
);
}
else
{
console
.
log
(
"
getCipherSuite callback =
"
+
data
);
}
if
(
err
)
{
console
.
log
(
"
getCipherSuite callback error =
"
+
err
);
}
else
{
console
.
log
(
"
getCipherSuite callback =
"
+
data
);
}
});
```
...
...
@@ -2360,9 +2360,9 @@ getCipherSuite(): Promise\<Array\<string\>\>
```
js
tls
.
getCipherSuite
().
then
(
data
=>
{
console
.
log
(
'
getCipherSuite success:
'
+
JSON
.
stringify
(
data
));
console
.
log
(
'
getCipherSuite success:
'
+
JSON
.
stringify
(
data
));
}).
catch
(
err
=>
{
console
.
error
(
err
);
console
.
error
(
err
);
});
```
...
...
@@ -2391,11 +2391,11 @@ getSignatureAlgorithms(callback: AsyncCallback\<Array\<string\>\>): void
```
js
tls
.
getSignatureAlgorithms
((
err
,
data
)
=>
{
if
(
err
)
{
console
.
log
(
"
getSignatureAlgorithms callback error =
"
+
err
);
}
else
{
console
.
log
(
"
getSignatureAlgorithms callback =
"
+
data
);
}
if
(
err
)
{
console
.
log
(
"
getSignatureAlgorithms callback error =
"
+
err
);
}
else
{
console
.
log
(
"
getSignatureAlgorithms callback =
"
+
data
);
}
});
```
...
...
@@ -2424,9 +2424,9 @@ getSignatureAlgorithms(): Promise\<Array\<string\>\>
```
js
tls
.
getSignatureAlgorithms
().
then
(
data
=>
{
console
.
log
(
"
getSignatureAlgorithms success
"
+
data
);
console
.
log
(
"
getSignatureAlgorithms success
"
+
data
);
}).
catch
(
err
=>
{
console
.
error
(
err
);
console
.
error
(
err
);
});
```
...
...
@@ -2460,11 +2460,11 @@ send(data: string, callback: AsyncCallback\<void\>): void
```
js
tls
.
send
(
"
xxxx
"
,
(
err
)
=>
{
if
(
err
)
{
console
.
log
(
"
send callback error =
"
+
err
);
}
else
{
console
.
log
(
"
send success
"
);
}
if
(
err
)
{
console
.
log
(
"
send callback error =
"
+
err
);
}
else
{
console
.
log
(
"
send success
"
);
}
});
```
...
...
@@ -2503,9 +2503,9 @@ send(data: string): Promise\<void\>
```
js
tls
.
send
(
"
xxxx
"
).
then
(()
=>
{
console
.
log
(
"
send success
"
);
console
.
log
(
"
send success
"
);
}).
catch
(
err
=>
{
console
.
error
(
err
);
console
.
error
(
err
);
});
```
...
...
@@ -2536,11 +2536,11 @@ close(callback: AsyncCallback\<void\>): void
```
js
tls
.
close
((
err
)
=>
{
if
(
err
)
{
console
.
log
(
"
close callback error =
"
+
err
);
}
else
{
console
.
log
(
"
close success
"
);
}
if
(
err
)
{
console
.
log
(
"
close callback error =
"
+
err
);
}
else
{
console
.
log
(
"
close success
"
);
}
});
```
...
...
@@ -2571,9 +2571,9 @@ close(): Promise\<void\>
```
js
tls
.
close
().
then
(()
=>
{
console
.
log
(
"
close success
"
);
console
.
log
(
"
close success
"
);
}).
catch
(
err
=>
{
console
.
error
(
err
);
console
.
error
(
err
);
});
```
...
...
zh-cn/application-dev/reference/apis/js-apis-webSocket.md
浏览文件 @
732ecacf
...
...
@@ -5,7 +5,9 @@
> 本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
使用WebSocket建立服务器与客户端的双向连接,需要先通过
[
createWebSocket
](
#websocketcreatewebsocket
)
方法创建
[
WebSocket
](
#websocket
)
对象,然后通过
[
connect
](
#connect
)
方法连接到服务器。当连接成功后,客户端会收到
[
open
](
#onopen
)
事件的回调,之后客户端就可以通过
[
send
](
#send
)
方法与服务器进行通信。当服务器发信息给客户端时,客户端会收到
[
message
](
#onmessage
)
事件的回调。当客户端不要此连接时,可以通过调用
[
close
](
#close
)
方法主动断开连接,之后客户端会收到
[
close
](
#onclose
)
事件的回调。
使用WebSocket建立服务器与客户端的双向连接,需要先通过
[
createWebSocket
](
#websocketcreatewebsocket
)
方法创建
[
WebSocket
](
#websocket
)
对象,然后通过
[
connect
](
#connect
)
方法连接到服务器。
当连接成功后,客户端会收到
[
open
](
#onopen
)
事件的回调,之后客户端就可以通过
[
send
](
#send
)
方法与服务器进行通信。
当服务器发信息给客户端时,客户端会收到
[
message
](
#onmessage
)
事件的回调。当客户端不要此连接时,可以通过调用
[
close
](
#close
)
方法主动断开连接,之后客户端会收到
[
close
](
#onclose
)
事件的回调。
若在上述任一过程中发生错误,客户端会收到
[
error
](
#onerror
)
事件的回调。
...
...
@@ -23,45 +25,45 @@ import webSocket from '@ohos.net.webSocket';
let
defaultIpAddress
=
"
ws://
"
;
let
ws
=
webSocket
.
createWebSocket
();
ws
.
on
(
'
open
'
,
(
err
,
value
)
=>
{
if
(
err
!=
undefined
)
{
console
.
log
(
JSON
.
stringify
(
err
))
return
if
(
err
!=
undefined
)
{
console
.
log
(
JSON
.
stringify
(
err
))
return
}
console
.
log
(
"
on open, status:
"
+
value
[
'
status
'
]
+
"
, message:
"
+
value
[
'
message
'
]);
// 当收到on('open')事件时,可以通过send()方法与服务器进行通信
ws
.
send
(
"
Hello, server!
"
,
(
err
,
value
)
=>
{
if
(
!
err
)
{
console
.
log
(
"
send success
"
);
}
else
{
console
.
log
(
"
send fail, err:
"
+
JSON
.
stringify
(
err
));
}
console
.
log
(
"
on open, status:
"
+
value
[
'
status
'
]
+
"
, message:
"
+
value
[
'
message
'
]);
// 当收到on('open')事件时,可以通过send()方法与服务器进行通信
ws
.
send
(
"
Hello, server!
"
,
(
err
,
value
)
=>
{
if
(
!
err
)
{
console
.
log
(
"
send success
"
);
}
else
{
console
.
log
(
"
send fail, err:
"
+
JSON
.
stringify
(
err
));
}
});
});
});
ws
.
on
(
'
message
'
,
(
err
,
value
)
=>
{
console
.
log
(
"
on message, message:
"
+
value
);
// 当收到服务器的`bye`消息时(此消息字段仅为示意,具体字段需要与服务器协商),主动断开连接
if
(
value
===
'
bye
'
)
{
ws
.
close
((
err
,
value
)
=>
{
if
(
!
err
)
{
console
.
log
(
"
close success
"
);
}
else
{
console
.
log
(
"
close fail, err is
"
+
JSON
.
stringify
(
err
));
}
});
}
console
.
log
(
"
on message, message:
"
+
value
);
// 当收到服务器的`bye`消息时(此消息字段仅为示意,具体字段需要与服务器协商),主动断开连接
if
(
value
===
'
bye
'
)
{
ws
.
close
((
err
,
value
)
=>
{
if
(
!
err
)
{
console
.
log
(
"
close success
"
);
}
else
{
console
.
log
(
"
close fail, err is
"
+
JSON
.
stringify
(
err
));
}
});
}
});
ws
.
on
(
'
close
'
,
(
err
,
value
)
=>
{
console
.
log
(
"
on close, code is
"
+
value
.
code
+
"
, reason is
"
+
value
.
reason
);
console
.
log
(
"
on close, code is
"
+
value
.
code
+
"
, reason is
"
+
value
.
reason
);
});
ws
.
on
(
'
error
'
,
(
err
)
=>
{
console
.
log
(
"
on error, error:
"
+
JSON
.
stringify
(
err
));
console
.
log
(
"
on error, error:
"
+
JSON
.
stringify
(
err
));
});
ws
.
connect
(
defaultIpAddress
,
(
err
,
value
)
=>
{
if
(
!
err
)
{
console
.
log
(
"
connect success
"
);
}
else
{
console
.
log
(
"
connect fail, err:
"
+
JSON
.
stringify
(
err
));
}
if
(
!
err
)
{
console
.
log
(
"
connect success
"
);
}
else
{
console
.
log
(
"
connect fail, err:
"
+
JSON
.
stringify
(
err
));
}
});
```
...
...
@@ -122,11 +124,11 @@ connect(url: string, callback: AsyncCallback\<boolean\>): void
let
ws
=
webSocket
.
createWebSocket
();
let
url
=
"
ws://
"
ws
.
connect
(
url
,
(
err
,
value
)
=>
{
if
(
!
err
)
{
console
.
log
(
"
connect success
"
);
}
else
{
console
.
log
(
"
connect fail, err:
"
+
JSON
.
stringify
(
err
))
}
if
(
!
err
)
{
console
.
log
(
"
connect success
"
);
}
else
{
console
.
log
(
"
connect fail, err:
"
+
JSON
.
stringify
(
err
))
}
});
```
...
...
@@ -164,16 +166,16 @@ connect(url: string, options: WebSocketRequestOptions, callback: AsyncCallback\<
let
ws
=
webSocket
.
createWebSocket
();
let
url
=
"
ws://
"
ws
.
connect
(
url
,
{
header
:
{
"
key
"
:
"
value
"
,
"
key2
"
:
"
value2
"
}
header
:
{
"
key
"
:
"
value
"
,
"
key2
"
:
"
value2
"
}
},
(
err
,
value
)
=>
{
if
(
!
err
)
{
console
.
log
(
"
connect success
"
);
}
else
{
console
.
log
(
"
connect fail, err:
"
+
JSON
.
stringify
(
err
))
}
if
(
!
err
)
{
console
.
log
(
"
connect success
"
);
}
else
{
console
.
log
(
"
connect fail, err:
"
+
JSON
.
stringify
(
err
))
}
});
```
...
...
@@ -217,9 +219,9 @@ let ws = webSocket.createWebSocket();
let
url
=
"
ws://
"
let
promise
=
ws
.
connect
(
url
);
promise
.
then
((
value
)
=>
{
console
.
log
(
"
connect success
"
)
console
.
log
(
"
connect success
"
)
}).
catch
((
err
)
=>
{
console
.
log
(
"
connect fail, error:
"
+
JSON
.
stringify
(
err
))
console
.
log
(
"
connect fail, error:
"
+
JSON
.
stringify
(
err
))
});
```
...
...
@@ -253,13 +255,13 @@ send(data: string | ArrayBuffer, callback: AsyncCallback\<boolean\>): void
let
ws
=
webSocket
.
createWebSocket
();
let
url
=
"
ws://
"
ws
.
connect
(
url
,
(
err
,
value
)
=>
{
ws
.
send
(
"
Hello, server!
"
,
(
err
,
value
)
=>
{
if
(
!
err
)
{
console
.
log
(
"
send success
"
);
}
else
{
console
.
log
(
"
send fail, err:
"
+
JSON
.
stringify
(
err
))
}
});
ws
.
send
(
"
Hello, server!
"
,
(
err
,
value
)
=>
{
if
(
!
err
)
{
console
.
log
(
"
send success
"
);
}
else
{
console
.
log
(
"
send fail, err:
"
+
JSON
.
stringify
(
err
))
}
});
});
```
...
...
@@ -298,12 +300,12 @@ send(data: string | ArrayBuffer): Promise\<boolean\>
let
ws
=
webSocket
.
createWebSocket
();
let
url
=
"
ws://
"
ws
.
connect
(
url
,
(
err
,
value
)
=>
{
let
promise
=
ws
.
send
(
"
Hello, server!
"
);
promise
.
then
((
value
)
=>
{
console
.
log
(
"
send success
"
)
}).
catch
((
err
)
=>
{
console
.
log
(
"
send fail, error:
"
+
JSON
.
stringify
(
err
))
});
let
promise
=
ws
.
send
(
"
Hello, server!
"
);
promise
.
then
((
value
)
=>
{
console
.
log
(
"
send success
"
)
}).
catch
((
err
)
=>
{
console
.
log
(
"
send fail, error:
"
+
JSON
.
stringify
(
err
))
});
});
```
...
...
@@ -335,11 +337,11 @@ close(callback: AsyncCallback\<boolean\>): void
```
js
let
ws
=
webSocket
.
createWebSocket
();
ws
.
close
((
err
,
value
)
=>
{
if
(
!
err
)
{
console
.
log
(
"
close success
"
)
}
else
{
console
.
log
(
"
close fail, err is
"
+
JSON
.
stringify
(
err
))
}
if
(
!
err
)
{
console
.
log
(
"
close success
"
)
}
else
{
console
.
log
(
"
close fail, err is
"
+
JSON
.
stringify
(
err
))
}
});
```
...
...
@@ -372,14 +374,14 @@ close(options: WebSocketCloseOptions, callback: AsyncCallback\<boolean\>): void
```
js
let
ws
=
webSocket
.
createWebSocket
();
ws
.
close
({
code
:
1000
,
reason
:
"
your reason
"
code
:
1000
,
reason
:
"
your reason
"
},
(
err
,
value
)
=>
{
if
(
!
err
)
{
console
.
log
(
"
close success
"
)
}
else
{
console
.
log
(
"
close fail, err is
"
+
JSON
.
stringify
(
err
))
}
if
(
!
err
)
{
console
.
log
(
"
close success
"
)
}
else
{
console
.
log
(
"
close fail, err is
"
+
JSON
.
stringify
(
err
))
}
});
```
...
...
@@ -417,13 +419,13 @@ close(options?: WebSocketCloseOptions): Promise\<boolean\>
```
js
let
ws
=
webSocket
.
createWebSocket
();
let
promise
=
ws
.
close
({
code
:
1000
,
reason
:
"
your reason
"
code
:
1000
,
reason
:
"
your reason
"
});
promise
.
then
((
value
)
=>
{
console
.
log
(
"
close success
"
)
console
.
log
(
"
close success
"
)
}).
catch
((
err
)
=>
{
console
.
log
(
"
close fail, err is
"
+
JSON
.
stringify
(
err
))
console
.
log
(
"
close fail, err is
"
+
JSON
.
stringify
(
err
))
});
```
...
...
@@ -447,7 +449,7 @@ on(type: 'open', callback: AsyncCallback\<Object\>): void
```
js
let
ws
=
webSocket
.
createWebSocket
();
ws
.
on
(
'
open
'
,
(
err
,
value
)
=>
{
console
.
log
(
"
on open, status:
"
+
value
[
'
status
'
]
+
"
, message:
"
+
value
[
'
message
'
]);
console
.
log
(
"
on open, status:
"
+
value
[
'
status
'
]
+
"
, message:
"
+
value
[
'
message
'
]);
});
```
...
...
@@ -474,7 +476,7 @@ off(type: 'open', callback?: AsyncCallback\<Object\>): void
```
js
let
ws
=
webSocket
.
createWebSocket
();
let
callback1
=
(
err
,
value
)
=>
{
console
.
log
(
"
on open, status:
"
+
value
[
'
status
'
]
+
"
, message:
"
+
value
[
'
message
'
]);
console
.
log
(
"
on open, status:
"
+
value
[
'
status
'
]
+
"
, message:
"
+
value
[
'
message
'
]);
}
ws
.
on
(
'
open
'
,
callback1
);
// 可以指定传入on中的callback取消一个订阅,也可以不指定callback清空所有订阅
...
...
@@ -504,7 +506,7 @@ on(type: 'message', callback: AsyncCallback\<string | ArrayBuffer\>): void
```
js
let
ws
=
webSocket
.
createWebSocket
();
ws
.
on
(
'
message
'
,
(
err
,
value
)
=>
{
console
.
log
(
"
on message, message:
"
+
value
);
console
.
log
(
"
on message, message:
"
+
value
);
});
```
...
...
@@ -554,7 +556,7 @@ on(type: 'close', callback: AsyncCallback\<{ code: number, reason: string }\>):
```
js
let
ws
=
webSocket
.
createWebSocket
();
ws
.
on
(
'
close
'
,
(
err
,
value
)
=>
{
console
.
log
(
"
on close, code is
"
+
value
.
code
+
"
, reason is
"
+
value
.
reason
);
console
.
log
(
"
on close, code is
"
+
value
.
code
+
"
, reason is
"
+
value
.
reason
);
});
```
...
...
@@ -603,7 +605,7 @@ on(type: 'error', callback: ErrorCallback): void
```
js
let
ws
=
webSocket
.
createWebSocket
();
ws
.
on
(
'
error
'
,
(
err
)
=>
{
console
.
log
(
"
on error, error:
"
+
JSON
.
stringify
(
err
))
console
.
log
(
"
on error, error:
"
+
JSON
.
stringify
(
err
))
});
```
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录