In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, I would like to share with you what WeChat Mini Programs has to pay attention to using Socket and how to use it. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.
First of all, a Mini Program can only have one WebSocket connection at a time. If a WebSocket connection already exists, the current connection will be closed and a new one will be established.
Second, if appID is used, the protocol must be wss://...
Recently, the team used Mini Program to do the quotation. When they connected to socket, they found that they broadcast directly without subscribe, and automatically closed the socket connection.
When time was running out, I scratched my head, so I quoted socket-io (websocket imitation socket-io based on Mini Program, not the official socket-io, portal), how to use it:
1. Paste an io js in the utils folder first.
2. Then npm install wxapp-socket-io
3. Create a new socket.js under the config folder to encapsulate the socket connection, as follows:
Const io = require ('.. / utils/io.js') let url = 'wss://.'let wsStatus = falselet onSocket = nullexport const connect = function (cb) {if (! onSocket) {onSocket = io (url) onSocket.on (' connect', function (res) {cb (true,onSocket) wsStatus = true}) setTimeout (function () {if (! wsStatus) {cb (false,onSocket)}, 10000)} else {cb (true,onSocket)} 4, call the page to be referenced Globally encapsulated subscription method
Let openSocket = require ('.. /.. / config/socket') let app = getApp () let socket = nullPage ({data: {zl: [422,400,468,834,746,845,517,630,797,890,529,553, 425,469,470,837,841,521,525], [422,400,468,834,785,446, 845,517,630,797,890,529,553, 425,469, 470,837,841,521,525]] OnLoad: function () {let that = this / / socket calls openSocket.connect (function (status, ws) {if (status) {socket = wsthis.subscribe ('zl') / / a pair of encapsulated calls to subscription methods socket.on (' broadcast', function (msg) {/ / broadcast console.log ("broadcast"); console.log (msg);})} else {alert ("socket connection failure")}) }, subscribe: function (type) {if (socket) {let eis = this.data [type] if (eis & & eis.length > 0) {let param = {/ / for reference only, change eis: eis.join (',')} socket.emit ('subscribe', JSON.stringify (param);}) These are all the contents of this article entitled "what are the precautions for WeChat Mini Programs to use Socket and how to use it?" Thank you for your reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.