In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about how to receive event push and message reordering in Wechat development. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
If the Wechat server does not receive a response within 5 seconds, it will disconnect and reinitiate the request for a total of three retries. In that case, the problem arises. There is such a scenario: when users follow the Wechat account, get the current user information, and then write the information to the database. Similar to the registration of PC-side websites. Perhaps because of this concern, the business logic we need to deal with is more complex. Such as sending points, writing user logs, and assigning user groups. Wait a minute... If a series of logic needs to be executed, or the network environment is so complex that there is no guarantee to respond to the current user's operation within 5 seconds, if the Wechat server pushes the same attention event to our server when the operation is not completed, we will execute our logic again, which may lead to duplicate data in the database (some children's shoes will say it). I determine whether it already exists before inserting the data, and if so, I do not perform the insert operation. What I want to say is that I thought the same way, but there was a gap between the real running environment and our debugging environment. I didn't realize the importance of message deduplication until I found that there was a lot of duplicate user information in the database. ).
There is a difference between a normal message and an event message. Normal messages use msgid, while event messages use FromUserName + CreateTime. My train of thought is:
Create a new class BaseMsg with three properties that are FromUser,MsgFlag,CreateTime. The code is as follows:
Public class BaseMsg {/ Sender ID / public string FromUser {get; set;} / message representation. Normal message, msgid, event message, creation time / public string MsgFlag {get; set;} / public DateTime CreateTime {get; set;}} added to the queue
Create a static list _ queue, which is used to store the message list, which is of type List.
Before processing the message body of Wechat, first determine whether the list is instantiated, if not, then instantiate, otherwise determine whether the length of the list is greater than or equal to 50 (this can be customized, the use is the number of messages concurrent by Wechat), if greater than or equal to 50, then retain the unanswered messages within 20 seconds (5 seconds retry, a total of 3 retries, that is, 15 seconds, just to be on the safe side, write here 20 seconds).
Gets the message type of the current message body and determines whether the current message has been requested based on _ queue. If it is an event, save the FromUser and creation time. Save MsgFlag if it is a normal message. Here is the code:
If (_ queue = = null) {_ queue = new List ();} else if (_ queue.Count > = 50) {_ queue = _ queue.Where (Q = > {return q.CreateTime.AddSeconds (20) > DateTime.Now;}). ToList () / / keep messages that do not respond within 20 seconds} XElement xdoc = XElement.Parse (xml); var msgtype = xdoc.Element ("MsgType"). Value.ToUpper (); var FromUserName = xdoc.Element ("FromUserName"). Value; var MsgId = xdoc.Element ("MsgId"). Value; var CreateTime = xdoc.Element ("CreateTime"). Value MsgType type = (MsgType) Enum.Parse (typeof (MsgType), msgtype); if (typewritten categories MsgType.event) {if (_ queue.FirstOrDefault (m = > {return m.MsgFlag = = MsgId) }) = = null) {_ queue.Add (new BaseMsg {CreateTime = DateTime.Now, FromUser = FromUserName, MsgFlag = MsgId}) } else {return null;}} else {if (_ queue.FirstOrDefault (m = > {return m.MsgFlag = = CreateTime) }) = = null) {_ queue.Add (new BaseMsg {CreateTime = DateTime.Now, FromUser = FromUserName, MsgFlag = CreateTime}) } else {return null;}}
When the message already exists in the queue, the current message is not converted into an entity, and the null is returned directly. When called, no processing is done when the null is returned.
Let's start with event messages. Let's continue with the previous article. All messages inherit BaseMessage, and all event types contain a property of Event. Here, to facilitate the call, the message type is defined as enumeration, as follows:
/ event type enumeration / public enum Event {/ non-event type / NOEVENT, / subscribe / SUBSCRIBE, / unsubscribe / UNSUBSCRIBE / / scan the QR code with parameters / SCAN, / Geographic location / LOCATION, / Click the button / CLICK / Link button / VIEW, / scan code push event / SCANCODE_PUSH / / scan code push event and pop up "message receiving" prompt box / SCANCODE_WAITMSG / / pop-up system to take photos and send pictures / PIC_SYSPHOTO, / pop-up photos or albums to send pictures / PIC_PHOTO_OR_ALBUM, / / PIC_PHOTO_OR_ALBUM / PIC_PHOTO_OR_ALBUM / / PIC_WEIXIN / pop-up geolocation selector / LOCATION_SELECT, / template message push / TEMPLATESENDJOBFINISH}
Once the enumeration is defined, the message entity is defined.
Follow / unfollow events
The xml packet is as follows:
123456789
Corresponding entity:
/ subscribe / unsubscribe event / public class SubEventMessage: EventMessage {private string _ eventkey; / event key value with qrscene_ as the prefix, followed by the parameter value of the QR code (the prefix has been removed and can be used directly) / public string EventKey {get {return _ eventkey } set {_ eventkey = value.Replace ("qrscene_", "");} / the ticket of the QR code, which can be exchanged for the QR code picture / public string Ticket {get; set;}}
It should be noted that when the user scans the QR code with parameters, if the user does not follow the current official account, the user will bring the qrscene_ parameter and Ticket in the message body, so two attributes are defined here: EventKey,Ticket. When assigning a value to EventKey, replace qrscene_, because what we really need is the following parameters.
Scan for QR code events with parameters
When a user scans a QR code with scene values, two events may be pushed:
If the user has not followed the official account, the user can follow the official account. After following, Wechat will push the event with scene value to the developer.
If the user has already followed the official account, Wechat will push the scanning event with scene value to the developer. 、
The first one has already been mentioned above, but here I will only explain the second one.
Event push when the user has followed it
The xml package is as follows:
123456789
The corresponding entities are as follows:
/ scan the QR code entity with parameters / public class ScanEventMessage: EventMessage {/ event key value, which is a 32-bit unsigned integer, that is, the QR code scene_id / public string EventKey {get; set when the QR code is created. } / ticket of QR code, which can be used in exchange for QR code picture / public string Ticket {get; set;}} to report geolocation events
When the official account is enabled to report the geographical location, every time you enter the official account session, after the user agrees to report the geographical location, it will report the geographical location when entering, or every 5 seconds after entering the reply call. The official account can modify the settings in the backstage of the public platform. When reporting a geographic location, WeChat will push the reported geographic location event to the url filled in by the developer.
The xml packet is as follows:
12345678923.137466113.352425119.385040
The corresponding entities are as follows:
/ report Geographic location entity / public class LocationEventMessage: EventMessage {/ Latitude / public string Latitude {get; set;} / Longitude / public string Longitude {get; set } / geolocation accuracy / public string Precision {get; set;}}
Common events for custom menu events are: click,view,scancode_puth,scancode_waitmsg,location_select. In addition, there are three kinds of events, because they are not commonly used, and the author did not expect to use the scene, so I will not tell them one by one again. Those who are interested can study by themselves or communicate with me.
Xml packets pushed by click events:
123456789
The format of the xml packet pushed by the view event is the same as that of click, so you can define a class as follows:
/ / normal menu events, including click and view / public class NormalMenuEventMessage: EventMessage {/ event key value, set jump URL / public string EventKey {get; set;}}
The xml packet for the scancode event is as follows:
1419265698
The corresponding entities are as follows:
/ menu scan event / public class ScanMenuEventMessage: EventMessage {/ event key value / public string EventKey {get; set;} / scan code type. Qrcode is a QR code, the rest is the bar code / public string ScanType {get; set;} / public string ScanResult {get; set;}}
At this point, all the commonly used event type messages have been defined. Combined with the previous article, the complete code for converting xml packets into objects is as follows:
Public class MessageFactory {private static List _ queue; public static BaseMessage CreateMessage (string xml) {if (_ queue = = null) {_ queue = new List ();} else if (_ queue.Count > = 50) {_ queue = _ queue.Where (Q = > {return q.CreateTime.AddSeconds (20) > DateTime.Now }) .ToList (); / keep messages that have not responded within 20 seconds} XElement xdoc = XElement.Parse (xml); var msgtype = xdoc.Element ("MsgType"). Value.ToUpper (); var FromUserName = xdoc.Element ("FromUserName"). Value; var MsgId = xdoc.Element ("MsgId"). Value; var CreateTime = xdoc.Element ("CreateTime"). Value MsgType type = (MsgType) Enum.Parse (typeof (MsgType), msgtype); if (typewritten categories MsgType.event) {if (_ queue.FirstOrDefault (m = > {return m.MsgFlag = = MsgId) }) = = null) {_ queue.Add (new BaseMsg {CreateTime = DateTime.Now, FromUser = FromUserName, MsgFlag = MsgId}) } else {return null;}} else {if (_ queue.FirstOrDefault (m = > {return m.MsgFlag = = CreateTime) }) = = null) {_ queue.Add (new BaseMsg {CreateTime = DateTime.Now, FromUser = FromUserName, MsgFlag = CreateTime}) } else {return null;}} switch (type) {case MsgType.TEXT: return Utils.ConvertObj (xml); case MsgType.IMAGE: return Utils.ConvertObj (xml) Case MsgType.VIDEO: return Utils.ConvertObj (xml); case MsgType.VOICE: return Utils.ConvertObj (xml); case MsgType.LINK: return Utils.ConvertObj (xml); case MsgType.LOCATION: return Utils.ConvertObj (xml) Case MsgType.EVENT:// event type {var eventtype = (Event) Enum.Parse (typeof (Event), xdoc.Element ("Event") .Value.ToUpper ()) Switch (eventtype) {case Event.CLICK: return Utils.ConvertObj (xml); case Event.VIEW: return Utils.ConvertObj (xml); case Event.LOCATION: return Utils.ConvertObj (xml) Case Event.LOCATION_SELECT: return Utils.ConvertObj (xml); case Event.SCAN: return Utils.ConvertObj (xml); case Event.SUBSCRIBE: return Utils.ConvertObj (xml); case Event.UNSUBSCRIBE: return Utils.ConvertObj (xml); case Event.SCANCODE_WAITMSG: return Utils.ConvertObj (xml) Default: return Utils.ConvertObj (xml);}} break; default: return Utils.ConvertObj (xml);} Thank you for reading! This is the end of the article on "how to receive event push and message scheduling in Wechat development". I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, you can share it for more people to see!
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