![Avatar](https://opcfoundation.org/wp-content/uploads/sp-resources/forum-avatars/defaults/userdefault.png)
10/19/2020
![sp_UserOfflineSmall](https://opcfoundation.org/wp-content/sp-resources/forum-themes/reboot/images/darkpack/sp_UserOfflineSmall.png)
I am working on an application where the server must know if a client is connected or not.
I have this server code:
SessionManager sm = .....
sm.SessionActivated += EventStatus;
sm.SessionClosing += EventStatus; // OK for graceful close, but fails for non graceful close
sm.SessionCreated += EventStatus;
The EventStatus fires for all events, including close when client closes gracefully. However, the EventStatus is not fired when the client does a non graceful shutdown (For example: unplugging the ethernet cable). In addition, sm.GetSessions() keeps returning the client. I have only had the program running for one hour after the client closed and I still get the session from sm.GetSessions(). I need to detect this condition within 5 minutes.
The server does not have a construction similar to what can be done on the client side i.e. the following is not possible on the server side:
session.KeepAlive += new KeepAliveEventHandler(Session_KeepAlive);
How do I solve this problem on the server side?
![Avatar](https://opcfoundation.org/wp-content/uploads/sp-resources/forum-avatars/defaults/admindefault.png)
05/30/2017
![sp_UserOfflineSmall](https://opcfoundation.org/wp-content/sp-resources/forum-themes/reboot/images/darkpack/sp_UserOfflineSmall.png)
The session does not close until the session times out.
You can control the max session timeout with a config setting but 5 minutes will likely be too fast.
This is by design because the session is not supposed to be closed until the server is sure that the client is gone for good. The timeout is the only mechanism available when a client does not shut down gracefully.
![Avatar](https://opcfoundation.org/wp-content/uploads/sp-resources/forum-avatars/defaults/userdefault.png)
10/19/2020
![sp_UserOfflineSmall](https://opcfoundation.org/wp-content/sp-resources/forum-themes/reboot/images/darkpack/sp_UserOfflineSmall.png)
I attempted the following solution, however neither session.Close() or session.Dispose() works. The code keeps going into the 'if" section for each iteration attempting to close the same session object.
for(;;) { DateTime minTime = DateTime.UtcNow - TimeSpan.FromMinutes(5); IList<Session> sessions = server.CurrentInstance.SessionManager.GetSessions(); for (int ii = 0; ii < sessions.Count; ii++) { Session session = sessions[ii]; if (session.SessionDiagnostics.ClientLastContactTime < minTime) { //session.Close(); session.Dispose(); } } Thread.Sleep(TimeSpan.FromMinutes(1)); }
![Avatar](https://opcfoundation.org/wp-content/uploads/sp-resources/forum-avatars/defaults/admindefault.png)
05/30/2017
![sp_UserOfflineSmall](https://opcfoundation.org/wp-content/sp-resources/forum-themes/reboot/images/darkpack/sp_UserOfflineSmall.png)
It is a mistake to assume there is a consistent physical channel that can be monitored. When using HTTPS the sockets come and go.
If you want this type of feature you have to hook the TCP transport stack rather than look for an API at the Server application level.
There may be something on the ServerBase object you can use.
1 Guest(s)