Package io.sockit.gameserver
Class Server
java.lang.Object
io.sockit.gameserver.Server
public class Server extends Object
This class is used to start and stop the Game Engine. It also has methods to set the dataStore where game data is saved and set the WebResourceGetter (for web requests such as .html and .css files. Below is example code for starting the game engine
Server.setDataStore(new LevelDbStore("./gameDb")); //sets the Database
Server.setLogger(new FileLogger("./serverLog.txt",true)); // Sets the Logger
Server.setWebHandler(new DefaultWebHandler("/webRoot")); //Sets the web provider
Server.startServerAsHttp(0); //0 will use default port of 80
-
Constructor Summary
Constructors Constructor Description Server() -
Method Summary
Modifier and Type Method Description static voidexecute(Runnable task, int delayInMillisSec)Executes a task asynchronously on the server after the specified delay - if delay is 0 task will be executed immediatelystatic voidlog(Exception ex)Logs an exception on the SocketServerstatic voidlog(String mesg)Logs a message on the SocketServerstatic voidlogToConsole(String mesg)Logs a message to the Server's console.static voidregisterGame(Game game)Registers a new Game on the Game enginestatic voidsetCombineLoginWithRegisterUser(boolean combineLoginWithRegister)This server setting allows user Login action to be combined with the user register action - this way there is a single point of entry for both first time users as well as returning users.static voidsetDataStore(DataStore dataStore)Sets the DataStore implementation which will be used to save the Game Engine data.static voidsetExpectedMaxActiveSessions(int expectedMaxActiveSessions)This optimizes the system for the specified number of sessionsstatic voidsetInitialUsersCacheSize(int size)Sets the initial size of the cache where user objects are kept in memory.static voidsetLogFile(String logFile)Sets the Server log file where all the log messages will be savedstatic voidsetSessionListenerFactory(SessionListenerFactory sessionListenerFactory)Sets the sessionListenerFactory which will be used to create a SessionListener instance for each new Session.static voidsetSslCertificate(File pfxFile, String pswd, String keyAlias)Sets the SSL certificate to use by the https service.static voidsetWebHandler(WebHandler webHandler)Sets the server Web Handler - the server will route all web requests to the specified WebHandler.static voidstartServerAsHttp(int port)Starts server as an http service (no SSL)static voidstartServerAsHttps(int port, boolean enableHttpRedirect)starts server as an https service (with ssl support).static voidstopServer()Stops the server
-
Constructor Details
-
Server
public Server()
-
-
Method Details
-
startServerAsHttp
Starts server as an http service (no SSL)- Parameters:
port- - the port on which server will accept new client connections. If 0 is passed server will use the default http port of 80- Throws:
Exception- - if unable to start server for reasons such as port is not available, etc
-
startServerAsHttps
starts server as an https service (with ssl support).- Parameters:
port- - the port on which server will accept new client connections. If 0 is passed server will use the default https port of 443enableHttpRedirect- - indicates whether to redirect client to https if client attempts to connect on http port.- Throws:
Exception- -
-
setCombineLoginWithRegisterUser
public static void setCombineLoginWithRegisterUser(boolean combineLoginWithRegister)This server setting allows user Login action to be combined with the user register action - this way there is a single point of entry for both first time users as well as returning users. This is useful for testing code during development- Parameters:
combineLoginWithRegister- - specifies whether to enable this setting or not
-
setWebHandler
Sets the server Web Handler - the server will route all web requests to the specified WebHandler.- Parameters:
webHandler- - An instance of a class which implements the WebHandler interface such as the DefaultWebHandler class
-
setSslCertificate
public static final void setSslCertificate(File pfxFile, String pswd, String keyAlias) throws ExceptionSets the SSL certificate to use by the https service.- Parameters:
pfxFile- - the keystore (PKCS#12 format) file where the private key and the public key certificate is storedpswd- - the keystore passwordkeyAlias- - the key alias. pass null if there is no key alias- Throws:
Exception- -
-
stopServer
public static final void stopServer()Stops the server -
setSessionListenerFactory
Sets the sessionListenerFactory which will be used to create a SessionListener instance for each new Session.- Parameters:
sessionListenerFactory- - the sessionListenerFactory which will be used to create a SessionListener instance for each new Session.
-
setInitialUsersCacheSize
public static void setInitialUsersCacheSize(int size)Sets the initial size of the cache where user objects are kept in memory. Default is 2000- Parameters:
size- - the size of the cache
-
setExpectedMaxActiveSessions
public static void setExpectedMaxActiveSessions(int expectedMaxActiveSessions)This optimizes the system for the specified number of sessions- Parameters:
expectedMaxActiveSessions- - the expected max number of active sessions.
-
setDataStore
Sets the DataStore implementation which will be used to save the Game Engine data.- Parameters:
dataStore- - An instance of a class which implements the DataStore Interface.
-
setLogFile
Sets the Server log file where all the log messages will be saved- Parameters:
logFile- - the log File
-
log
Logs a message on the SocketServer- Parameters:
mesg- - the message to be logged
-
log
Logs an exception on the SocketServer- Parameters:
ex- - the exception to be logged
-
registerGame
Registers a new Game on the Game engine- Parameters:
game- - the game to be registered- Throws:
DuplicateGameNameHashException- -
-
execute
Executes a task asynchronously on the server after the specified delay - if delay is 0 task will be executed immediately- Parameters:
task- - the Runnable task to be executeddelayInMillisSec- - the delay in milli seconds
-
logToConsole
Logs a message to the Server's console. Use this method instead of System.out.println() if you want to print messages to the console in the correct order.- Parameters:
mesg- - the message to log
-