harmony 鸿蒙@ohos.net.mdns (MDNS管理)

  • 2023-06-24
  • 浏览 (737)

@ohos.net.mdns (MDNS管理)

MDNS即多播DNS(Multicast DNS),提供局域网内的本地服务添加、移除、发现、解析等能力。

说明: 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

导入模块

import mdns from '@ohos.net.mdns'

mdns.addLocalService10+

addLocalService(context: Context, serviceInfo: LocalServiceInfo, callback: AsyncCallback<LocalServiceInfo>): void

添加一个mDNS服务,使用callback方式作为异步方法。

系统能力:SystemCapability.Communication.NetManager.MDNS

参数:

参数名 类型 必填 说明
context Context 应用的上下文。
FA模型的应用Context定义见Context
Stage模型的应用Context定义见Context
serviceInfo LocalServiceInfo mDNS服务的信息。
callback AsyncCallback<LocalServiceInfo> 回调函数。成功添加error为undefined,data为添加到本地的mdns服务信息。

错误码:

错误码ID 错误信息
401 Parameter error.
2100002 Operation failed. Cannot connect to service.
2100003 System internal error.
2204003 Callback duplicated.
2204008 Service instance duplicated.
2204010 Send packet failed.

错误码说明: 以上错误码的详细介绍参见MDNS错误码

示例:

Stage模型示例:

// 获取context
import mdns from '@ohos.net.mdns'
import UIAbility from '@ohos.app.ability.UIAbility';
import { BusinessError } from '@ohos.base';
import window from '@ohos.window';

export class GlobalContext {
  private constructor() {}
  private static instance: GlobalContext;
  private _objects = new Map<string, Object>();

  public static getContext(): GlobalContext {
    if (!GlobalContext.instance) {
      GlobalContext.instance = new GlobalContext();
    }
    return GlobalContext.instance;
  }

  getObject(value: string): Object|undefined {
    return this._objects.get(value);
  }

  setObject(key: string, objectClass: Object): void {
    this._objects.set(key, objectClass);
  }
}

class EntryAbility extends UIAbility {
  value:number = 0;
  onWindowStageCreate(windowStage:window.WindowStage): void{
    GlobalContext.getContext().setObject("value", this.value);
  }
}
let context = GlobalContext.getContext().getObject("value");

let localServiceInfo: mdns.LocalServiceInfo = {
  serviceType: "_print._tcp",
  serviceName: "servicename",
  port: 5555,
  host: {
  address: "10.14.**.***",
  },
  serviceAttribute: [{key: "111", value: [1]}]
}

mdns.addLocalService(context as Context, localServiceInfo, (error:BusinessError, data:mdns.LocalServiceInfo) =>  {
  console.log(JSON.stringify(error));
  console.log(JSON.stringify(data));
});

mdns.addLocalService10+

addLocalService(context: Context, serviceInfo: LocalServiceInfo): Promise<LocalServiceInfo>

添加一个mDNS服务,使用Promise方式作为异步方法。

系统能力:SystemCapability.Communication.NetManager.MDNS

参数:

参数名 类型 必填 说明
context Context 应用的上下文。
FA模型的应用Context定义见Context
Stage模型的应用Context定义见Context
serviceInfo LocalServiceInfo mDNS服务的信息。

返回值:

类型 说明
Promise<LocalServiceInfo> 以Promise形式返回添加的mdns服务信息。

错误码:

错误码ID 错误信息
401 Parameter error.
2100002 Operation failed. Cannot connect to service.
2100003 System internal error.
2204003 Callback duplicated.
2204008 Service instance duplicated.
2204010 Send packet failed.

错误码说明: 以上错误码的详细介绍参见MDNS错误码

示例:

Stage模型示例:

// 获取context
import mdns from '@ohos.net.mdns'
import UIAbility from '@ohos.app.ability.UIAbility';
import { BusinessError } from '@ohos.base';
import window from '@ohos.window';

export class GlobalContext {
  private constructor() {}
  private static instance: GlobalContext;
  private _objects = new Map<string, Object>();

  public static getContext(): GlobalContext {
    if (!GlobalContext.instance) {
      GlobalContext.instance = new GlobalContext();
    }
    return GlobalContext.instance;
  }

  getObject(value: string): Object|undefined {
    return this._objects.get(value);
  }

  setObject(key: string, objectClass: Object): void {
    this._objects.set(key, objectClass);
  }
}

class EntryAbility extends UIAbility {
  value:number = 0;
  onWindowStageCreate(windowStage:window.WindowStage): void{
    GlobalContext.getContext().setObject("value", this.value);
  }
}
let context = GlobalContext.getContext().getObject("value");

let localServiceInfo: mdns.LocalServiceInfo = {
  serviceType: "_print._tcp",
  serviceName: "servicename",
  port: 5555,
  host: {
    address: "10.14.**.***",
  },
  serviceAttribute: [{key: "111", value: [1]}]
}

mdns.addLocalService(context as Context, localServiceInfo).then((data: mdns.LocalServiceInfo) => {
  console.log(JSON.stringify(data));
});

mdns.removeLocalService10+

removeLocalService(context: Context, serviceInfo: LocalServiceInfo, callback: AsyncCallback<LocalServiceInfo>): void

移除一个mDNS服务,使用callback方式作为异步方法。

系统能力: SystemCapability.Communication.NetManager.MDNS

参数

参数名 类型 必填 说明
context Context 应用的上下文。
FA模型的应用Context定义见Context
Stage模型的应用Context定义见Context
serviceInfo LocalServiceInfo mDNS服务的信息。
callback AsyncCallback<LocalServiceInfo> 回调函数。成功移除error为undefined,data为移除本地的mdns服务信息。

错误码:

错误码ID 错误信息
401 Parameter error.
2100002 Operation failed. Cannot connect to service.
2100003 System internal error.
2204002 Callback not found.
2204008 Service instance not found.
2204010 Send packet failed.

错误码说明: 以上错误码的详细介绍参见MDNS错误码

示例:

Stage模型示例:

// 获取context
import mdns from '@ohos.net.mdns'
import UIAbility from '@ohos.app.ability.UIAbility';
import { BusinessError } from '@ohos.base';
import window from '@ohos.window';

export class GlobalContext {
  private constructor() {}
  private static instance: GlobalContext;
  private _objects = new Map<string, Object>();

  public static getContext(): GlobalContext {
    if (!GlobalContext.instance) {
      GlobalContext.instance = new GlobalContext();
    }
    return GlobalContext.instance;
  }

  getObject(value: string): Object|undefined {
    return this._objects.get(value);
  }

  setObject(key: string, objectClass: Object): void {
    this._objects.set(key, objectClass);
  }
}

class EntryAbility extends UIAbility {
  value:number = 0;
  onWindowStageCreate(windowStage:window.WindowStage): void{
    GlobalContext.getContext().setObject("value", this.value);
  }
}
let context = GlobalContext.getContext().getObject("value");

let localServiceInfo: mdns.LocalServiceInfo = {
  serviceType: "_print._tcp",
  serviceName: "servicename",
  port: 5555,
  host: {
  address: "10.14.**.***",
  },
  serviceAttribute: [{key: "111", value: [1]}]
}

mdns.removeLocalService(context as Context, localServiceInfo, (error: BusinessError, data: mdns.LocalServiceInfo) =>  {
  console.log(JSON.stringify(error));
  console.log(JSON.stringify(data));
});

mdns.removeLocalService10+

removeLocalService(context: Context, serviceInfo: LocalServiceInfo): Promise<LocalServiceInfo>

移除一个mDNS服务. 使用Promise方式作为异步方法。

系统能力: SystemCapability.Communication.NetManager.MDNS

参数

参数名 类型 必填 说明
context Context 应用的上下文。
FA模型的应用Context定义见Context
Stage模型的应用Context定义见Context
serviceInfo LocalServiceInfo mDNS服务的信息。

返回值:

类型 说明
Promise<LocalServiceInfo> 以Promise形式返回移除的mdns服务信息。

错误码:

错误码ID 错误信息
401 Parameter error.
2100002 Operation failed. Cannot connect to service.
2100003 System internal error.
2204002 Callback not found.
2204008 Service instance not found.
2204010 Send packet failed.

错误码说明: 以上错误码的详细介绍参见MDNS错误码

示例:

Stage模型示例:

import mdns from '@ohos.net.mdns'
// 获取context
import UIAbility from '@ohos.app.ability.UIAbility';
import { BusinessError } from '@ohos.base';
import window from '@ohos.window';

export class GlobalContext {
  private constructor() {}
  private static instance: GlobalContext;
  private _objects = new Map<string, Object>();

  public static getContext(): GlobalContext {
    if (!GlobalContext.instance) {
      GlobalContext.instance = new GlobalContext();
    }
    return GlobalContext.instance;
  }

  getObject(value: string): Object|undefined {
    return this._objects.get(value);
  }

  setObject(key: string, objectClass: Object): void {
    this._objects.set(key, objectClass);
  }
}

class EntryAbility extends UIAbility {
  value:number = 0;
  onWindowStageCreate(windowStage:window.WindowStage): void{
    GlobalContext.getContext().setObject("value", this.value);
  }
}
let context = GlobalContext.getContext().getObject("value");

let localServiceInfo: mdns.LocalServiceInfo = {
  serviceType: "_print._tcp",
  serviceName: "servicename",
  port: 5555,
  host: {
  address: "10.14.**.***",
  },
  serviceAttribute: [{key: "111", value: [1]}]
}

mdns.removeLocalService(context as Context, localServiceInfo).then((data: mdns.LocalServiceInfo) => {
  console.log(JSON.stringify(data));
});

mdns.createDiscoveryService10+

createDiscoveryService(context: Context, serviceType: string): DiscoveryService

返回一个DiscoveryService对象,该对象用于发现指定服务类型的mDNS服务。

系统能力:SystemCapability.Communication.NetManager.MDNS

参数:

参数名 类型 必填 说明
context Context 应用的上下文。
FA模型的应用Context定义见Context
Stage模型的应用Context定义见Context
serviceType string 需要发现的mDNS服务类型。

返回值:

Type Description
DiscoveryService 基于指定serviceType和Context的发现服务对象。

错误码:

错误码ID 错误信息
401 Parameter error.

示例

Stage模型示例:

// 获取context
import mdns from '@ohos.net.mdns'
import UIAbility from '@ohos.app.ability.UIAbility';
import { BusinessError } from '@ohos.base';
import window from '@ohos.window';

export class GlobalContext {
  private constructor() {}
  private static instance: GlobalContext;
  private _objects = new Map<string, Object>();

  public static getContext(): GlobalContext {
    if (!GlobalContext.instance) {
      GlobalContext.instance = new GlobalContext();
    }
    return GlobalContext.instance;
  }

  getObject(value: string): Object|undefined {
    return this._objects.get(value);
  }

  setObject(key: string, objectClass: Object): void {
    this._objects.set(key, objectClass);
  }
}

class EntryAbility extends UIAbility {
  value:number = 0;
  onWindowStageCreate(windowStage:window.WindowStage): void{
    GlobalContext.getContext().setObject("value", this.value);
  }
}
let context = GlobalContext.getContext().getObject("value");

let serviceType = "_print._tcp";
let discoveryService : Object = mdns.createDiscoveryService(context as Context, serviceType);

mdns.resolveLocalService10+

resolveLocalService(context: Context, serviceInfo: LocalServiceInfo, callback: AsyncCallback<LocalServiceInfo>): void

解析一个mDNS服务,使用callback方式作为异步方法。

系统能力: SystemCapability.Communication.NetManager.MDNS

参数:

参数名 类型 必填 说明
context Context 应用的上下文。
FA模型的应用Context定义见Context
Stage模型的应用Context定义见Context
serviceInfo LocalServiceInfo mDNS服务的信息。
callback AsyncCallback<LocalServiceInfo> 回调函数。成功移除error为undefined,data为解析的mdns服务信息。

错误码:

错误码ID 错误信息
401 Parameter error.
2100002 Operation failed. Cannot connect to service.
2100003 System internal error.
2204003 Callback duplicated.
2204006 Request timeout.
2204010 Send packet failed.

错误码说明: 以上错误码的详细介绍参见MDNS错误码

示例:

Stage模型示例:

// 获取context
import mdns from '@ohos.net.mdns'
import UIAbility from '@ohos.app.ability.UIAbility';
import { BusinessError } from '@ohos.base';
import window from '@ohos.window';

export class GlobalContext {
  private constructor() {}
  private static instance: GlobalContext;
  private _objects = new Map<string, Object>();

  public static getContext(): GlobalContext {
    if (!GlobalContext.instance) {
      GlobalContext.instance = new GlobalContext();
    }
    return GlobalContext.instance;
  }

  getObject(value: string): Object|undefined {
    return this._objects.get(value);
  }

  setObject(key: string, objectClass: Object): void {
    this._objects.set(key, objectClass);
  }
}

class EntryAbility extends UIAbility {
  value:number = 0;
  onWindowStageCreate(windowStage:window.WindowStage): void{
    GlobalContext.getContext().setObject("value", this.value);
  }
}
let context = GlobalContext.getContext().getObject("value");

let localServiceInfo: mdns.LocalServiceInfo = {
  serviceType: "_print._tcp",
  serviceName: "servicename",
  port: 5555,
  host: {
  address: "10.14.**.***",
  },
  serviceAttribute: [{key: "111", value: [1]}]
}

mdns.resolveLocalService(context as Context, localServiceInfo, (error: BusinessError, data: mdns.LocalServiceInfo) =>  {
  console.log(JSON.stringify(error));
  console.log(JSON.stringify(data));
});

mdns.resolveLocalService10+

resolveLocalService(context: Context, serviceInfo: LocalServiceInfo): Promise<LocalServiceInfo>

解析一个mDNS服务,使用Promise方式作为异步方法。

系统能力: SystemCapability.Communication.NetManager.MDNS

参数:

参数名 类型 必填 说明
context Context 应用的上下文。
FA模型的应用Context定义见Context
Stage模型的应用Context定义见Context
serviceInfo LocalServiceInfo mDNS服务的信息。

返回值:

类型 说明
Promise<LocalServiceInfo> 以Promise形式返回解析的mDNS服务信息。

错误码:

错误码ID 错误信息
401 Parameter error.
2100002 Operation failed. Cannot connect to service.
2100003 System internal error.
2204003 Callback duplicated.
2204006 Request timeout.
2204010 Send packet failed.

错误码说明: 以上错误码的详细介绍参见MDNS错误码

示例:

Stage模型示例:

// 获取context
import mdns from '@ohos.net.mdns'
import UIAbility from '@ohos.app.ability.UIAbility';
import { BusinessError } from '@ohos.base';
import window from '@ohos.window';

export class GlobalContext {
  private constructor() {}
  private static instance: GlobalContext;
  private _objects = new Map<string, Object>();

  public static getContext(): GlobalContext {
    if (!GlobalContext.instance) {
      GlobalContext.instance = new GlobalContext();
    }
    return GlobalContext.instance;
  }

  getObject(value: string): Object|undefined {
    return this._objects.get(value);
  }

  setObject(key: string, objectClass: Object): void {
    this._objects.set(key, objectClass);
  }
}

class EntryAbility extends UIAbility {
  value:number = 0;
  onWindowStageCreate(windowStage:window.WindowStage): void{
    GlobalContext.getContext().setObject("value", this.value);
  }
}
let context = GlobalContext.getContext().getObject("value");

let localServiceInfo: mdns.LocalServiceInfo = {
  serviceType: "_print._tcp",
  serviceName: "servicename",
  port: 5555,
  host: {
  address: "10.14.**.***",
  },
  serviceAttribute: [{key: "111", value: [1]}]
}

mdns.resolveLocalService(context as Context, localServiceInfo).then((data: mdns.LocalServiceInfo) => {
  console.log(JSON.stringify(data));
});

DiscoveryService10+

指定服务类型的发现服务对象。

startSearchingMDNS10+

startSearchingMDNS(): void

开始搜索局域网内的mDNS服务。

系统能力:SystemCapability.Communication.NetManager.MDNS

示例:

Stage模型示例:

// 获取context
import mdns from '@ohos.net.mdns'
import UIAbility from '@ohos.app.ability.UIAbility';
import { BusinessError } from '@ohos.base';
import window from '@ohos.window';

export class GlobalContext {
  private constructor() {}
  private static instance: GlobalContext;
  private _objects = new Map<string, Object>();

  public static getContext(): GlobalContext {
    if (!GlobalContext.instance) {
      GlobalContext.instance = new GlobalContext();
    }
    return GlobalContext.instance;
  }

  getObject(value: string): Object|undefined {
    return this._objects.get(value);
  }

  setObject(key: string, objectClass: Object): void {
    this._objects.set(key, objectClass);
  }
}

class EntryAbility extends UIAbility {
  value:number = 0;
  onWindowStageCreate(windowStage:window.WindowStage): void{
    GlobalContext.getContext().setObject("value", this.value);
  }
}
let context = GlobalContext.getContext().getObject("value");
let serviceType = "_print._tcp";
let discoveryService = mdns.createDiscoveryService(context as Context, serviceType);
discoveryService.startSearchingMDNS();

stopSearchingMDNS10+

stopSearchingMDNS(): void

停止搜索局域网内的mDNS服务。

系统能力:SystemCapability.Communication.NetManager.MDNS

示例:

Stage模型示例:

// 获取context
import mdns from '@ohos.net.mdns'
import UIAbility from '@ohos.app.ability.UIAbility';
import { BusinessError } from '@ohos.base';
import window from '@ohos.window';

export class GlobalContext {
  private constructor() {}
  private static instance: GlobalContext;
  private _objects = new Map<string, Object>();

  public static getContext(): GlobalContext {
    if (!GlobalContext.instance) {
      GlobalContext.instance = new GlobalContext();
    }
    return GlobalContext.instance;
  }

  getObject(value: string): Object|undefined {
    return this._objects.get(value);
  }

  setObject(key: string, objectClass: Object): void {
    this._objects.set(key, objectClass);
  }
}

class EntryAbility extends UIAbility {
  value:number = 0;
  onWindowStageCreate(windowStage:window.WindowStage): void{
    GlobalContext.getContext().setObject("value", this.value);
  }
}
let context = GlobalContext.getContext().getObject("value");
let serviceType = "_print._tcp";
let discoveryService = mdns.createDiscoveryService(context as Context, serviceType);
discoveryService.stopSearchingMDNS();

on(‘discoveryStart’)10+

on(type: ‘discoveryStart’, callback: Callback<{serviceInfo: LocalServiceInfo, errorCode?: MdnsError}>): void

订阅开启监听mDNS服务的通知。

系统能力:SystemCapability.Communication.NetManager.MDNS

参数:

参数名 类型 必填 说明
type string 订阅事件,固定为’discoveryStart’。
discoveryStart:开始搜索局域网内的mDNS服务事件。
callback Callback<{serviceInfo: LocalServiceInfo, errorCode?: MdnsError}> mDNS服务的信息和事件错误信息。

示例:

import mdns from '@ohos.net.mdns'
import UIAbility from '@ohos.app.ability.UIAbility';
import { BusinessError } from '@ohos.base';
import window from '@ohos.window';

export class GlobalContext {
  private constructor() {}
  private static instance: GlobalContext;
  private _objects = new Map<string, Object>();

  public static getContext(): GlobalContext {
    if (!GlobalContext.instance) {
      GlobalContext.instance = new GlobalContext();
    }
    return GlobalContext.instance;
  }

  getObject(value: string): Object|undefined {
    return this._objects.get(value);
  }

  setObject(key: string, objectClass: Object): void {
    this._objects.set(key, objectClass);
  }
}

class EntryAbility extends UIAbility {
  value:number = 0;
  onWindowStageCreate(windowStage:window.WindowStage): void{
    GlobalContext.getContext().setObject("value", this.value);
  }
}

// 参考mdns.createDiscoveryService
class DataServiceInfo{
  serviceInfo: mdns.LocalServiceInfo|null = null
  errorCode?: mdns.MdnsError = 0
}
let context = GlobalContext.getContext().getObject("value");
let serviceType = "_print._tcp";
let discoveryService = mdns.createDiscoveryService(context as Context, serviceType);
discoveryService.startSearchingMDNS();

discoveryService.on('discoveryStart', (data: DataServiceInfo) => {
  console.log(JSON.stringify(data));
});

discoveryService.stopSearchingMDNS();

off(‘discoveryStart’)10+

off(type: ‘discoveryStart’, callback?: Callback<{ serviceInfo: LocalServiceInfo, errorCode?: MdnsError }>): void

取消开启监听mDNS服务的通知。

系统能力:SystemCapability.Communication.NetManager.MDNS

参数:

参数名 类型 必填 说明
type string 取消订阅的事件,固定为’discoveryStart’。
discoveryStart:开始搜索局域网内的mDNS服务事件。
callback Callback<{serviceInfo: LocalServiceInfo, errorCode?: MdnsError}> mDNS服务的信息和事件错误信息。

示例:

import mdns from '@ohos.net.mdns'
import UIAbility from '@ohos.app.ability.UIAbility';
import { BusinessError } from '@ohos.base';
import window from '@ohos.window';

export class GlobalContext {
  private constructor() {}
  private static instance: GlobalContext;
  private _objects = new Map<string, Object>();

  public static getContext(): GlobalContext {
    if (!GlobalContext.instance) {
      GlobalContext.instance = new GlobalContext();
    }
    return GlobalContext.instance;
  }

  getObject(value: string): Object|undefined {
    return this._objects.get(value);
  }

  setObject(key: string, objectClass: Object): void {
    this._objects.set(key, objectClass);
  }
}

class EntryAbility extends UIAbility {
  value:number = 0;
  onWindowStageCreate(windowStage:window.WindowStage): void{
    GlobalContext.getContext().setObject("value", this.value);
  }
}
// 参考mdns.createDiscoveryService
let context = GlobalContext.getContext().getObject("value");
let serviceType = "_print._tcp";
let discoveryService = mdns.createDiscoveryService(context as Context, serviceType);
discoveryService.startSearchingMDNS();

interface Data {
  serviceInfo: mdns.LocalServiceInfo,
  errorCode?: mdns.MdnsError
}
discoveryService.on('discoveryStart', (data: Data) => {
  console.log(JSON.stringify(data));
});

discoveryService.stopSearchingMDNS();

discoveryService.off('discoveryStart', (data: Data) => {
  console.log(JSON.stringify(data));
});

on(‘discoveryStop’)10+

on(type: ‘discoveryStop’, callback: Callback<{serviceInfo: LocalServiceInfo, errorCode?: MdnsError}>): void

订阅停止监听mDNS服务的通知。

系统能力:SystemCapability.Communication.NetManager.MDNS

参数:

参数名 类型 必填 说明
type string 订阅事件,固定为’discoveryStop’。
discoveryStop:停止搜索局域网内的mDNS服务事件。
callback Callback<{serviceInfo: LocalServiceInfo, errorCode?: MdnsError}> mDNS服务的信息和事件错误信息。

示例:

import mdns from '@ohos.net.mdns'
import UIAbility from '@ohos.app.ability.UIAbility';
import { BusinessError } from '@ohos.base';
import window from '@ohos.window';

export class GlobalContext {
  private constructor() {}
  private static instance: GlobalContext;
  private _objects = new Map<string, Object>();

  public static getContext(): GlobalContext {
    if (!GlobalContext.instance) {
      GlobalContext.instance = new GlobalContext();
    }
    return GlobalContext.instance;
  }

  getObject(value: string): Object|undefined {
    return this._objects.get(value);
  }

  setObject(key: string, objectClass: Object): void {
    this._objects.set(key, objectClass);
  }
}

class EntryAbility extends UIAbility {
  value:number = 0;
  onWindowStageCreate(windowStage:window.WindowStage): void{
    GlobalContext.getContext().setObject("value", this.value);
  }
}
// 参考mdns.createDiscoveryService
let context = GlobalContext.getContext().getObject("value");
let serviceType = "_print._tcp";
let discoveryService = mdns.createDiscoveryService(context as Context, serviceType);
discoveryService.startSearchingMDNS();

interface Data {
  serviceInfo: mdns.LocalServiceInfo,
  errorCode?: mdns.MdnsError
}
discoveryService.on('discoveryStop', (data: Data) => {
  console.log(JSON.stringify(data));
});

discoveryService.stopSearchingMDNS();

off(‘discoveryStop’)10+

off(type: ‘discoveryStop’, callback?: Callback<{ serviceInfo: LocalServiceInfo, errorCode?: MdnsError }>): void

取消订阅停止监听mDNS服务的通知。

系统能力:SystemCapability.Communication.NetManager.MDNS

参数:

参数名 类型 必填 说明
type string 取消订阅的事件’discoveryStop’。
discoveryStop:停止搜索局域网内的mDNS服务事件。
callback Callback<{serviceInfo: LocalServiceInfo, errorCode?: MdnsError}> mDNS服务的信息和事件错误信息。

示例:

import mdns from '@ohos.net.mdns'
import UIAbility from '@ohos.app.ability.UIAbility';
import { BusinessError } from '@ohos.base';
import window from '@ohos.window';

export class GlobalContext {
  private constructor() {}
  private static instance: GlobalContext;
  private _objects = new Map<string, Object>();

  public static getContext(): GlobalContext {
    if (!GlobalContext.instance) {
      GlobalContext.instance = new GlobalContext();
    }
    return GlobalContext.instance;
  }

  getObject(value: string): Object|undefined {
    return this._objects.get(value);
  }

  setObject(key: string, objectClass: Object): void {
    this._objects.set(key, objectClass);
  }
}

class EntryAbility extends UIAbility {
  value:number = 0;
  onWindowStageCreate(windowStage:window.WindowStage): void{
    GlobalContext.getContext().setObject("value", this.value);
  }
}
// 参考mdns.createDiscoveryService
let context = GlobalContext.getContext().getObject("value");
let serviceType = "_print._tcp";
let discoveryService = mdns.createDiscoveryService(context as Context, serviceType);
discoveryService.startSearchingMDNS();

interface Data {
  serviceInfo: mdns.LocalServiceInfo,
  errorCode?: mdns.MdnsError
}
discoveryService.on('discoveryStop', (data: Data) => {
  console.log(JSON.stringify(data));
});

discoveryService.stopSearchingMDNS();

discoveryService.off('discoveryStop', (data: Data) => {
  console.log(JSON.stringify(data));
});

on(‘serviceFound’)10+

on(type: ‘serviceFound’, callback: Callback<LocalServiceInfo>): void

订阅发现mDNS服务的通知。

系统能力:SystemCapability.Communication.NetManager.MDNS

参数:

参数名 类型 必填 说明
type string 订阅事件,固定为’serviceFound’。
serviceFound:发现mDNS服务事件。
callback Callback<LocalServiceInfo> mDNS服务的信息。

示例:

import mdns from '@ohos.net.mdns'
import UIAbility from '@ohos.app.ability.UIAbility';
import { BusinessError } from '@ohos.base';
import window from '@ohos.window';

export class GlobalContext {
  private constructor() {}
  private static instance: GlobalContext;
  private _objects = new Map<string, Object>();

  public static getContext(): GlobalContext {
    if (!GlobalContext.instance) {
      GlobalContext.instance = new GlobalContext();
    }
    return GlobalContext.instance;
  }

  getObject(value: string): Object|undefined {
    return this._objects.get(value);
  }

  setObject(key: string, objectClass: Object): void {
    this._objects.set(key, objectClass);
  }
}

class EntryAbility extends UIAbility {
  value:number = 0;
  onWindowStageCreate(windowStage:window.WindowStage): void{
    GlobalContext.getContext().setObject("value", this.value);
  }
}
// 参考mdns.createDiscoveryService
let context = GlobalContext.getContext().getObject("value");
let serviceType = "_print._tcp";
let discoveryService = mdns.createDiscoveryService(context as Context, serviceType);
discoveryService.startSearchingMDNS();

discoveryService.on('serviceFound', (data: mdns.LocalServiceInfo) => {
  console.log(JSON.stringify(data));
});

discoveryService.stopSearchingMDNS();

off(‘serviceFound’)10+

off(type: ‘serviceFound’, callback?: Callback<LocalServiceInfo>): void

取消订阅发现mDNS服务的通知。

系统能力:SystemCapability.Communication.NetManager.MDNS

参数:

参数名 类型 必填 说明
type string 取消订阅的事件,固定为’serviceFound’。
serviceFound:发现mDNS服务事件。
callback Callback<LocalServiceInfo> mDNS服务的信息。

示例:

import mdns from '@ohos.net.mdns'
import UIAbility from '@ohos.app.ability.UIAbility';
import { BusinessError } from '@ohos.base';
import window from '@ohos.window';

export class GlobalContext {
  private constructor() {}
  private static instance: GlobalContext;
  private _objects = new Map<string, Object>();

  public static getContext(): GlobalContext {
    if (!GlobalContext.instance) {
      GlobalContext.instance = new GlobalContext();
    }
    return GlobalContext.instance;
  }

  getObject(value: string): Object|undefined {
    return this._objects.get(value);
  }

  setObject(key: string, objectClass: Object): void {
    this._objects.set(key, objectClass);
  }
}

class EntryAbility extends UIAbility {
  value:number = 0;
  onWindowStageCreate(windowStage:window.WindowStage): void{
    GlobalContext.getContext().setObject("value", this.value);
  }
}
// 参考mdns.createDiscoveryService
let context = GlobalContext.getContext().getObject("value");
let serviceType = "_print._tcp";
let discoveryService = mdns.createDiscoveryService(context as Context, serviceType);
discoveryService.startSearchingMDNS();

discoveryService.on('serviceFound', (data: mdns.LocalServiceInfo) => {
  console.log(JSON.stringify(data));
});

discoveryService.stopSearchingMDNS();

discoveryService.off('serviceFound', (data: mdns.LocalServiceInfo) => {
  console.log(JSON.stringify(data));
});

on(‘serviceLost’)10+

on(type: ‘serviceLost’, callback: Callback<LocalServiceInfo>): void

订阅移除mDNS服务的通知。

系统能力:SystemCapability.Communication.NetManager.MDNS

参数:

参数名 类型 必填 说明
type string 订阅事件,固定为’serviceLost’。
serviceLost:移除mDNS服务事件。
callback Callback<LocalServiceInfo> mDNS服务的信息。

示例:

import mdns from '@ohos.net.mdns'
import UIAbility from '@ohos.app.ability.UIAbility';
import { BusinessError } from '@ohos.base';
import window from '@ohos.window';

export class GlobalContext {
  private constructor() {}
  private static instance: GlobalContext;
  private _objects = new Map<string, Object>();

  public static getContext(): GlobalContext {
    if (!GlobalContext.instance) {
      GlobalContext.instance = new GlobalContext();
    }
    return GlobalContext.instance;
  }

  getObject(value: string): Object|undefined {
    return this._objects.get(value);
  }

  setObject(key: string, objectClass: Object): void {
    this._objects.set(key, objectClass);
  }
}

class EntryAbility extends UIAbility {
  value:number = 0;
  onWindowStageCreate(windowStage:window.WindowStage): void{
    GlobalContext.getContext().setObject("value", this.value);
  }
}
// 参考mdns.createDiscoveryService
let context = GlobalContext.getContext().getObject("value");
let serviceType = "_print._tcp";
let discoveryService = mdns.createDiscoveryService(context as Context, serviceType);
discoveryService.startSearchingMDNS();

discoveryService.on('serviceLost', (data: mdns.LocalServiceInfo) => {
  console.log(JSON.stringify(data));
});

discoveryService.stopSearchingMDNS();

off(‘serviceLost’)10+

off(type: ‘serviceLost’, callback?: Callback<LocalServiceInfo>): void

取消订阅移除mDNS服务的通知。

系统能力:SystemCapability.Communication.NetManager.MDNS

参数:

参数名 类型 必填 说明
type string 取消订阅的事件,固定为’serviceLost’。
serviceLost:移除mDNS服务事件。
callback Callback<LocalServiceInfo> mDNS服务的信息。

示例:

import mdns from '@ohos.net.mdns'
import UIAbility from '@ohos.app.ability.UIAbility';
import { BusinessError } from '@ohos.base';
import window from '@ohos.window';

export class GlobalContext {
  private constructor() {}
  private static instance: GlobalContext;
  private _objects = new Map<string, Object>();

  public static getContext(): GlobalContext {
    if (!GlobalContext.instance) {
      GlobalContext.instance = new GlobalContext();
    }
    return GlobalContext.instance;
  }

  getObject(value: string): Object|undefined {
    return this._objects.get(value);
  }

  setObject(key: string, objectClass: Object): void {
    this._objects.set(key, objectClass);
  }
}

class EntryAbility extends UIAbility {
  value:number = 0;
  onWindowStageCreate(windowStage:window.WindowStage): void{
    GlobalContext.getContext().setObject("value", this.value);
  }
}
// 参考mdns.createDiscoveryService
let context = GlobalContext.getContext().getObject("value");
let serviceType = "_print._tcp";
let discoveryService = mdns.createDiscoveryService(context as Context, serviceType);
discoveryService.startSearchingMDNS();

discoveryService.on('serviceLost', (data: mdns.LocalServiceInfo) => {
  console.log(JSON.stringify(data));
});

discoveryService.stopSearchingMDNS();

discoveryService.off('serviceLost', (data: mdns.LocalServiceInfo) => {
  console.log(JSON.stringify(data));
});

LocalServiceInfo10+

mDNS服务信息

系统能力:SystemCapability.Communication.NetManager.MDNS

名称 类型 必填 说明
serviceType string mDNS服务的类型。格式_<name>.<_tcp/_udp>,name长度小于63字符并且不能包含字符’.‘。
serviceName string mDNS服务的名字。
port number mDNS服务的端口号。
host NetAddress mDNS服务设备的IP地址。采用设备的IP,添加服务和移除服务时候不生效。
serviceAttribute serviceAttribute<ServiceAttribute> mDNS服务属性信息。

ServiceAttribute10+

mDNS服务属性信息

系统能力:SystemCapability.Communication.NetManager.MDNS

名称 类型 必填 说明
key string mDNS服务属性键值,键值长度应该小于9个字符。
value Array<number> mDNS服务属性值。

MdnsError

mDNS错误信息。

系统能力:SystemCapability.Communication.NetManager.MDNS

名称 说明
INTERNAL_ERROR 0 内部错误导致操作失败。(暂不支持)
ALREADY_ACTIVE 1 服务已经存在导致操作失败。(暂不支持)
MAX_LIMIT 2 请求超过最大限制导致操作失败。(暂不支持)

你可能感兴趣的鸿蒙文章

harmony 鸿蒙接口

harmony 鸿蒙系统公共事件定义(待停用)

harmony 鸿蒙系统公共事件定义

harmony 鸿蒙开发说明

harmony 鸿蒙企业设备管理概述(仅对系统应用开放)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager模块)

harmony 鸿蒙@ohos.distributedBundle (分布式包管理)

harmony 鸿蒙@ohos.bundle (Bundle模块)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (企业设备管理扩展能力)

0  赞