Purpose: Give an idea of how to create an auto resarter on crash.
Difficulty: 5/10 depending on your knowledge.
Description: Well im not going to help you and spoon feed you, just going to give you brief exlaination can't believe people can't figure little stuff out and for god sake don't bother posting errors because methods are completely wrong except some xD
First you would need to declare the variables:
Code:
public boolean serverDownAttempt = false;
public boolean createNewConnection = false;
public int serverChecker = 6000; //After how long to check.
You need to make an if statment so when its time to check it creates a new connection.
Code:
if(serverChecker == 0) {
createNewConnection = true;
}
Also you want timer to go down.
Code:
if(serverChecker > 0) {
serverChecker -= 1;
}
Then you need the actual statment to go through the whole process of server being down.
Code:
if(createNewConnection && logIn) { //Creates connection and logs in
playerHandler.kickNewConnection; //Kicks the connection
serverChecker = 6000; // Resets timer back to normal state.
} else {
if(createNewConnection && !logIn) { //If it trys logging in and fails attempts again.
serverDownAttempt = true;
serverChecker = 1000; // Notice timer set quicker.
}
Code:
if(serverDownAttempt && (serverChecker == 0)) {
if(createNewConnection && !logIn) {
appendToRestart(); //Restart the server method
}
}
These methods would be declared:
Code:
public void appendToRestart() { // Restart method
closeListener(); //Shutsdown the server
runserver(); //Runs the server again
}
Code:
public void closeListener() {
try {
server.shutdownClientHandler = true;
if (server.clientListener != null) {
server.clientListener.close();
}
server.clientListener = null;
} catch (java.lang.Exception __ex) {
__ex.printStackTrace();
}
}
And last but all the run server method.
Code:
public void runserver() {
try {
String File = "runserver.bat"; // these 2 lines would be edited.
String Dir = "./" + File; // Directory
Runtime.getRuntime().exec(Dir);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
Can't figure out where some stuff goes? Then click the spoiler for help.
Note: All the methods and stuff are all within the server already so i dont see why someone couldn't do this and i took all this out my head so there was no use of convections what so ever so don't bother posting on it.
Hopefully you now have some understanding if there's something you can't understand please post it surely me or another member will answer your question/problem with an answer.