In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
The editor of this article gives you a detailed introduction of "how to realize a chat room in WeChat Mini Programs". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to realize a chat room in WeChat Mini Programs" can help you solve your doubts. Let's follow the editor's ideas to learn new knowledge.
How to realize chat Room in WeChat Mini Programs
Websoctet.js file under the utils folder
Var url = 'ws:// address port'; function connect (user, func) {wx.connectSocket ({url: url, header: {"content-type": 'application/x-www-form-urlencoded'}}); wx.onSocketOpen (function (res) {send (' {"type": "login", "client_name": "'+ user.nickName+'", "room_id": "1"}')}); / / accept message wx.onSocketMessage (func) } / / send message function send (msg) {wx.sendSocketMessage ({data: msg});} module.exports = {connect: connect, send: send}
Specific page .js file content:
Var websocket = require ('.. /.. / utils/websocket.js') / / event handling function add: function (e) {websocket.send ('{"type": "say", "from_client_id": "'+ user.nickName+'", "to_client_id": "all", "content": "'+ this.data.message+'"}')}, onLoad: function () {var that = this / / call the method of the application instance to obtain global data app.getUserInfo (function (userInfo) {user = userInfo) Websocket.connect (user, function (res) {text = that.encodeStr (res.data) + "\ n"; console.log (res) that.setData ({text: text}); / / websocket.send ('{"type": "pong"}');})})}
The following method is very interesting, because the server used is an open source PHP server, so the Chinese content of the chat room received by WeChat Mini Programs is ASCII encoded, so it can be converted by this method.
EncodeStr: function (str) {var character = str.split ("\\ u"); var native1 = character [0]; for (var I = 1; I
< character.length; i++) { var code = character[i]; native1 += String.fromCharCode(parseInt("0x" + code.substring(0, 4))); if (code.length >4) {native1 + = code.substring (4, code.length);}} return native1}, reading here, this article "how to implement chat rooms in WeChat Mini Programs" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it before you can understand it. If you want to know more about related articles, welcome to follow 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.