Package com.smartfoxserver.extensions
Class BaseClientRequestHandler
java.lang.Object
com.smartfoxserver.extensions.BaseClientRequestHandler
- All Implemented Interfaces:
IClientRequestHandler
- Direct Known Subclasses:
SignUpAssistantComponent
Using SFSExtension as the base class for an Extension we can register separate request handlers for every supported client request.
This is typically done in the Extension's init() method:
class RunHandler extends BaseClientRequestHandler
{
void handleClientRequest(User sender, ISFSObject params, TransportType txType)
{
trace("Inside Run handler");
}
}
private RunHandler runHandler;
@Override
public void init()
{
runHandler = new RunHandler();
// Using a class instance
addRequestHandler("Run", runHandler);
// Using a method reference
addRequestHandler("Walk", this::walkHandler);
// Using a lambda
addRequestHandler("Fly", (User sender, ISFSObject params, TransportType txType) -> {
trace("Inside Fly handler");
});
}
private void walkHandler(User sender, ISFSObject params, TransportType txType)
{
trace("Inside Walk handler");
}
The example demonstrates three different ways to provide a handler for a specific Extension command:
- A class extending BaseClientRequestHandler
- A local method reference
- A lambda
-
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.IClientRequestHandler
handleClientRequest
-
Constructor Details
-
BaseClientRequestHandler
public BaseClientRequestHandler()
-
-
Method Details
-
getParentExtension
Obtain a reference to the parent extension.- Specified by:
getParentExtensionin interfaceIClientRequestHandler- Returns:
- a reference to the parent extension
-
setParentExtension
- Specified by:
setParentExtensionin interfaceIClientRequestHandler
-
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" (or action id)params- the parametersrecipient- the recipient User
-
send
Broadcast a response back to multiple Users- Parameters:
cmdName- name of the "command" (or action id)params- the parametersrecipients- a list of recipients
-
send
Send a response back to a User- Parameters:
cmdName- name of the "command" (or action id)params- 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" (or action id)params- 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.
-