I am answering from the CometD point of view.
The best way to restart a CometD session after the old one expired is to do nothing.
CometD has an automatic reconnect feature so if the network connection drops and the session expires, CometD will automatically retry until it succeeds to connect again with the server, with a backoff interval between attempts.
In order to guarantee that your subscriptions are restored on re-handshake, you just need to follow the guidelines, in particular perform your subscriptions from a /meta/handshake
successful listener.
If you want to test your JavaScript code behaviour in case of a disconnection, you just need to create the disconnection, and not calling cometd.init()
twice.
You can kill the server, unplug the network cable, undeploy the webapp, etc.
If you explicitly disconnect the client, then you have to re-handshake manually:
cometd.init(...);
...
// Disconnect
cometd.disconnect();
// Wait a while, then re-handshake
cometd.handshake();
Note that you must "wait a while" because cometd.disconnect()
is asynchronous.
Alternatively, you can do:
cometd.init(...);
...
// Disconnect and re-handshake when fully disconnected
cometd.addListener('/meta/disconnect', function(message)
{
cometd.handshake();
});
cometd.disconnect();
// Another solution, using setTimeout()
cometd.disconnect();
window.setTimeout(function()
{
cometd.handshake();
}, 5000);