Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

WeChat small program API mDNS


May 18, 2021 WeChat Mini Program Development Document


Table of contents


wx.stopLocalServiceDiscovery(Object object)

Base library 2.4.0 starts to support, and low versions need to be compatible.

Stop searching for mDNS services

Parameters

Object object

Property Type The default Required Description
success function Whether The interface calls a successful callback function
fail function Whether The interface calls the failed callback function
complete function Whether Callback function at end of interface call (call succeeds, fails are executed)

object.fail callback function

Parameters
Object res
Property Type Description
errMsg string The error message

The legal value of res.err Msg

Value Description The lowest version
task not found StopLocalServiceDiscovery is not currently in the search service

wx.startLocalServiceDiscovery(Object object)

Base library 2.4.0 starts to support, and low versions need to be compatible.

Start searching for mDNS services under your local area network. The results of the search are returned via the wx.onLocalService event.

Parameters

Object object

Property Type The default Required Description
serviceType string Is The type of service to search for
success function Whether The interface calls a successful callback function
fail function Whether The interface calls the failed callback function
complete function Whether Callback function at end of interface call (call succeeds, fails are executed)

object.fail callback function

Parameters
Object res
Property Type Description
errMsg string The error message

The legal value of res.err Msg

Value Description The lowest version
invalid param ServiceType is empty
scan task already exist Call startLocalServiceDiscovery again if the search initiated by the current startLocalServiceDiscovery does not stop

The sample code


    wx.startLocalServiceDiscovery({
      // 当前手机所连的局域网下有一个 _http._tcp. 类型的服务
      serviceType: '_http._tcp.',
      success: console.log,
      fail: console.log
    })

Attention

  1. wx.startLocalServiceDiscovery is a performance-consuming behavior that automatically stops 30 seconds after the start and performs the callback function registered by wx.onLocalServiceDiscoveryStop.
  2. After calling wx.startLocalServiceDiscovery, the next wx.startLocalServiceDiscovery can only be initiated after the search behavior is stopped. Stopping this search behavior includes calling wx.stopLocalServiceDiscovery and automatically stopping the search after 30 seconds.

wx.onLocalServiceResolveFail(function callback)

Base library 2.4.0 starts to support, and low versions need to be compatible.

Listen for events that fail to be resolved by the mDNS service

Parameters

function callback

The callback function for the mDNS service to resolve failed events

Parameters

Object res
Property Type Description
serviceType string The type of service
serviceName string The name of the service

wx.onLocalServiceLost(function callback)

Base library 2.4.0 starts to support, and low versions need to be compatible.

Listens for events that the mDNS service leaves

Parameters

function callback

The callback function for the event that the mDNS service leaves

Parameters

Object res
Property Type Description
serviceType string The type of service
serviceName string The name of the service

wx.onLocalServiceFound(function callback)

Base library 2.4.0 starts to support, and low versions need to be compatible.

Listen for events discovered by the mDNS service

Parameters

function callback

The callback function for events discovered by the mDNS service

Parameters

Object res
Property Type Description
serviceType string The type of service
serviceName string The name of the service
Ip string The ip address of the service
port number The port of the service

wx.onLocalServiceDiscoveryStop(function callback)

Base library 2.4.0 starts to support, and low versions need to be compatible.

Listen for events when the mDNS service stops searching

Parameters

function callback

The callback function for events that the mDNS service stops searching for


wx.offLocalServiceResolveFail(function callback)

Base library 2.4.0 starts to support, and low versions need to be compatible.

Cancel listening to the mDNS service to resolve the failed event

Parameters

function callback

The callback function for the mDNS service to resolve failed events


wx.offLocalServiceLost(function callback)

Base library 2.4.0 starts to support, and low versions need to be compatible.

Cancel the event that the mDNS service left

Parameters

function callback

The callback function for the event that the mDNS service leaves


wx.offLocalServiceFound(function callback)

Base library 2.4.0 starts to support, and low versions need to be compatible.

Cancel the events discovered by the mDNS service

Parameters

function callback

The callback function for events discovered by the mDNS service


wx.offLocalServiceDiscoveryStop(function callback)

Base library 2.4.0 starts to support, and low versions need to be compatible.

Cancel the event that the mDNS service stopped the search

Parameters

function callback

The callback function for events that the mDNS service stops searching for