Class BaseServerEventHandler

java.lang.Object
com.smartfoxserver.extensions.BaseServerEventHandler
All Implemented Interfaces:
IServerEventHandler

public abstract class BaseServerEventHandler extends Object implements IServerEventHandler
Using SFSExtension as the base class for an Extension we can register separate handlers for different server side events. This is typically done in the Extension's init() method:

 class JoinZoneHandler extends BaseServerEventHandler
 {
 	void handleServerEvent(ISFSEvent evt) throws SFSException
 	{
 		trace("Inside Zone Join handler");
 		var user = (User) evt.getParameter(SFSEventParam.USER);
 		var zone = (Zone) evt.getParameter(SFSEventParam.ZONE);
 
 		// More app logic...
 	}
 }
 
 private JoinZoneHandler joinZoneHandler;
 
 @Override
 public void init()
 {
 	joinZoneHandler = new JoinZoneHandler();
 
 	// Using a class instance
 	addEventHandler(SFSEvent.USER_JOIN_ZONE, joinZoneHandler);	
 
 	// Using a method reference
 	addRequestHandler("Walk", this::onJoinZone);
 
 }
 
 private void onJoinZone(ISFSEvent evt)
 {
 	trace("Inside Zone Join handler");
 	var user = (User) evt.getParameter(SFSEventParam.USER);
 	var zone = (Zone) evt.getParameter(SFSEventParam.ZONE);
 
 	// More app logic...
 }
 
The example demonstrates two different ways to provide a server event handler:
  • A class extending BaseServerEventHandler
  • A local method reference
See Also:
  • Constructor Details

    • BaseServerEventHandler

      public BaseServerEventHandler()
  • Method Details

    • getParentExtension

      public SFSExtension getParentExtension()
      Obtain a reference to the parent extension.
      Specified by:
      getParentExtension in interface IServerEventHandler
      Returns:
      a reference to the parent extension
    • setParentExtension

      public void setParentExtension(SFSExtension ext)
      Specified by:
      setParentExtension in interface IServerEventHandler
    • getApi

      protected ISFSApi getApi()
      Get a reference to the main server side API
      Returns:
      the server side API object
    • getBuddyApi

      protected ISFSBuddyApi getBuddyApi()
      Get a reference to the server side Buddy List API
      Returns:
      the server side API object
    • getGameApi

      protected ISFSGameApi getGameApi()
      Get a reference to the server side Game API
      Returns:
      the server side API object
    • getMMOApi

      protected ISFSMMOApi getMMOApi()
      Get a reference to the server side MMO API
      Returns:
      the server side API object
    • send

      protected void send(String cmdName, ISFSObject params, User recipient)
      Send a response back to a User
      Parameters:
      cmdName - name of the command identifying the Extension request
      params - the parameters
      recipient - the recipient User
    • send

      protected void send(String cmdName, ISFSObject params, List<User> recipients)
      Broadcast a response back to multiple Users
      Parameters:
      cmdName - name of the command identifying the Extension request
      params - the parameters
      recipients - a list of recipients
    • send

      protected void send(String cmdName, ISFSObject params, User recipient, TransportType txType)
      Send a response back to a User
      Parameters:
      cmdName - name of the command identifying the Extension request
      params - the parameters
      recipient - the recipient User
      txType - the transport type (TCP/UDP etc...)
    • send

      protected void send(String cmdName, ISFSObject params, List<User> recipients, TransportType txType)
      Broadcast a response back to multiple Users
      Parameters:
      cmdName - name of the command identifying the Extension request
      params - the parameters
      recipients - a list of recipients
      txType - the transport type (TCP/UDP etc...)
    • trace

      protected void trace(Object... args)
      Traces a message to the console and log files using the Logger INFO level
      Parameters:
      args - any number of strings/object to trace
    • trace

      protected void trace(ExtensionLogLevel level, Object... args)
      Trace a message to the console and log files using the specified logging level
      Parameters:
      level - the logging level
      args - any number of object to trace
    • getLogger

      protected org.slf4j.Logger getLogger()
      Obtain a direct reference to the Extension's logger
      Returns:
      the logger object.