The HTTP session and the CometD sessions have different life cycles: for example, if there is a temporary connection failure, the CometD session will fail and the server will ask the client to re-acknowledge, thereby creating another CometD session (representing one user, but with a different CometD clientId
). In this case, the HttpSession
will remain the same.
With this in mind, you need to maintain, at the application level, a mapping between the username, the HttpSession
correspondent, and the ServerSession
correspondent. Let me call this mapping HttpCometDMapper
. Each time a new user is registered, you register his name (or other unique user identifier), HttpSession
and the current ServerSession
. You'll probably need a two-step process when you first bind the username and HttpSession
, and then the same username with ServerSession
.
If you are doing a CometD handshake again, you are updating mapper with the new ServerSession
.
You can bind these two sessions by registering the HttpSessionListener
with an HttpSession
so that when you destroy it, you retrieve the current CometD ServerSession
from the display device and call ServerSession.disconnect()
on it.
Povterter is a little more complicated, because CometD has no concept of inactivity timeout, for example HttpSession
. It should be implemented in an application with your own logic.
One part of this is registering RemoveListener
on a ServerSession
, for example:
serverSession.addListener(new ServerSession.RemoveListener() { public void removed(ServerSession session, boolean timeout); { if (!timeout) {
This listener watches for an explicit disconnect from the client (and server - beware of reconnecting).
Itβs a little harder to implement the same mechanism for implicit outages. In this case, the timeout
parameter will be true, but it may occur due to a temporary network failure (as opposed to a client failure forever), and the same user may already overwrite the new ServerSession
.
I think that in this case the application timeout can solve the problem: when you see that ServerSession
deleted due to the timeout, you mark this user and start the application timeout. If the same user reboots, cancel the application timeout; otherwise the user has really left, the application timed out, and you also have an invalid HttpSession
.
What is higher - only ideas and suggestions; the actual implementation is largely dependent on the details of the application (and therefore CometD is not provided out of the box).
The key points are mapper, HttpSessionListener
and RemoveListener
, and knowing the life cycles of these components. Once you do this, you can write the right code that does the right thing for your application.
Finally, note that CometD has a transport-agnostic way to interact with HttpSession
using a BayeuxContext
instance, which you can get from BayeuxServer.getContext()
. I also suggest you also see if this can simplify the situation, especially for retrieving tokens stored in HttpSession
.