May 17, 2021 WeChat Mini Program Development Document
When using web-related APIs in small programs/games, you need to be aware of the following issues, please let the developer know in advance.
Each WeChat program needs to set up the communication domain name in advance, the small program can only communicate with the specified domain name network. Includes normal HTTPS requests (wx.request), upload files (wx.uploadFile), download files (wx.downloadFile), and WebSocket communication (wx.connectSocket).
Starting with the base library 2.4.0, the network interface allows communication with the LOCALS IP, but be aware that does not allow communication with the local IP.
Starting at 2.7.0, UDP communication (wx.createUDPSocket) is available.
Server domain name please configure in "Small program background-development-development settings-server domain name", when configuring need to be aware of:
The widget must use HTTPS/WSS to initiate a network request. T he HTTPS certificate used by the server domain name is verified at the time of the request, and if the validation fails, the request cannot be successfully initiated. D ue to system limitations, different platforms have different degrees of rigness to certificate requirements. To ensure the compatibility of small programs, it is recommended that developers configure certificates to the highest standards and use the relevant tools to check that existing certificates meet the requirements.
The certificate requirements are as follows:
Certificate validity can be verified using the opensl s_client -connect example.com:443 command, or you can use other online tools.
In addition to the network request API, other HTTPS requests in the small program are checked as follows if an exception occurs. Such as https pictures can not load, audio and video can not play, etc.
In weChat developer tools, you can temporarily turn on the option of requesting domain names, TLS versions, and HTTPS certificates without verification in the development environment, skipping the verification of server domain names. At this point, the server domain name is not verified in WeChat developer tools and when the phone turns on debugging mode.
After a successful server domain name configuration, it is recommended that developers turn off this option for development and test it under each platform to confirm that the server domain name configuration is correct.
If you see the phenomenon of "On debug mode can make a request, turn off debug mode can make a request" on your phone, confirm that the domain name verification has been skipped and that the server domain name and certificate configuration are correct.
Base Library 2.4.0 provides a range of mDNS APIs, such as wx.startLocalServiceDiscovery, that can be used to obtain IP for devices that provide mDNS services within a local area network. The url parameter of wx.request/wx.connectSocket/wx.uploadFile/wx.downloadFile is allowed to be in the format of $?IP:$?PORT/$?PATH, and the request/connection is successful only if the IP is in the same segment as the mobile IP and is not the same as the local IP (generally speaking, the same local area network, such as a connection under the same wifi).
In this case, the verification of the security domain is not performed, and https/wss are not required, or they can be used.
wx.request({
url: 'http://10.9.176.40:828'
// 省略其他参数
})
wx.connectSocket({
url: 'ws://10.9.176.42:828'
// 省略其他参数
})
The base library 2.7.0 starts with the wx.createUDPSocket interface for UDP communication. The communication rules are the same and only non-local IP under the same LAN is allowed.
Currently, the program only supports obtaining IP from other devices within the LAN through the mDNS protocol. The implementation of the mDNS API on iOS is based on Bonjour, while on Android it is based on the Android system interface.
serviceType
The interface that initiated the mDNS service search wx.startLocalServiceDiscovery has a serviceType parameter that specifies the type of service to search for.
ServiceType format and specification, iOS Bonjour Overview is mentioned in Bonjour Names for Existing Service Types.
Android documentation also mentions this.