Multicast DNS (mDNS) provides functions such as adding, removing, discovering, and resolving local services on a LAN.
Typical mDNS management scenarios include:
Managing local services on a LAN, such as adding, removing, and resolving local services.
Discovering local services and listening to the status changes of local services of the specified type through the DiscoveryService object.
To ensure efficient application operation, most APIs are asynchronous. Asynchronous APIs support both callback and promise-based approaches. The examples below use callbacks. For other approaches, see @ohos.net.mdns (mDNS Management).
The following describes the development procedure specific to each application scenario.
In the sample code provided in this topic, this.context is used to obtain UIAbilityContext, where this indicates a UIAbility instance inherited from UIAbility. To use the capabilities provided by UIAbilityContext on a page, see Obtaining the Context of UIAbility.
Import the mdns, BusinessError, featureAbility, and common namespaces from @kit.NetworkKit.
// Import the mdns namespace from @kit.NetworkKit.
import { mdns } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { featureAbility, common } from '@kit.AbilityKit'; Call addLocalService to add a local service.
let context: common.UIAbilityContext = this.getUIContext().getHostContext() as common.UIAbilityContext;
// Create a LocalService object.
let localServiceInfo: mdns.LocalServiceInfo = {
serviceType: "_print._tcp",
serviceName: "servicename",
port: 5555,
host: {
address: "10.14.**.**"
},
serviceAttribute: [{key: "111", value: [1]}]
}
// Call addLocalService to add a local service.
mdns.addLocalService(context, localServiceInfo).then((data: mdns.LocalServiceInfo) => {
console.info(JSON.stringify(data));
}); (Optional) Call resolveLocalService to resolve the local service for the IP address of the local network.
// (Optional) Call resolveLocalService to resolve the local service.
mdns.resolveLocalService(context, localServiceInfo).then((data: mdns.LocalServiceInfo) => {
console.info(JSON.stringify(data));
}); Call removeLocalService to remove the local service.
// Call removeLocalService to remove the local service.
mdns.removeLocalService(context, localServiceInfo).then((data: mdns.LocalServiceInfo) => {
console.info(JSON.stringify(data));
}); Import the mdns namespace from @kit.NetworkKit.
// Import the mdns namespace from @kit.NetworkKit.
import { common, featureAbility, UIAbility } from '@kit.AbilityKit';
import { mdns } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { window } from '@kit.ArkUI'; Create a DiscoveryService object, which is used to discover mDNS services of the specified type.
// Construct a singleton object.
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);
}
}
// Obtain the context of the stage model.
class EntryAbility extends UIAbility {
value:number = 0;
onWindowStageCreate(windowStage: window.WindowStage): void{
GlobalContext.getContext().setObject("value", this.value);
}
}
let context = GlobalContext.getContext().getObject("value") as common.UIAbilityContext;
// Create a DiscoveryService object, which is used to discover mDNS services of the specified type.
let serviceType = "_print._tcp";
let discoveryService = mdns.createDiscoveryService(context, serviceType); Subscribe to mDNS service discovery status changes.
// Subscribe to mDNS service discovery status changes.
discoveryService.on('discoveryStart', (data: mdns.DiscoveryEventInfo) => {
console.info(JSON.stringify(data));
});
discoveryService.on('discoveryStop', (data: mdns.DiscoveryEventInfo) => {
console.info(JSON.stringify(data));
});
discoveryService.on('serviceFound', (data: mdns.LocalServiceInfo) => {
console.info(JSON.stringify(data));
});
discoveryService.on('serviceLost', (data: mdns.LocalServiceInfo) => {
console.info(JSON.stringify(data));
}); Enable discovery of mDNS services on the LAN.
// Enable discovery of mDNS services on the LAN. discoveryService.startSearchingMDNS();
Stops searching for mDNS services on the LAN.
// Stop searching for mDNS services on the LAN. discoveryService.stopSearchingMDNS();
Unsubscribe from mDNS service discovery status changes.
// Unsubscribe from mDNS service discovery status changes.
discoveryService.off('discoveryStart', (data: mdns.DiscoveryEventInfo) => {
console.info(JSON.stringify(data));
});
discoveryService.off('discoveryStop', (data: mdns.DiscoveryEventInfo) => {
console.info(JSON.stringify(data));
});
discoveryService.off('serviceFound', (data: mdns.LocalServiceInfo) => {
console.info(JSON.stringify(data));
});
discoveryService.off('serviceLost', (data: mdns.LocalServiceInfo) => {
console.info(JSON.stringify(data));
});