提交 d727e29e 编写于 作者: L liyufan

add MDNS code

Signed-off-by: Nliyufan <liyufan5@huawei.com>
上级 ac494246
# 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
......@@ -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.
先完成此消息的编辑!
想要评论请 注册