ASP. NET SignalR with LayIM2.0 Easy implementation of the web chat Room (eight) using Redis to implement user online offline status message processing

Source: Internet
Author: User

Objective

The preview in the last article seems to be "the small details of the chat room, have you noticed it?" ”。 Today is also for the article to pave it. The previous version has a lot of problems, such as: whether the current login user is a legitimate issue, UserID parameters if you pass the background without verification. There is also a fatal problem, user AB is online, but if a does not open B's window or B does not open a window, then send a message, the other party is not receiving. Because they didn't get into the same group. This article describes some of the redis things. Because the project itself is for learning and practicing something. So, Redis is not my strong point, just research research, specific professional usage I am not very good. Still in the study ...

Implementation ideas

First, I used the hash table structure in Redis to store the user's online information. As shown: Key represents userid, value is the user's ConnectionID.

  

is not very simple, so what are the benefits of storing this data?

1. How many online users can we count

2. With the front-end interface, to achieve whether a friend online

3. Determine if a friend is online to decide whether or not to push a message like this friend (directly store offline messages if not online)

4. Solve the problems in the preface. To explain the problem in detail, such as a to B message, a Click Open the Window B, now A has been added to the group AB. However, B is not in group AB, so B does not receive messages from this group. If a opens the b window, determine if a is online, if a is online, then add a to the AB group, that is, one more step a= "group operation." This solves the problem that the different groups of AB cause the message not to be received. Detailed View:

  

Implementation Details

As long as we make a connection in the hub code, lose the connection, add the operation logic to the current user in the Reconnect method.

  /// <summary>        ///get current User information/// </summary>        Privateonlineuser Currentonlineuser {Get            {                return NewOnlineuser {ConnectionID=Currentconnectid, UserID=Currentuserid}; }        }        /// <summary>        ///Establish a connection/// </summary>        /// <returns></returns>         Public OverrideTask onconnected () {//Add the current user to the Redis online user cacheLayIMCache.Instance.OperateOnlineUser (Currentonlineuser); returnClients.Caller.receiveMessage ("Connection Successful"); }        /// <summary>        ///Lost connection/// </summary>        /// <param name= "stopcalled" ></param>        /// <returns></returns>         Public OverrideTask ondisconnected (BOOLstopcalled) {            //Remove the current user from the online user listLayIMCache.Instance.OperateOnlineUser (Currentonlineuser, Isdelete:true); returnClients.Caller.receiveMessage ("Lost connection"); }        /// <summary>        ///re-connect/// </summary>        /// <returns></returns>         Public OverrideTask onreconnected () {//Add the current user to the Redis online user cacheLayIMCache.Instance.OperateOnlineUser (Currentonlineuser); returnClients.Caller.receiveMessage ("re-connect"); }

I use it here. NET client is StackExchange.Redis.Extensions.Core , he's actually in Stackexchange.redis is based on a layer of encapsulation. More convenient to use, like directly with the Stackexchange.redis also no problem.

The detailed code is as follows:

              StaticNewtonsoftserializer serializer =NewNewtonsoftserializer (); Stackexchangerediscacheclient cacheclient=Newstackexchangerediscacheclient (serializer); #regionOnline user Processing Public voidOperateonlineuser (onlineuser user,BOOLIsdelete =false)        {            if(isdelete) {cacheclient.hashdelete (layimconst.layim_all_onlineusers, User.userid); }            Else{cacheclient.hashsetasync (layimconst.layim_all_onlineusers, User.userid, User.connectionid            ); }        }        #endregion

When we refresh the page, the Ondisconnected method is called first, and the Onconnected method is called. However, the HashSet method can overwrite its value if it is the same key.

This is where the interface is not changed, except for the addition of some Redis cache-based logic.

GitHub:https://github.com/fanpan26/LayIM_NetClient like it to a star, thank you.

AC Group: 145322742 (PS: Limited technology, Master light spray)

ASP. NET SignalR with LayIM2.0 Easy implementation of the web chat Room (eight) using Redis to implement user online offline status message processing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.