Tcp-based chat program breeze IM (c # Open Source ),
In breeze IM, when a client is offline, other clients can perceive that the user has been offline and turn the Avatar icon into gray.
The disconnection of the sensor connection is a built-in function of the networkcomms framework. When the server detects a connection disconnection through heartbeat detection, the server deletes the connection from the connection list maintained in networkcomms and triggers the corresponding delegate.
To handle a disconnection, you only need to register the NetworkComms. AppendGlobalConnectionCloseHandler method.
The server code is as follows:
// If a client is offline, this method is triggered NetworkComms. AppendGlobalConnectionCloseHandler (HandleConnectionClosed );
Offline processing of a user on the client
// Process a client offline private void HandleConnectionClosed (Connection connection Connection) {try {var tempUserID = ""; lock (syncLocker) {// find the user ID foreach (var kv in userManager) corresponding to the network connection in the user dictionary {// if the network connection to be closed is the same as the network connection of a user, find the user if (kv. value = connection. connectionInfo. networkIdentifier) {tempUserID = kv. key; break ;}} if (tempUserID! = "") {// If the user ID is not empty, delete this option from the user dictionary if (userManager. containsKey (tempUserID) {// when the connection is closed, delete the user userManager from the User Manager. remove (tempUserID) ;}}// send a notification to other clients to notify them that a user is offline if (tempUserID! = "") {Userstatenoworkflow (tempUserID, false) ;}// a message should be sent to all other online users} catch (Exception ex) {LogTools. logException (ex, "NetworkComms_ConnectionClosed ");}}
Www.networkcomms.cn
Www.cnblogs.com/networkcomms