Skip to content

mDNS

stopLocalServiceDiscovery

TIP

The API usage is as follows: wx.stopLocalServiceDiscovery(Object object)

  • Functional description: Stops searching for mDNS services.

  • Parameters and descriptions: Object object。

    PropertiesTypeDefault valueRequiredDescription
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback function for interface call completion (executed regardless of success or failure)
  • object.fail callback function parameter: Object res。

    PropertiesTypeDescription
    errMsgstringError message, its legal value is task not found: stopLocalServiceDiscovery is called when the service is not currently being searched.

startLocalServiceDiscovery

TIP

The API usage is as follows: wx.startLocalServiceDiscovery(Object object)

TIP

  • iOS needs to access the TMFMiniAppExtMDNS extension library to use mDNS related interfaces.
  • wx.startLocalServiceDiscovery is a performance-consuming behavior. It will automatically stop and execute after 30 seconds. wx.onLocalServiceDiscoveryStop Registered callback function.
  • After calling wx.startLocalServiceDiscovery, the next wx.startLocalServiceDiscovery can only be initiated after the current search behavior stops. The operations to stop the current search behavior include calling wx.stopLocalServiceDiscovery and the system automatically stopping the current search after 30 seconds.
  • Functional description: Start searching for mDNS services in the LAN. The search results will be returned through wx.onLocalService* events.

  • Parameters and descriptions: Object object。

    PropertiesTypeDefault valueRequiredDescription
    serviceTypestring-YesType of service to search
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback function for interface call completion (executed regardless of success or failure)
  • object.fail callback function parameter: Object res。

    PropertiesTypeDescription
    errMsgstringError message, legal value is
    invalid param: serviceType is empty
    scan task already exist: If the current search initiated by startLocalServiceDiscovery is not stopped, call startLocalServiceDiscovery again
  • Sample code:

js
wx.startLocalServiceDiscovery({
    // There is a service of type _http._tcp. in the local area network connected to the current mobile phone.
    serviceType: '_http._tcp.',
    success: console.log,
    fail: console.log
})

offLocalServiceResolveFail

TIP

The API usage is as follows: wx.offLocalServiceResolveFail(function listener)

  • Functional description: Remove the listener function for events where mDNS service resolution fails

  • Parameters and descriptions: function listener, Listener function passed in by onLocalServiceResolveFail. If this parameter is not passed, all listeners are removed.

  • Sample code:

js
const listener = function (res) { console.log(res) }

wx.onLocalServiceResolveFail(listener)
wx.offLocalServiceResolveFail(listener)  // Pass the same function object used for listening.

onLocalServiceResolveFail

TIP

The API usage is as follows: wx.onLocalServiceResolveFail(function listener)

  • Functional description: Listen for events where mDNS service resolution fails

  • Parameters and descriptions: function listener, Listener function for events of mDNS service resolution failure, parameter Object res is as follows.

    PropertiesTypeDescription
    serviceTypestringType of service
    serviceNamestringName of service

offLocalServiceLost

TIP

The API usage is as follows: wx.offLocalServiceLost(function listener)

  • Functional description: Remove the listener function for the mDNS service leaving event.

  • Parameters and descriptions: function listener, onLocalServiceLost The listener function passed in. If this parameter is not passed, all listeners are removed.

  • Sample code:

js
const listener = function (res) { console.log(res) }

wx.onLocalServiceLost(listener)
wx.offLocalServiceLost(listener)  // Pass the same function object used for listening.

onLocalServiceLost

TIP

The API usage is as follows: wx.onLocalServiceLost(function listener)

  • Functional description: Listen for the mDNS service leaving event.

  • Parameters and descriptions: function listener, The listener function for the mDNS service leaving event, the parameter Object res is as follows:

    PropertiesTypeDescription
    serviceTypestringType of service
    serviceNamestringName of service

offLocalServiceFound

TIP

The API usage is as follows: wx.offLocalServiceFound(function listener)

  • Functional description: Remove the listener function for the mDNS service discovery event.

  • Parameters and descriptions: function listener, onLocalServiceFound The listener function passed in. If this parameter is not passed, all listeners are removed.

  • Sample code:

js
const listener = function (res) { console.log(res) }

wx.onLocalServiceFound(listener)
wx.offLocalServiceFound(listener) // Pass the same function object used for listening.

onLocalServiceFound

TIP

The API usage is as follows: wx.onLocalServiceFound(function listener)

  • Functional description: Listen for the mDNS service discovery event.

  • Parameters and descriptions: function listener, The listener function for the mDNS service discovery event, the parameter Object res is as follows:

    PropertiesTypeDescription
    serviceTypestringType of service
    serviceNamestringName of service
    ipstringThe ip address of the service
    portnumberThe port of the service

offLocalServiceDiscoveryStop

TIP

The API usage is as follows: wx.offLocalServiceDiscoveryStop(function listener)

  • Functional description: Remove the listener function for the mDNS service stop searching event.

  • Parameters and descriptions: function listener, onLocalServiceDiscoveryStop The listener function passed in. If this parameter is not passed, all listeners are removed.

  • Sample code:

js
const listener = function (res) { console.log(res) }

wx.onLocalServiceDiscoveryStop(listener)
wx.offLocalServiceDiscoveryStop(listener) // Pass the same function object used for listening.

onLocalServiceDiscoveryStop

TIP

The API usage is as follows: wx.onLocalServiceDiscoveryStop(function listener)

  • Functional description: Listen for the mDNS service stop searching event.
  • Parameters and descriptions: function listener, The listener function for the mDNS service stop searching event.