I'm writing a java class that needs to be very robust in recovering from potential network outages/pc crashes etc.
One of the tests is how it reconnects if smartfox server is closed suddenly.
I've added the SFSEvent.onConnection and SFSEvent.onConnectionLost event listeners and set it so a reconnect is attempted when either of the above get called:
Code: Select all
System.out.println("Great, successfull connected!"+event.getParams().getString("error"));
//next we try to login...
sfs.login(sfoxzone, sfoxuser, "");
System.out.println("Ouch, connection failed because of: "+event.getParams().getString("error"));
else if (event.getName().equals(SFSEvent.onConnectionLost))
System.out.println("connection lost - reconnect...");
the class will keep requesting the connect once smartfox server is closed (onConnectionLost event) and keep trying as the onConnection listener returns "fail"
However once the server is restarted it throws the exception below - it looks like the connect requests are coming in too fast before the server is fully initialised:
java.net.ConnectException: Connection refused: connect
at sun.nio.ch.Net.connect(Native Method)
I can sort of make this more reliable by putting in a wait command before each connect retry, it still throws the exception but will retry again and connect successfully but this is not a great solution,
I notice there's a sfs.onError(Exception e) method but not sure how to use this to capture and handle the exception
what's the best way of checking the server is ready to recieve a connect request?