Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the precautions for WeChat Mini Programs to use Socket and how to use it?

Shulou Source: shulou.com Published: 2022-06-01 05:38:17 10月04日 Update

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.

Tags: Programs knowledge articles methods packages matters notes content files folders broadcasts subscriptions different big urgent scratching the cheek for reference only usage overall situation at the same time Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS NVidia OPPO Reno Apple Shulou Information