Package com.smartfoxserver.extensions
Class BaseServerEventHandler
java.lang.Object
com.smartfoxserver.extensions.BaseServerEventHandler
- All Implemented Interfaces:
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected ISFSApigetApi()Get a reference to the main server side APIprotected ISFSBuddyApiGet a reference to the server side Buddy List APIprotected ISFSGameApiGet a reference to the server side Game APIprotected org.slf4j.LoggerObtain a direct reference to the Extension's loggerprotected ISFSMMOApiGet a reference to the server side MMO APIObtain a reference to the parent extension.protected voidsend(String cmdName, ISFSObject params, User recipient) Send a response back to a Userprotected voidsend(String cmdName, ISFSObject params, User recipient, TransportType txType) Send a response back to a Userprotected voidsend(String cmdName, ISFSObject params, List<User> recipients) Broadcast a response back to multiple Usersprotected voidsend(String cmdName, ISFSObject params, List<User> recipients, TransportType txType) Broadcast a response back to multiple Usersvoidprotected voidtrace(ExtensionLogLevel level, Object... args) Trace a message to the console and log files using the specified logging levelprotected voidTraces a message to the console and log files using the Logger INFO levelMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.smartfoxserver.extensions.IServerEventHandler
handleServerEvent
-
Constructor Details
-
BaseServerEventHandler
public BaseServerEventHandler()
-
-
Method Details
-
getParentExtension
Obtain a reference to the parent extension.- Specified by:
getParentExtensionin interfaceIServerEventHandler- Returns:
- a reference to the parent extension
-
setParentExtension
- Specified by:
setParentExtensionin interfaceIServerEventHandler
-
getApi
Get a reference to the main server side API- Returns:
- the server side API object
-
getBuddyApi
Get a reference to the server side Buddy List API- Returns:
- the server side API object
-
getGameApi
Get a reference to the server side Game API- Returns:
- the server side API object
-
getMMOApi
Get a reference to the server side MMO API- Returns:
- the server side API object
-
send
Send a response back to a User- Parameters:
cmdName- name of the command identifying the Extension requestparams- the parametersrecipient- the recipient User
-
send
Broadcast a response back to multiple Users- Parameters:
cmdName- name of the command identifying the Extension requestparams- the parametersrecipients- a list of recipients
-
send
Send a response back to a User- Parameters:
cmdName- name of the command identifying the Extension requestparams- the parametersrecipient- the recipient UsertxType- the transport type (TCP/UDP etc...)
-
send
Broadcast a response back to multiple Users- Parameters:
cmdName- name of the command identifying the Extension requestparams- the parametersrecipients- a list of recipientstxType- the transport type (TCP/UDP etc...)
-
trace
Traces a message to the console and log files using the Logger INFO level- Parameters:
args- any number of strings/object to trace
-
trace
Trace a message to the console and log files using the specified logging level- Parameters:
level- the logging levelargs- 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.
-