1# MDNS Management 2 3## Overview 4 5Multicast DNS (MDNS) provides functions such as adding, removing, discovering, and resolving local services on a LAN. 6- Local service: a service provider on a LAN, for example, a printer or scanner. 7 8Typical MDNS management scenarios include: 9 10- Managing local services on a LAN, such as adding, removing, and resolving local services. 11- Discovering local services and listening to the status changes of local services of the specified type through the **DiscoveryService** object. 12 13> **NOTE** 14> To maximize the application running efficiency, most API calls are called asynchronously in callback or promise mode. The following code examples use the promise mode. For details about the APIs, see [MDNS Management](../reference/apis-network-kit/js-apis-net-mdns.md). 15 16The following describes the development procedure specific to each application scenario. 17 18## Available APIs 19 20For the complete list of JS APIs and example code, see, see [MDNS Management](../reference/apis-network-kit/js-apis-net-mdns.md). 21 22| API | Description| 23| ----------------------- | ---- | 24| addLocalService(context: Context, serviceInfo: LocalServiceInfo, callback: AsyncCallback\<LocalServiceInfo>): void | Adds an MDNS service. This API uses an asynchronous callback to return the result.| 25| removeLocalService(context: Context, serviceInfo: LocalServiceInfo, callback: AsyncCallback\<LocalServiceInfo>): void | Removes an MDNS service. This API uses an asynchronous callback to return the result.| 26| createDiscoveryService(context: Context, serviceType: string): DiscoveryService | Creates a **DiscoveryService** object, which is used to discover MDNS services of the specified type.| 27| resolveLocalService(context: Context, serviceInfo: LocalServiceInfo, callback: AsyncCallback\<LocalServiceInfo>): void | Resolves an MDNS service. This API uses an asynchronous callback to return the result.| 28| startSearchingMDNS(): void | Searches for MDNS services on the LAN.| 29| stopSearchingMDNS(): void | Stops searching for MDNS services on the LAN.| 30| on(type: 'discoveryStart', callback: Callback<{serviceInfo: LocalServiceInfo, errorCode?: MdnsError}>): void | Enables listening for **discoveryStart** events.| 31| off(type: 'discoveryStart', callback?: Callback<{ serviceInfo: LocalServiceInfo, errorCode?: MdnsError }>): void | Disables listening for **discoveryStart** events.| 32| on(type: 'discoveryStop', callback: Callback<{serviceInfo: LocalServiceInfo, errorCode?: MdnsError}>): void | Enables listening for **discoveryStop** events.| 33| off(type: 'discoveryStop', callback?: Callback<{ serviceInfo: LocalServiceInfo, errorCode?: MdnsError }>): void | Disables listening for **discoveryStop** events.| 34| on(type: 'serviceFound', callback: Callback\<LocalServiceInfo>): void | Enables listening for **serviceFound** events.| 35| off(type: 'serviceFound', callback?: Callback\<LocalServiceInfo>): void | Disables listening for **serviceFound** events.| 36| on(type: 'serviceLost', callback: Callback\<LocalServiceInfo>): void | Enables listening for **serviceLost** events.| 37| off(type: 'serviceLost', callback?: Callback\<LocalServiceInfo>): void | Disables listening for **serviceLost** events.| 38 39## Managing Local Services 40 411. Connect the device to the Wi-Fi network. 422. Import the **mdns** namespace from **@kit.NetworkKit**. 433. Call **addLocalService** to add a local service. 444. (Optional) Call **resolveLocalService** to resolve the local service for the IP address of the local network. 455. Call **removeLocalService** to remove the local service. 46 47>**NOTE** 48> 49>In the sample code provided in this topic, **this.context** is used to obtain the UIAbilityContext, where **this** indicates a UIAbility instance inherited from **UIAbility**. To use **UIAbilityContext** APIs on pages, see [Obtaining the Context of UIAbility](../application-models/uiability-usage.md#obtaining-the-context-of-uiability). 50 51```ts 52// Import the mdns namespace from @kit.NetworkKit. 53import { mdns } from '@kit.NetworkKit'; 54import { BusinessError } from '@kit.BasicServicesKit'; 55import { featureAbility, common } from '@kit.AbilityKit'; 56 57let context: common.UIAbilityContext = this.getUIContext().getHostContext() as common.UIAbilityContext; 58 59class ServiceAttribute { 60 key: string = "111" 61 value: Array<number> = [1] 62} 63 64// Create a LocalService object. 65let localServiceInfo: mdns.LocalServiceInfo = { 66 serviceType: "_print._tcp", 67 serviceName: "servicename", 68 port: 5555, 69 host: { 70 address: "10.14.**.***" 71 }, 72 serviceAttribute: [{key: "111", value: [1]}] 73} 74 75// Call addLocalService to add a local service. 76mdns.addLocalService(context, localServiceInfo).then((data: mdns.LocalServiceInfo) => { 77 console.log(JSON.stringify(data)); 78}); 79 80// (Optional) Call resolveLocalService to resolve the local service. 81mdns.resolveLocalService(context, localServiceInfo).then((data: mdns.LocalServiceInfo) => { 82 console.log(JSON.stringify(data)); 83}); 84 85// Call removeLocalService to remove the local service. 86mdns.removeLocalService(context, localServiceInfo).then((data: mdns.LocalServiceInfo) => { 87 console.log(JSON.stringify(data)); 88}); 89``` 90 91## Discovering Local Services 92 931. Connect the device to the Wi-Fi network. 942. Import the **mdns** namespace from **@kit.NetworkKit**. 953. Create a **DiscoveryService** object, which is used to discover MDNS services of the specified type. 964. Subscribe to MDNS service discovery status changes. 975. Enable discovery of MDNS services on the LAN. 986. Stop searching for MDNS services on the LAN. 997. Unsubscribe from MDNS service discovery status changes. 100 101```ts 102// Import the mdns namespace from @kit.NetworkKit. 103import { common, featureAbility, UIAbility } from '@kit.AbilityKit'; 104import { mdns } from '@kit.NetworkKit'; 105import { BusinessError } from '@kit.BasicServicesKit'; 106import { window } from '@kit.ArkUI'; 107 108// Construct a singleton object. 109export class GlobalContext { 110 private constructor() {} 111 private static instance: GlobalContext; 112 private _objects = new Map<string, Object>(); 113 114 public static getContext(): GlobalContext { 115 if (!GlobalContext.instance) { 116 GlobalContext.instance = new GlobalContext(); 117 } 118 return GlobalContext.instance; 119 } 120 121 getObject(value: string): Object | undefined { 122 return this._objects.get(value); 123 } 124 125 setObject(key: string, objectClass: Object): void { 126 this._objects.set(key, objectClass); 127 } 128} 129 130// Obtain the context of the stage model. 131class EntryAbility extends UIAbility { 132 value:number = 0; 133 onWindowStageCreate(windowStage: window.WindowStage): void{ 134 GlobalContext.getContext().setObject("value", this.value); 135 } 136} 137 138let context = GlobalContext.getContext().getObject("value") as common.UIAbilityContext; 139 140// Create a **DiscoveryService** object, which is used to discover MDNS services of the specified type. 141let serviceType = "_print._tcp"; 142let discoveryService = mdns.createDiscoveryService(context, serviceType); 143 144// Subscribe to MDNS service discovery status changes. 145discoveryService.on('discoveryStart', (data: mdns.DiscoveryEventInfo) => { 146 console.log(JSON.stringify(data)); 147}); 148discoveryService.on('discoveryStop', (data: mdns.DiscoveryEventInfo) => { 149 console.log(JSON.stringify(data)); 150}); 151discoveryService.on('serviceFound', (data: mdns.LocalServiceInfo) => { 152 console.log(JSON.stringify(data)); 153}); 154discoveryService.on('serviceLost', (data: mdns.LocalServiceInfo) => { 155 console.log(JSON.stringify(data)); 156}); 157 158// Enable discovery of MDNS services on the LAN. 159discoveryService.startSearchingMDNS(); 160 161// Stop searching for MDNS services on the LAN. 162discoveryService.stopSearchingMDNS(); 163 164// Unsubscribe from MDNS service discovery status changes. 165discoveryService.off('discoveryStart', (data: mdns.DiscoveryEventInfo) => { 166 console.log(JSON.stringify(data)); 167}); 168discoveryService.off('discoveryStop', (data: mdns.DiscoveryEventInfo) => { 169 console.log(JSON.stringify(data)); 170}); 171discoveryService.off('serviceFound', (data: mdns.LocalServiceInfo) => { 172 console.log(JSON.stringify(data)); 173}); 174discoveryService.off('serviceLost', (data: mdns.LocalServiceInfo) => { 175 console.log(JSON.stringify(data)); 176}); 177``` 178