亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Home WeChat Applet WeChat Development Introduction to the method of using C# to develop WeChat public accounts to receive event push and message deduplication

Introduction to the method of using C# to develop WeChat public accounts to receive event push and message deduplication

Mar 19, 2017 pm 06:00 PM
c# WeChat public account

This article mainly introduces the method of receiving event push and message deduplication using C# to develop WeChat public account. It analyzes the usage skills of event push and message deduplication in detail. It has certain reference value for WeChat development. It is needed Friends can refer to

. This article describes the method of receiving event push and message deduplication in C# WeChat public account development. Share it with everyone for your reference. The specific analysis is as follows:

If the WeChat server does not receive a response within 5 seconds, it will disconnect the connection and re-initiate the request, retrying three times in total. In this case, the problem arises. There is a scenario: when a user follows a WeChat account, the current user information is obtained, and then the information is written to the database. Similar to registration on PC website. Perhaps because of this concern, the business logic we need to handle is relatively complex. Such as sending points, writing user logs, and assigning user groups. Wait... A series of logic needs to be executed, or the network environment is relatively complex and there is no guarantee that the current user's operation will be responded to within 5 seconds. Then if the operation is not completed yet, the WeChat server pushes the same attention event to our server. We will execute our logic again, which may lead to duplicate data in the database (some children's shoes will say, before inserting the data, I first determine whether it already exists, and if it exists, the insertion will not be performed. Operation. What I want to say is that I thought so too at first, but there is still a gap between the real operating environment and our debugging environment. It was not until I found that there was a lot of duplicate user information in the database that I discovered the message deduplication importance.).


There is a difference between deduplication of messages. Ordinary messages and event messages. Ordinary messages use msgid, while event messages use FromUserName + CreateTime. My idea is:

Create a new class BaseMsg with three attributes: FromUser, MsgFlag, and CreateTime. The code is as follows:

The code is as follows:

public class BaseMsg
{
        /// <summary>
        /// 發(fā)送者標識
        /// </summary>
        public string FromUser { get; set; }
        /// <summary>
        /// 消息表示。普通消息時,為msgid,事件消息時,為事件的創(chuàng)建時間
        /// </summary>
        public string MsgFlag { get; set; }
        /// <summary>
        /// 添加到隊列的時間
        /// </summary>
        public DateTime CreateTime { get; set; }
}



Create a static list_queue to store the message list. The type of the list is List.
Before processing the WeChat message body, first determine whether the list is instantiated. If not, instantiate it. Otherwise, determine whether the length of the list is greater than or equal to 50 (this can be customized and is used for the concurrent message volume of WeChat). If it is greater than or equal to 50, messages that have not responded within 20 seconds will be retained (retry once every 5 seconds, a total of 3 retries, which is 15 seconds, and 20 seconds will be written here to be on the safe side).
Get the message type of the current message body, and determine whether the current message has been requested based on _queue. If it is an event, FromUser and creation time are saved. If it is a normal message, MsgFlag is saved. The following is the code:

The code is as follows:

if (_queue == null)
{
 _queue = new List<BaseMsg>();
}
else if(_queue.Count>=50)
{
 _queue = _queue.Where(q => { return q.CreateTime.AddSeconds(20) > DateTime.Now; }).ToList();//保留20秒內(nèi)未響應的消息
}
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 (type!=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 will not be converted into an entity, and null will be returned directly. , when calling, no processing will be done when null is returned.

Let’s start with the event message. Continuing from the previous article. All messages inherit BaseMessage, and all event types contain an Event property. For the convenience of calling, the message

code is as follows:

/// <summary>
/// 事件類型枚舉
/// </summary>
public enum Event
{
        /// <summary>
        /// 非事件類型
        /// </summary>
        NOEVENT,
        /// <summary>
        /// 訂閱
        /// </summary>
        SUBSCRIBE,
        /// <summary>
        /// 取消訂閱
        /// </summary>
        UNSUBSCRIBE,
        /// <summary>
        /// 掃描帶參數(shù)的二維碼
        /// </summary>
        SCAN,
        /// <summary>
        /// 地理位置
        /// </summary>
        LOCATION,
        /// <summary>
        /// 單擊按鈕
        /// </summary>
        CLICK,
        /// <summary>
        /// 鏈接按鈕
        /// </summary>
        VIEW,
        /// <summary>
        /// 掃碼推事件
        /// </summary>
        SCANCODE_PUSH,
        /// <summary>
        /// 掃碼推事件且彈出“消息接收中”提示框
        /// </summary>
        SCANCODE_WAITMSG,
        /// <summary>
        /// 彈出系統(tǒng)拍照發(fā)圖
        /// </summary>
        PIC_SYSPHOTO,
        /// <summary>
        /// 彈出拍照或者相冊發(fā)圖
        /// </summary>
        PIC_PHOTO_OR_ALBUM,
        /// <summary>
        /// 彈出微信相冊發(fā)圖器
        /// </summary>
        PIC_WEIXIN,
        /// <summary>
        /// 彈出地理位置選擇器
        /// </summary>
        LOCATION_SELECT,
        /// <summary>
        /// 模板消息推送
        /// </summary>
        TEMPLATESENDJOBFINISH
}



After defining the enumeration, it is time to define the message entity.

Follow/unfollow event
xml data packet is as follows:

The code is as follows:

<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[FromUser]]></FromUserName>
<CreateTime>123456789</CreateTime>
<MsgType><![CDATA[event]]></MsgType>
<Event><![CDATA[subscribe]]></Event>
</xml>



Corresponding entity:

The code is as follows:

/// <summary>
/// 訂閱/取消訂閱事件
/// </summary>
public class SubEventMessage : EventMessage
{
        private string _eventkey;
        /// <summary>
        /// 事件KEY值,qrscene_為前綴,后面為二維碼的參數(shù)值(已去掉前綴,可以直接使用)
        /// </summary>
        public string EventKey
        {
            get { return _eventkey; }
            set { _eventkey = value.Replace("qrscene_", ""); }
        }
        /// <summary>
        /// 二維碼的ticket,可用來換取二維碼圖片
        /// </summary>
        public string Ticket { get; set; }
}



It should be noted here that when the user scans the QR code with parameters, if the user does not follow the current official account, the user will follow When , the qrscene_ parameter and Ticket will be included in the message body, so two attributes are defined here: EventKey and Ticket. When assigning a value to EventKey, replace qrscene_, because what we really need is the following parameters.

Scanning QR code event with parameters
When the user scans the QR code with scene value, two events may be pushed:

If the user has not followed the official account, the user You can follow the official account. After following, WeChat will push the following events with scene values ??to the developers.
If the user has followed the official account, WeChat will push the scanning event with scene value to the developer. ,
The first type has been discussed above, and only the second type will be explained here.

Event push when the user has paid attention

xml package is as follows:

The code is as follows:

<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[FromUser]]></FromUserName>
<CreateTime>123456789</CreateTime>
<MsgType><![CDATA[event]]></MsgType>
<Event><![CDATA[SCAN]]></Event>
<EventKey><![CDATA[SCENE_VALUE]]></EventKey>
<Ticket><![CDATA[TICKET]]></Ticket>
</xml>



corresponding The entities are as follows:

The code is as follows:

/// <summary>
/// 掃描帶參數(shù)的二維碼實體
/// </summary>
public class ScanEventMessage : EventMessage
{
 
        /// <summary>
        /// 事件KEY值,是一個32位無符號整數(shù),即創(chuàng)建二維碼時的二維碼scene_id
        /// </summary>
        public string EventKey { get; set; }
        /// <summary>
        /// 二維碼的ticket,可用來換取二維碼圖片
        /// </summary>
        public string Ticket { get; set; }
}



Reporting geographical location events
When the official account turns on the geographical location reporting function, every time you enter the official account During the conversation, after the user agrees to report the geographical location, the geographical location will be reported when entering, or the geographical location will be reported every 5 seconds after entering the conversation. The official account can modify the settings in the background of the official platform. When reporting the geographical location, WeChat will push the reported geographical location event to the URL filled in by the developer.

xml data package is as follows:

The code is as follows:

<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>123456789</CreateTime>
<MsgType><![CDATA[event]]></MsgType>
<Event><![CDATA[LOCATION]]></Event>
<Latitude>23.137466</Latitude>
<Longitude>113.352425</Longitude>
<Precision>119.385040</Precision>
</xml>



The corresponding entities are as follows:

code show as below:

/// <summary>
/// 上報地理位置實體
/// </summary>
public class LocationEventMessage : EventMessage
{
 
        /// <summary>
        /// 地理位置緯度
        /// </summary>
        public string Latitude { get; set; }
        /// <summary>
        /// 地理位置經(jīng)度
        /// </summary>
        public string Longitude { get; set; }
       /// <summary>
        /// 地理位置精度
       /// </summary>
        public string Precision { get; set; }
}



自定義菜單事件常用的事件有:click,view,scancode_puth,scancode_waitmsg,location_select。另外還有三種發(fā)圖的事件,由于并不常用,筆者也沒想到使用場景,再次就不一一講述了,有興趣的可以自己研究下,或者和我進行交流。

click事件推送的xml數(shù)據(jù)包:

代碼如下:

<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[FromUser]]></FromUserName>
<CreateTime>123456789</CreateTime>
<MsgType><![CDATA[event]]></MsgType>
<Event><![CDATA[CLICK]]></Event>
<EventKey><![CDATA[EVENTKEY]]></EventKey>
</xml>



view事件推送的xml數(shù)據(jù)包和click的格式是一樣的,所以定義一個類就可以了,如下:

代碼如下:

/// <summary>
/// 普通菜單事件,包括click和view
/// </summary>
public class NormalMenuEventMessage : EventMessage
{
 
        /// <summary>
        /// 事件KEY值,設置的跳轉(zhuǎn)URL
        /// </summary>
        public string EventKey { get; set; }
}


scancode事件的xml數(shù)據(jù)包如下:

代碼如下:

<xml><ToUserName><![CDATA[ToUserName]]></ToUserName>
<FromUserName><![CDATA[FromUserName]]></FromUserName>
<CreateTime>1419265698</CreateTime>
<MsgType><![CDATA[event]]></MsgType>
<Event><![CDATA[scancode_push]]></Event>
<EventKey><![CDATA[EventKey]]></EventKey>
<ScanCodeInfo><ScanType><![CDATA[qrcode]]></ScanType>
<ScanResult><![CDATA[http://weixin.qq.com/r/JEy5oRLE0U_urVbC9xk2]]></ScanResult>
</ScanCodeInfo>
</xml>

對應的實體如下:

代碼如下:

/// <summary>
/// 菜單掃描事件
/// </summary>
public class ScanMenuEventMessage : EventMessage
{
 
        /// <summary>
        /// 事件KEY值
        /// </summary>
        public string EventKey { get; set; }
        /// <summary>
        /// 掃碼類型。qrcode是二維碼,其他的是條碼
        /// </summary>
        public string ScanType { get; set; }
        /// <summary>
        /// 掃描結(jié)果
        /// </summary>
        public string ScanResult { get; set; }
}



至此,當前常用的事件類型消息都已定義完畢,結(jié)合上一篇所講的,將xml數(shù)據(jù)包轉(zhuǎn)換成對象的完整代碼如下:

代碼如下:

public class MessageFactory
{
        private static List<BaseMsg> _queue; 
        public static BaseMessage CreateMessage(string xml)
        {
            if (_queue == null)
            {
                _queue = new List<BaseMsg>();
            }
            else if(_queue.Count>=50)
            {
                _queue = _queue.Where(q => { return q.CreateTime.AddSeconds(20) > DateTime.Now; }).ToList();//保留20秒內(nèi)未響應的消息
            }
            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 (type!=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<TextMessage>(xml);
                case MsgType.IMAGE: return Utils.ConvertObj<ImgMessage>(xml);
                case MsgType.VIDEO: return Utils.ConvertObj<VideoMessage>(xml);
                case MsgType.VOICE: return Utils.ConvertObj<VoiceMessage>(xml);
                case MsgType.LINK:
                    return Utils.ConvertObj<LinkMessage>(xml);
                case MsgType.LOCATION:
                    return Utils.ConvertObj<LocationMessage>(xml);
                case MsgType.EVENT://事件類型
                {
                    var eventtype = (Event)Enum.Parse(typeof(Event), xdoc.Element("Event").Value.ToUpper());
                    switch (eventtype)
                    {
                        case Event.CLICK:
                            return Utils.ConvertObj<NormalMenuEventMessage>(xml);
                        case Event.VIEW: return Utils.ConvertObj<NormalMenuEventMessage>(xml);
                        case Event.LOCATION: return Utils.ConvertObj<LocationEventMessage>(xml);
                        case Event.LOCATION_SELECT: return Utils.ConvertObj<LocationMenuEventMessage>(xml);
                        case Event.SCAN: return Utils.ConvertObj<ScanEventMessage>(xml);
                        case Event.SUBSCRIBE: return Utils.ConvertObj<SubEventMessage>(xml);
                        case Event.UNSUBSCRIBE: return Utils.ConvertObj<SubEventMessage>(xml);
                        case Event.SCANCODE_WAITMSG: return Utils.ConvertObj<ScanMenuEventMessage>(xml);
                        default:
                            return Utils.ConvertObj<EventMessage>(xml);
                    }
                } break;
                default:
                    return Utils.ConvertObj<BaseMessage>(xml);
            }
        }
}


The above is the detailed content of Introduction to the method of using C# to develop WeChat public accounts to receive event push and message deduplication. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1488
72
The difference between multithreading and asynchronous c# The difference between multithreading and asynchronous c# Apr 03, 2025 pm 02:57 PM

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

C# vs. C  : History, Evolution, and Future Prospects C# vs. C : History, Evolution, and Future Prospects Apr 19, 2025 am 12:07 AM

The history and evolution of C# and C are unique, and the future prospects are also different. 1.C was invented by BjarneStroustrup in 1983 to introduce object-oriented programming into the C language. Its evolution process includes multiple standardizations, such as C 11 introducing auto keywords and lambda expressions, C 20 introducing concepts and coroutines, and will focus on performance and system-level programming in the future. 2.C# was released by Microsoft in 2000. Combining the advantages of C and Java, its evolution focuses on simplicity and productivity. For example, C#2.0 introduced generics and C#5.0 introduced asynchronous programming, which will focus on developers' productivity and cloud computing in the future.

How to convert xml to json How to convert xml to json Apr 03, 2025 am 09:09 AM

Methods to convert XML to JSON include: writing scripts or programs in programming languages ??(such as Python, Java, C#) to convert; pasting or uploading XML data using online tools (such as XML to JSON, Gojko's XML converter, XML online tools) and selecting JSON format output; performing conversion tasks using XML to JSON converters (such as Oxygen XML Editor, Stylus Studio, Altova XMLSpy); converting XML to JSON using XSLT stylesheets; using data integration tools (such as Informatic

What is c# multithreading programming? C# multithreading programming uses c# multithreading programming What is c# multithreading programming? C# multithreading programming uses c# multithreading programming Apr 03, 2025 pm 02:45 PM

C# multi-threaded programming is a technology that allows programs to perform multiple tasks simultaneously. It can improve program efficiency by improving performance, improving responsiveness and implementing parallel processing. While the Thread class provides a way to create threads directly, advanced tools such as Task and async/await can provide safer asynchronous operations and a cleaner code structure. Common challenges in multithreaded programming include deadlocks, race conditions, and resource leakage, which require careful design of threading models and the use of appropriate synchronization mechanisms to avoid these problems.

C# .NET: Building Applications with the .NET Ecosystem C# .NET: Building Applications with the .NET Ecosystem Apr 27, 2025 am 12:12 AM

How to build applications using .NET? Building applications using .NET can be achieved through the following steps: 1) Understand the basics of .NET, including C# language and cross-platform development support; 2) Learn core concepts such as components and working principles of the .NET ecosystem; 3) Master basic and advanced usage, from simple console applications to complex WebAPIs and database operations; 4) Be familiar with common errors and debugging techniques, such as configuration and database connection issues; 5) Application performance optimization and best practices, such as asynchronous programming and caching.

From Web to Desktop: The Versatility of C# .NET From Web to Desktop: The Versatility of C# .NET Apr 15, 2025 am 12:07 AM

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

.NET Framework vs. C#: Decoding the Terminology .NET Framework vs. C#: Decoding the Terminology Apr 21, 2025 am 12:05 AM

.NETFramework is a software framework, and C# is a programming language. 1..NETFramework provides libraries and services, supporting desktop, web and mobile application development. 2.C# is designed for .NETFramework and supports modern programming functions. 3..NETFramework manages code execution through CLR, and the C# code is compiled into IL and runs by CLR. 4. Use .NETFramework to quickly develop applications, and C# provides advanced functions such as LINQ. 5. Common errors include type conversion and asynchronous programming deadlocks. VisualStudio tools are required for debugging.

What are the benefits of multithreading in c#? What are the benefits of multithreading in c#? Apr 03, 2025 pm 02:51 PM

The advantage of multithreading is that it can improve performance and resource utilization, especially for processing large amounts of data or performing time-consuming operations. It allows multiple tasks to be performed simultaneously, improving efficiency. However, too many threads can lead to performance degradation, so you need to carefully select the number of threads based on the number of CPU cores and task characteristics. In addition, multi-threaded programming involves challenges such as deadlock and race conditions, which need to be solved using synchronization mechanisms, and requires solid knowledge of concurrent programming, weighing the pros and cons and using them with caution.

See all articles