NETWORKCOMMS V3 C # Communication framework handles client offline

Source: Internet
Author: User

Original: http://www.networkcomms.cn/Article/ViewArticle.aspx?ItemID=27833&PageID=1224

"1" Processing client offline

When using NETWORKCOMMS v3 for network communication, the client is offline and usually we do not need to do extra processing.

On the server side Networkcomms static classes are used to control the global

All connections to the server are saved in the

Allconnectionsbyidentifier Allconnectionsbyendpoint

Of these 2 dictionaries

When a new customer is added and the connection is created, the communication framework will automatically add the connection to the 2 dictionaries above

When there is a client offline, the server side through the heartbeat detection, will detect a client has been offline, after the communication framework calls Networkcomms.removeconnectionreference static method, the offline client-related connection from the above dictionary to remove.

When the client is offline, the communication framework has been handled for us, without our extra work.

"2" handles client offline in IM systems

In IM systems, we usually join the User Manager on the server side, and notify other users when a user is online. When a client falls out of line, it also notifies other users.

In this case, we need to do additional work on the server to notify other users that a client user is offline.

The method is simple:

Registering Client Offline events

Networkcomms.appendglobalconnectionclosehandler (handleconnectionclosed);
  //handling a client offline situation        Private voidhandleconnectionclosed (Connection Connection) {Try            {                                             varTempuserid =""; Lock(synclocker) {//find the user ID corresponding to the network connection in the user dictionary                    foreach(varKvinchUsermanager) {                        //If you are shutting down a network connection that is the same as a user's network connection, locate the user                        if(KV. Value = =connection.ConnectionInfo.NetworkIdentifier) {Tempuserid=KV.                            Key;  Break; }                    }                     if(Tempuserid! ="")                    {                        //if the found user ID is not empty, the item is removed from the user dictionary                        if(Usermanager.containskey (Tempuserid)) {//When the connection is closed, remove the user from the User ManagerUsermanager.remove (Tempuserid); }                    }                }                  //send notifications to other clients informing them of a user's downline                if(Tempuserid! ="") {userstatenotify (Tempuserid,false); }                //a message should be sent to all other users online            }            Catch(Exception ex) {logtools.logexception (ex,"networkcomms_connectionclosed"); }        }

"3" handles the client offline in the file transfer system

If the client is disconnected from the server while the file is being transferred, we need to tell the server to delete the cached files associated with this user (half of the files are transferred).

1, the registration of a client offline method

Networkcomms.appendglobalconnectionclosehandler (handleconnectionclosed);

2, the specific treatment method

  Private voidhandleconnectionclosed (Connection Connection) {Try            {                //notifies the file sender that the client has been offline                 This. Filemanager.removefilebynetworkid (Connection.ConnectionInfo.NetworkIdentifier.ToString ()); //notifies the file receiver that the client has been offline                 This. Recvmanager.removefilebynetworkid (Connection.ConnectionInfo.NetworkIdentifier.ToString ()); //Traverse to see if the user ID exists                varTempuserid =""; //Delete information about the-tray dictionary                Lock(synclocker) {//Remove any associated data from the cachesIncomingdatacache.remove (connection.                    ConnectionInfo); Incomingdatainfocache.remove (connection.                     ConnectionInfo); foreach(varKvinchUsermanager) {                        //If you are shutting down a network connection that is the same as a user's network connection, locate the user                        if(KV. Value = =connection.ConnectionInfo.NetworkIdentifier) {Tempuserid=KV.                            Key;  Break; }                    }                     if(Tempuserid! ="")                    {                         if(Usermanager.containskey (Tempuserid)) {//When the connection is closed, remove the user from the User ManagerUsermanager.remove (Tempuserid); }                    }                }             }            Catch(Exception ex) {logtools.logexception (ex,"networkcomms_connectionclosed"); }        }

NETWORKCOMMS V3 C # Communication framework handles client offline

Related Article

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.