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 callback mode. For details about the APIs, see [mDNS Management](../reference/apis/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 APIs and example code, see [mDNS Management](../reference/apis/js-apis-net-mdns.md). 21 22| Type| API| Description| 23| ---- | ---- | ---- | 24| ohos.net.mdns | addLocalService(context: Context, serviceInfo: LocalServiceInfo, callback: AsyncCallback\<LocalServiceInfo>): void | Adds an mDNS service. This API uses an asynchronous callback to return the result.| 25| ohos.net.mdns | removeLocalService(context: Context, serviceInfo: LocalServiceInfo, callback: AsyncCallback\<LocalServiceInfo>): void | Removes an mDNS service. This API uses an asynchronous callback to return the result.| 26| ohos.net.mdns | createDiscoveryService(context: Context, serviceType: string): DiscoveryService | Creates a **DiscoveryService** object, which is used to discover mDNS services of the specified type.| 27| ohos.net.mdns | resolveLocalService(context: Context, serviceInfo: LocalServiceInfo, callback: AsyncCallback\<LocalServiceInfo>): void | Resolves an mDNS service. This API uses an asynchronous callback to return the result.| 28| ohos.net.mdns.DiscoveryService | startSearchingMDNS(): void | Searches for mDNS services on the LAN.| 29| ohos.net.mdns.DiscoveryService | stopSearchingMDNS(): void | Stops searching for mDNS services on the LAN.| 30| ohos.net.mdns.DiscoveryService | on(type: 'discoveryStart', callback: Callback<{serviceInfo: LocalServiceInfo, errorCode?: MdnsError}>): void | Enables listening for **discoveryStart** events.| 31| ohos.net.mdns.DiscoveryService | off(type: 'discoveryStart', callback?: Callback<{ serviceInfo: LocalServiceInfo, errorCode?: MdnsError }>): void | Disables listening for **discoveryStart** events.| 32| ohos.net.mdns.DiscoveryService | on(type: 'discoveryStop', callback: Callback<{serviceInfo: LocalServiceInfo, errorCode?: MdnsError}>): void | Enables listening for **discoveryStop** events.| 33| ohos.net.mdns.DiscoveryService | off(type: 'discoveryStop', callback?: Callback<{ serviceInfo: LocalServiceInfo, errorCode?: MdnsError }>): void | Disables listening for **discoveryStop** events.| 34| ohos.net.mdns.DiscoveryService | on(type: 'serviceFound', callback: Callback\<LocalServiceInfo>): void | Enables listening for **serviceFound** events.| 35| ohos.net.mdns.DiscoveryService | off(type: 'serviceFound', callback?: Callback\<LocalServiceInfo>): void | Disables listening for **serviceFound** events.| 36| ohos.net.mdns.DiscoveryService | on(type: 'serviceLost', callback: Callback\<LocalServiceInfo>): void | Enables listening for **serviceLost** events.| 37| ohos.net.mdns.DiscoveryService | 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 **@ohos.net.mdns**. 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 @ohos.net.mdns. 49import mdns from '@ohos.net.mdns' 50import UIAbility from '@ohos.app.ability.UIAbility'; 51import { BusinessError } from '@ohos.base'; 52import featureAbility from '@ohos.ability.featureAbility'; 53import window from '@ohos.window'; 54 55// Construct a singleton object. 56export class GlobalContext { 57 private constructor() {} 58 private static instance: GlobalContext; 59 private _objects = new Map<string, Object>(); 60 61 public static getContext(): GlobalContext { 62 if (!GlobalContext.instance) { 63 GlobalContext.instance = new GlobalContext(); 64 } 65 return GlobalContext.instance; 66 } 67 68 getObject(value: string): Object | undefined { 69 return this._objects.get(value); 70 } 71 72 setObject(key: string, objectClass: Object): void { 73 this._objects.set(key, objectClass); 74 } 75} 76 77// Obtain the context of the stage model. 78class EntryAbility extends UIAbility { 79 value: number = 0; 80 onWindowStageCreate(windowStage: window.WindowStage): void{ 81 GlobalContext.getContext().setObject("value", this.value); 82 } 83} 84let context = GlobalContext.getContext().getObject("value"); 85 86class ServiceAttribute { 87 key: string = "111" 88 value: Array<number> = [1] 89} 90 91// Create a LocalService object. 92let localServiceInfo: mdns.LocalServiceInfo = { 93 serviceType: "_print._tcp", 94 serviceName: "servicename", 95 port: 5555, 96 host: { 97 address: "10.14.**.***" 98 }, 99 serviceAttribute: [{key: "111", value: [1]}] 100} 101 102// Call addLocalService to add a local service. 103mdns.addLocalService(context as Context, localServiceInfo, (error: BusinessError, data: mdns.LocalServiceInfo) => { 104 console.log(JSON.stringify(error)); 105 console.log(JSON.stringify(data)); 106}); 107 108// (Optional) Call resolveLocalService to resolve the local service. 109mdns.resolveLocalService(context as Context, localServiceInfo, (error: BusinessError, data: mdns.LocalServiceInfo) => { 110 console.log(JSON.stringify(error)); 111 console.log(JSON.stringify(data)); 112}); 113 114// Call removeLocalService to remove the local service. 115mdns.removeLocalService(context as Context, localServiceInfo, (error: BusinessError, data: mdns.LocalServiceInfo) => { 116 console.log(JSON.stringify(error)); 117 console.log(JSON.stringify(data)); 118}); 119``` 120 121## Discovering Local Services 122 1231. Connect the device to the Wi-Fi network. 1242. Import the **mdns** namespace from **@ohos.net.mdns**. 1253. Create a **DiscoveryService** object, which is used to discover mDNS services of the specified type. 1264. Subscribe to mDNS service discovery status changes. 1275. Enable discovery of mDNS services on the LAN. 1286. Stop searching for mDNS services on the LAN. 1297. Unsubscribe from mDNS service discovery status changes. 130 131```ts 132// Import the mdns namespace from @ohos.net.mdns. 133import mdns from '@ohos.net.mdns' 134import UIAbility from '@ohos.app.ability.UIAbility'; 135import { BusinessError } from '@ohos.base'; 136import featureAbility from '@ohos.ability.featureAbility'; 137import window from '@ohos.window'; 138 139// Construct a singleton object. 140export class GlobalContext { 141 private constructor() {} 142 private static instance: GlobalContext; 143 private _objects = new Map<string, Object>(); 144 145 public static getContext(): GlobalContext { 146 if (!GlobalContext.instance) { 147 GlobalContext.instance = new GlobalContext(); 148 } 149 return GlobalContext.instance; 150 } 151 152 getObject(value: string): Object | undefined { 153 return this._objects.get(value); 154 } 155 156 setObject(key: string, objectClass: Object): void { 157 this._objects.set(key, objectClass); 158 } 159} 160 161// Obtain the context of the stage model. 162class EntryAbility extends UIAbility { 163 value:number = 0; 164 onWindowStageCreate(windowStage: window.WindowStage): void{ 165 GlobalContext.getContext().setObject("value", this.value); 166 } 167} 168 169let context = GlobalContext.getContext().getObject("value"); 170 171// Create a DiscoveryService object, which is used to discover mDNS services of the specified type. 172let serviceType = "_print._tcp"; 173let discoveryService = mdns.createDiscoveryService(context as Context, serviceType); 174 175class DataServiceInfo{ 176 serviceInfo: mdns.LocalServiceInfo|null = null 177 errorCode?: mdns.MdnsError = mdns.MdnsError.INTERNAL_ERROR 178} 179// Subscribe to mDNS service discovery status changes. 180discoveryService.on('discoveryStart', (data: DataServiceInfo) => { 181 console.log(JSON.stringify(data)); 182}); 183discoveryService.on('discoveryStop', (data: DataServiceInfo) => { 184 console.log(JSON.stringify(data)); 185}); 186discoveryService.on('serviceFound', (data: mdns.LocalServiceInfo) => { 187 console.log(JSON.stringify(data)); 188}); 189discoveryService.on('serviceLost', (data: mdns.LocalServiceInfo) => { 190 console.log(JSON.stringify(data)); 191}); 192 193// Enable discovery of mDNS services on the LAN. 194discoveryService.startSearchingMDNS(); 195 196// Stop searching for mDNS services on the LAN. 197discoveryService.stopSearchingMDNS(); 198 199// Unsubscribe from mDNS service discovery status changes. 200discoveryService.off('discoveryStart', (data: DataServiceInfo) => { 201 console.log(JSON.stringify(data)); 202}); 203discoveryService.off('discoveryStop', (data: DataServiceInfo) => { 204 console.log(JSON.stringify(data)); 205}); 206discoveryService.off('serviceFound', (data: mdns.LocalServiceInfo) => { 207 console.log(JSON.stringify(data)); 208}); 209discoveryService.off('serviceLost', (data: mdns.LocalServiceInfo) => { 210 console.log(JSON.stringify(data)); 211}); 212``` 213