Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
d727e29e
D
Docs
项目概览
OpenHarmony
/
Docs
大约 1 年 前同步成功
通知
159
Star
292
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看板
体验新版 GitCode,发现更多精彩内容 >>
提交
d727e29e
编写于
4月 18, 2023
作者:
L
liyufan
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add MDNS code
Signed-off-by:
N
liyufan
<
liyufan5@huawei.com
>
上级
ac494246
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
462 addition
and
12 deletion
+462
-12
zh-cn/application-dev/connectivity/net-mdns.md
zh-cn/application-dev/connectivity/net-mdns.md
+167
-0
zh-cn/application-dev/reference/apis/js-apis-net-mdns.md
zh-cn/application-dev/reference/apis/js-apis-net-mdns.md
+295
-12
未找到文件。
zh-cn/application-dev/connectivity/net-mdns.md
0 → 100644
浏览文件 @
d727e29e
# MDNS管理
## 简介
MDNS即多播DNS(Multicast DNS),提供局域网内的本地服务添加、移除、发现、解析等能力。
> **说明:**
> 为了保证应用的运行效率,大部分API调用都是异步的,对于异步调用的API均提供了callback和Promise两种方式,以下示例均采用callback函数,更多方式可以查阅[API参考](../reference/apis/js-apis-net-mdns.md)。
## 基本概念
-
本地服务:局域网内服务的提供方,比如打印机、扫描器等。
## 约束
-
开发语言:C++ JS
-
系统:linux内核
-
本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
## 场景介绍
MDNS管理的典型场景有:
-
管理本地服务,通过对本地服务的创建,删除和解析等,管理本地服务。
-
发现本地服务,通过DiscoveryService对象,对指定类型的本地服务状态变化进行监听。
以下分别介绍具体开发方式。
## 接口说明
完整的JS API说明以及实例代码请参考:
[
以太网连接
](
../reference/apis/js-apis-net-mdns.md
)
。
| 类型 | 接口 | 功能说明 |
| ---- | ---- | ---- |
| ohos.net.mdns | addLocalService(context: Context, serviceInfo: LocalServiceInfo, callback: AsyncCallback
\<
LocalServiceInfo>): void | 添加一个mDNS服务,使用callback方式作为异步方法。 |
| ohos.net.mdns | removeLocalService(context: Context, serviceInfo: LocalServiceInfo, callback: AsyncCallback
\<
LocalServiceInfo>): void | 移除一个mDNS服务,使用callback方式作为异步方法。 |
| ohos.net.mdns | createDiscoveryService(context: Context, serviceType: string): DiscoveryService | 返回一个DiscoveryService对象,该对象用于发现指定服务类型的mDNS服务。 |
| ohos.net.mdns | resolveLocalService(context: Context, serviceInfo: LocalServiceInfo, callback: AsyncCallback
\<
LocalServiceInfo>): void | 解析一个mDNS服务,使用callback方式作为异步方法。 |
| ohos.net.mdns.DiscoveryService | startSearchingMDNS(): void | 开始搜索局域网内的mDNS服务。 |
| ohos.net.mdns.DiscoveryService | stopSearchingMDNS(): void | 停止搜索局域网内的mDNS服务。 |
| ohos.net.mdns.DiscoveryService | on(type: 'discoveryStart', callback: Callback
<
{
serviceInfo:
LocalServiceInfo
,
errorCode
?
:
MdnsError
}
>
): void | 订阅开启监听mDNS服务的通知。 |
| ohos.net.mdns.DiscoveryService | on(type: 'discoveryStop', callback: Callback
<
{
serviceInfo:
LocalServiceInfo
,
errorCode
?
:
MdnsError
}
>
): void | 订阅停止监听mDNS服务的通知。 |
| ohos.net.mdns.DiscoveryService | on(type: 'serviceFound', callback: Callback
\<
LocalServiceInfo>): void | 订阅发现mDNS服务的通知。 |
| ohos.net.mdns.DiscoveryService | on(type: 'serviceLost', callback: Callback
\<
LocalServiceInfo>): void | 订阅移除mDNS服务的通知。 |
## 管理本地服务
1.
设备连接WiFi。
2.
从@ohos.net.mdns里导入mdns的命名空间。
3.
调用addLocalService方法,添加本地服务。
4.
通过resolveLocalService方法,解析本地网络的IP地址。
5.
通过removeLocalService方法,移除本地服务。
```
js
// 从@ohos.net.mdns中导入mdns命名空间
import
mdns
from
'
@ohos.net.mdns
'
// FA模型获取context
import
featureAbility
from
'
@ohos.ability.featureAbility
'
;
let
context
=
featureAbility
.
getContext
();
// stage模型获取context
import
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
class
EntryAbility
extends
UIAbility
{
onWindowStageCreate
(
windowStage
){
globalThis
.
context
=
this
.
context
;
}
}
let
context
=
globalThis
.
context
;
// 建立LocalService对象
let
localServiceInfo
=
{
serviceType
:
"
_print._tcp
"
,
serviceName
:
"
servicename
"
,
port
:
5555
,
host
:
{
address
:
"
10.14.**.***
"
,
},
serviceAttribute
:
[{
key
:
"
111
"
,
value
:
[
1
]
}]
}
// addLocalService添加本地服务
mdns
.
addLocalService
(
context
,
localServiceInfo
,
function
(
error
,
data
)
{
console
.
log
(
JSON
.
stringify
(
error
));
console
.
log
(
JSON
.
stringify
(
data
));
});
// resolveLocalService解析本地服务对象
mdns
.
resolveLocalService
(
context
,
localServiceInfo
,
function
(
error
,
data
)
{
console
.
log
(
JSON
.
stringify
(
error
));
console
.
log
(
JSON
.
stringify
(
data
));
});
// removeLocalService移除本地服务
mdns
.
removeLocalService
(
context
,
localServiceInfo
,
function
(
error
,
data
)
{
console
.
log
(
JSON
.
stringify
(
error
));
console
.
log
(
JSON
.
stringify
(
data
));
});
```
## 发现本地服务
1.
设备连接WiFi。
2.
从@ohos.net.mdns里导入mdns的命名空间。
3.
创建DiscoveryService对象,用来监听本地服务。
4.
订阅指定状态变化。
5.
开启监听本地服务。
6.
停止监听本地服务。
```
js
// 从@ohos.net.mdns中导入mdns命名空间
import
mdns
from
'
@ohos.net.mdns
'
// FA模型获取context
import
featureAbility
from
'
@ohos.ability.featureAbility
'
;
let
context
=
featureAbility
.
getContext
();
// stage模型获取context
import
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
class
EntryAbility
extends
UIAbility
{
onWindowStageCreate
(
windowStage
){
globalThis
.
context
=
this
.
context
;
}
}
let
context
=
globalThis
.
context
;
// 建立LocalService对象
let
localServiceInfo
=
{
serviceType
:
"
_print._tcp
"
,
serviceName
:
"
servicename
"
,
port
:
5555
,
host
:
{
address
:
"
10.14.**.***
"
,
},
serviceAttribute
:
[{
key
:
"
111
"
,
value
:
[
1
]
}]
}
// 创建指定类型的DiscoveryService对象
let
serviceType
=
"
_print._tcp
"
;
let
discoveryService
=
mdns
.
createDiscoveryService
(
context
,
serviceType
);
// 订阅指定状态变化
discoveryService
.
on
(
'
discoveryStart
'
,
(
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
data
));
});
discoveryService
.
on
(
'
discoveryStop
'
,
(
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
data
));
});
discoveryService
.
on
(
'
serviceFound
'
,
(
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
data
));
});
discoveryService
.
on
(
'
serviceLost
'
,
(
data
)
=>
{
console
.
log
(
JSON
.
stringify
(
data
));
});
// 开启监听本地服务
discoveryService
.
stopSearchingMDNS
();
// 停止监听本地服务
discoveryService
.
stopSearchingMDNS
();
```
\ No newline at end of file
zh-cn/application-dev/reference/apis/js-apis-net-mdns.md
浏览文件 @
d727e29e
...
...
@@ -43,7 +43,13 @@ addLocalService(context: Context, serviceInfo: LocalServiceInfo, callback: Async
**示例:**
FA模型示例:
```
js
// 获取context
import
featureAbility
from
'
@ohos.ability.featureAbility
'
;
let
context
=
featureAbility
.
getContext
();
let
localServiceInfo
=
{
serviceType
:
"
_print._tcp
"
,
serviceName
:
"
servicename
"
,
...
...
@@ -58,8 +64,39 @@ let localServiceInfo = {
}
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
));
});
```
Stage模型示例:
```
ts
// 获取context
import
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
class
EntryAbility
extends
UIAbility
{
onWindowStageCreate
(
windowStage
){
globalThis
.
context
=
this
.
context
;
}
}
let
context
=
globalThis
.
context
;
let
localServiceInfo
=
{
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
));
});
```
...
...
@@ -100,7 +137,13 @@ addLocalService(context: Context, serviceInfo: LocalServiceInfo): Promise\<Local
**示例:**
FA模型示例:
```
js
// 获取context
import
featureAbility
from
'
@ohos.ability.featureAbility
'
;
let
context
=
featureAbility
.
getContext
();
let
localServiceInfo
=
{
serviceType
:
"
_print._tcp
"
,
serviceName
:
"
servicename
"
,
...
...
@@ -115,7 +158,37 @@ let localServiceInfo = {
}
mdns
.
addLocalService
(
context
,
localServiceInfo
).
then
(
function
(
data
)
{
console
.
log
(
JSON
.
stringify
(
data
))
console
.
log
(
JSON
.
stringify
(
data
));
});
```
Stage模型示例:
```
ts
// 获取context
import
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
class
EntryAbility
extends
UIAbility
{
onWindowStageCreate
(
windowStage
){
globalThis
.
context
=
this
.
context
;
}
}
let
context
=
globalThis
.
context
;
let
localServiceInfo
=
{
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
));
});
```
...
...
@@ -151,7 +224,44 @@ removeLocalService(context: Context, serviceInfo: LocalServiceInfo, callback: As
**示例:**
FA模型示例:
```
js
// 获取context
import
featureAbility
from
'
@ohos.ability.featureAbility
'
;
let
context
=
featureAbility
.
getContext
();
let
localServiceInfo
=
{
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
));
});
```
Stage模型示例:
```
ts
// 获取context
import
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
class
EntryAbility
extends
UIAbility
{
onWindowStageCreate
(
windowStage
){
globalThis
.
context
=
this
.
context
;
}
}
let
context
=
globalThis
.
context
;
let
localServiceInfo
=
{
serviceType
:
"
_print._tcp
"
,
serviceName
:
"
servicename
"
,
...
...
@@ -166,8 +276,8 @@ let localServiceInfo = {
}
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
))
;
});
```
...
...
@@ -208,7 +318,13 @@ removeLocalService(context: Context, serviceInfo: LocalServiceInfo): Promise\<Lo
**示例:**
FA模型示例:
```
js
// 获取context
import
featureAbility
from
'
@ohos.ability.featureAbility
'
;
let
context
=
featureAbility
.
getContext
();
let
localServiceInfo
=
{
serviceType
:
"
_print._tcp
"
,
serviceName
:
"
servicename
"
,
...
...
@@ -223,7 +339,37 @@ let localServiceInfo = {
}
mdns
.
removeLocalService
(
context
,
localServiceInfo
).
then
(
function
(
data
)
{
console
.
log
(
JSON
.
stringify
(
data
))
console
.
log
(
JSON
.
stringify
(
data
));
});
```
Stage模型示例:
```
ts
// 获取context
import
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
class
EntryAbility
extends
UIAbility
{
onWindowStageCreate
(
windowStage
){
globalThis
.
context
=
this
.
context
;
}
}
let
context
=
globalThis
.
context
;
let
localServiceInfo
=
{
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
));
});
```
...
...
@@ -248,11 +394,32 @@ createDiscoveryService(context: Context, serviceType: string): DiscoveryService
| ----------------------------- |---------------------------------|
| DiscoveryService | 基于指定serviceType和Context的发现服务对象。 |
**Example**
**示例**
FA模型示例:
```
js
// 获取context
import
featureAbility
from
'
@ohos.ability.featureAbility
'
;
let
context
=
featureAbility
.
getContext
();
let
serviceType
=
"
_print._tcp
"
;
let
discoveryService
=
mdns
.
createDiscoveryService
(
context
,
serviceType
);
```
Stage模型示例:
```
ts
// 获取context
import
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
class
EntryAbility
extends
UIAbility
{
onWindowStageCreate
(
windowStage
){
globalThis
.
context
=
this
.
context
;
}
}
let
context
=
globalThis
.
context
;
let
serviceType
=
"
_print._tcp
"
;
let
discoveryService
=
mdns
.
createDiscoveryService
(
context
,
serviceType
);
```
...
...
@@ -288,7 +455,44 @@ resolveLocalService(context: Context, serviceInfo: LocalServiceInfo, callback: A
**示例:**
FA模型示例:
```
js
// 获取context
import
featureAbility
from
'
@ohos.ability.featureAbility
'
;
let
context
=
featureAbility
.
getContext
();
let
localServiceInfo
=
{
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
));
});
```
Stage模型示例:
```
ts
// 获取context
import
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
class
EntryAbility
extends
UIAbility
{
onWindowStageCreate
(
windowStage
){
globalThis
.
context
=
this
.
context
;
}
}
let
context
=
globalThis
.
context
;
let
localServiceInfo
=
{
serviceType
:
"
_print._tcp
"
,
serviceName
:
"
servicename
"
,
...
...
@@ -303,8 +507,8 @@ let localServiceInfo = {
}
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
))
;
});
```
...
...
@@ -345,7 +549,13 @@ resolveLocalService(context: Context, serviceInfo: LocalServiceInfo): Promise\<L
**示例:**
FA模型示例:
```
js
// 获取context
import
featureAbility
from
'
@ohos.ability.featureAbility
'
;
let
context
=
featureAbility
.
getContext
();
let
localServiceInfo
=
{
serviceType
:
"
_print._tcp
"
,
serviceName
:
"
servicename
"
,
...
...
@@ -361,9 +571,38 @@ let localServiceInfo = {
mdns
.
resolveLocalService
(
context
,
localServiceInfo
).
then
(
function
(
data
)
{
console
.
log
(
JSON
.
stringify
(
data
));
})
})
;
```
Stage模型示例:
```
ts
// 获取context
import
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
class
EntryAbility
extends
UIAbility
{
onWindowStageCreate
(
windowStage
){
globalThis
.
context
=
this
.
context
;
}
}
let
context
=
globalThis
.
context
;
let
localServiceInfo
=
{
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
));
});
```
## DiscoveryService
指定服务类型的发现服务对象。
...
...
@@ -378,7 +617,29 @@ startSearchingMDNS(): void
**示例:**
FA模型示例:
```
js
// 获取context
import
featureAbility
from
'
@ohos.ability.featureAbility
'
;
let
context
=
featureAbility
.
getContext
();
let
discoveryService
=
mdns
.
createDiscoveryService
(
context
,
serviceType
);
discoveryService
.
startSearchingMDNS
();
```
Stage模型示例:
```
ts
// 获取context
import
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
class
EntryAbility
extends
UIAbility
{
onWindowStageCreate
(
windowStage
){
globalThis
.
context
=
this
.
context
;
}
}
let
context
=
globalThis
.
context
;
let
discoveryService
=
mdns
.
createDiscoveryService
(
context
,
serviceType
);
discoveryService
.
startSearchingMDNS
();
```
...
...
@@ -393,7 +654,29 @@ stopSearchingMDNS(): void
**示例:**
FA模型示例:
```
js
// 获取context
import
featureAbility
from
'
@ohos.ability.featureAbility
'
;
let
context
=
featureAbility
.
getContext
();
let
discoveryService
=
mdns
.
createDiscoveryService
(
context
,
serviceType
);
discoveryService
.
stopSearchingMDNS
();
```
Stage模型示例:
```
ts
// 获取context
import
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
class
EntryAbility
extends
UIAbility
{
onWindowStageCreate
(
windowStage
){
globalThis
.
context
=
this
.
context
;
}
}
let
context
=
globalThis
.
context
;
let
discoveryService
=
mdns
.
createDiscoveryService
(
context
,
serviceType
);
discoveryService
.
stopSearchingMDNS
();
```
...
...
@@ -458,7 +741,7 @@ discoveryService.stopSearchingMDNS();
### on('serviceFound')
on(type: 'serviceFound', callback: Callback
<
[
LocalServiceInfo
](
#localserviceinfo
)
>
): void
on(type: 'serviceFound', callback: Callback
\<
LocalServiceInfo
>): void
订阅发现mDNS服务的通知。
...
...
@@ -487,7 +770,7 @@ discoveryService.stopSearchingMDNS();
### on('serviceLost')
on(type: 'serviceLost', callback: Callback
<
[
LocalServiceInfo
](
#localserviceinfo
)
>
): void
on(type: 'serviceLost', callback: Callback
\<
LocalServiceInfo
>): void
订阅移除mDNS服务的通知。
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录