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```ts 48// Import the mdns namespace from @kit.NetworkKit. 49import { mdns } from '@kit.NetworkKit'; 50import { BusinessError } from '@kit.BasicServicesKit'; 51import { featureAbility } from '@kit.AbilityKit'; 52 53let context = getContext(this) as Context; 54 55class ServiceAttribute { 56 key: string = "111" 57 value: Array<number> = [1] 58} 59 60// Create a LocalService object. 61let localServiceInfo: mdns.LocalServiceInfo = { 62 serviceType: "_print._tcp", 63 serviceName: "servicename", 64 port: 5555, 65 host: { 66 address: "10.14.**.***" 67 }, 68 serviceAttribute: [{key: "111", value: [1]}] 69} 70 71// Call addLocalService to add a local service. 72mdns.addLocalService(context, localServiceInfo).then((data: mdns.LocalServiceInfo) => { 73 console.log(JSON.stringify(data)); 74}); 75 76// (Optional) Call resolveLocalService to resolve the local service. 77mdns.resolveLocalService(context, localServiceInfo).then((data: mdns.LocalServiceInfo) => { 78 console.log(JSON.stringify(data)); 79}); 80 81// Call removeLocalService to remove the local service. 82mdns.removeLocalService(context, localServiceInfo).then((data: mdns.LocalServiceInfo) => { 83 console.log(JSON.stringify(data)); 84}); 85``` 86 87## Discovering Local Services 88 891. Connect the device to the Wi-Fi network. 902. Import the **mdns** namespace from **@kit.NetworkKit**. 913. Create a **DiscoveryService** object, which is used to discover MDNS services of the specified type. 924. Subscribe to MDNS service discovery status changes. 935. Enable discovery of MDNS services on the LAN. 946. Stop searching for MDNS services on the LAN. 957. Unsubscribe from MDNS service discovery status changes. 96 97```ts 98// Import the mdns namespace from @kit.NetworkKit. 99import { common, featureAbility, UIAbility } from '@kit.AbilityKit'; 100import { mdns } from '@kit.NetworkKit'; 101import { BusinessError } from '@kit.BasicServicesKit'; 102import { window } from '@kit.ArkUI'; 103 104// Construct a singleton object. 105export class GlobalContext { 106 private constructor() {} 107 private static instance: GlobalContext; 108 private _objects = new Map<string, Object>(); 109 110 public static getContext(): GlobalContext { 111 if (!GlobalContext.instance) { 112 GlobalContext.instance = new GlobalContext(); 113 } 114 return GlobalContext.instance; 115 } 116 117 getObject(value: string): Object | undefined { 118 return this._objects.get(value); 119 } 120 121 setObject(key: string, objectClass: Object): void { 122 this._objects.set(key, objectClass); 123 } 124} 125 126// Obtain the context of the stage model. 127class EntryAbility extends UIAbility { 128 value:number = 0; 129 onWindowStageCreate(windowStage: window.WindowStage): void{ 130 GlobalContext.getContext().setObject("value", this.value); 131 } 132} 133 134let context = GlobalContext.getContext().getObject("value") as common.UIAbilityContext; 135 136// Create a **DiscoveryService** object, which is used to discover MDNS services of the specified type. 137let serviceType = "_print._tcp"; 138let discoveryService = mdns.createDiscoveryService(context, serviceType); 139 140// Subscribe to MDNS service discovery status changes. 141discoveryService.on('discoveryStart', (data: mdns.DiscoveryEventInfo) => { 142 console.log(JSON.stringify(data)); 143}); 144discoveryService.on('discoveryStop', (data: mdns.DiscoveryEventInfo) => { 145 console.log(JSON.stringify(data)); 146}); 147discoveryService.on('serviceFound', (data: mdns.LocalServiceInfo) => { 148 console.log(JSON.stringify(data)); 149}); 150discoveryService.on('serviceLost', (data: mdns.LocalServiceInfo) => { 151 console.log(JSON.stringify(data)); 152}); 153 154// Enable discovery of MDNS services on the LAN. 155discoveryService.startSearchingMDNS(); 156 157// Stop searching for MDNS services on the LAN. 158discoveryService.stopSearchingMDNS(); 159 160// Unsubscribe from MDNS service discovery status changes. 161discoveryService.off('discoveryStart', (data: mdns.DiscoveryEventInfo) => { 162 console.log(JSON.stringify(data)); 163}); 164discoveryService.off('discoveryStop', (data: mdns.DiscoveryEventInfo) => { 165 console.log(JSON.stringify(data)); 166}); 167discoveryService.off('serviceFound', (data: mdns.LocalServiceInfo) => { 168 console.log(JSON.stringify(data)); 169}); 170discoveryService.off('serviceLost', (data: mdns.LocalServiceInfo) => { 171 console.log(JSON.stringify(data)); 172}); 173``` 174