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 void execute​(Runnable task, int delayInMillisSec)
    Executes a task asynchronously on the server after the specified delay - if delay is 0 task will be executed immediately
    static void log​(Exception ex)
    Logs an exception on the SocketServer
    static void log​(String mesg)
    Logs a message on the SocketServer
    static void logToConsole​(String mesg)
    Logs a message to the Server's console.
    static void registerGame​(Game game)
    Registers a new Game on the Game engine
    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.
    static void setDataStore​(DataStore dataStore)
    Sets the DataStore implementation which will be used to save the Game Engine data.
    static void setExpectedMaxActiveSessions​(int expectedMaxActiveSessions)
    This optimizes the system for the specified number of sessions
    static void setInitialUsersCacheSize​(int size)
    Sets the initial size of the cache where user objects are kept in memory.
    static void setLogFile​(String logFile)
    Sets the Server log file where all the log messages will be saved
    static void setSessionListenerFactory​(SessionListenerFactory sessionListenerFactory)
    Sets the sessionListenerFactory which will be used to create a SessionListener instance for each new Session.
    static void setSslCertificate​(File pfxFile, String pswd, String keyAlias)
    Sets the SSL certificate to use by the https service.
    static void setWebHandler​(WebHandler webHandler)
    Sets the server Web Handler - the server will route all web requests to the specified WebHandler.
    static void startServerAsHttp​(int port)
    Starts server as an http service (no SSL)
    static void startServerAsHttps​(int port, boolean enableHttpRedirect)
    starts server as an https service (with ssl support).
    static void stopServer()
    Stops the server

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

  • Method Details

    • startServerAsHttp

      public static final void startServerAsHttp​(int port) throws Exception
      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

      public static final void startServerAsHttps​(int port, boolean enableHttpRedirect) throws Exception
      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 443
      enableHttpRedirect - - 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

      public static void setWebHandler​(WebHandler webHandler)
      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 Exception
      Sets 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 stored
      pswd - - the keystore password
      keyAlias - - the key alias. pass null if there is no key alias
      Throws:
      Exception - -
    • stopServer

      public static final void stopServer()
      Stops the server
    • setSessionListenerFactory

      public static void setSessionListenerFactory​(SessionListenerFactory sessionListenerFactory)
      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

      public static void setDataStore​(DataStore dataStore)
      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

      public static void setLogFile​(String logFile)
      Sets the Server log file where all the log messages will be saved
      Parameters:
      logFile - - the log File
    • log

      public static void log​(String mesg)
      Logs a message on the SocketServer
      Parameters:
      mesg - - the message to be logged
    • log

      public static void log​(Exception ex)
      Logs an exception on the SocketServer
      Parameters:
      ex - - the exception to be logged
    • registerGame

      public static final void registerGame​(Game game) throws DuplicateGameNameHashException
      Registers a new Game on the Game engine
      Parameters:
      game - - the game to be registered
      Throws:
      DuplicateGameNameHashException - -
    • execute

      public static void execute​(Runnable task, int delayInMillisSec)
      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 executed
      delayInMillisSec - - the delay in milli seconds
    • logToConsole

      public static void logToConsole​(String mesg)
      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