Class SFSExtension
- All Implemented Interfaces:
ISFSEventListener,ISFSExtension
Developers can create each request/event handlers as separate classes and then register them in the SFSExtension which will take care of the invocation details.
SFSExtension also provides a servlet-like Filter system. Extension Filters are added to a filter chain and executed in the order of insertion. They can be used to log, filter, or handle specific requests or events before they get to the Extension itself. The advantage of pluggable Filters is that they don't get in the way of your Extension code, their execution order can be altered and they can be used to stop the execution flow, if necessary.
An example of this could be a custom ban filter where User credentials are checked against a black list before the request is passed to your Login Handler. Another example of usage would be logging or filtering public and private messages.
For a more detailed overview of Extensions check the relevant section in our main documentation site.
-
Field Summary
FieldsFields inherited from class com.smartfoxserver.extensions.BaseSFSExtension
log, sfsApi -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidaddEventHandler(SFSEventType eventType, IServerEventHandler handler) protected voidaddEventHandler(SFSEventType eventType, Class<?> theClass) Add a request handler for a specific event type.final voidaddFilter(String filterName, SFSExtensionFilter filter) Add a filter to the Extensionprotected voidaddRequestHandler(String requestId, IClientRequestHandler requestHandler) protected voidaddRequestHandler(String requestId, Class<?> theClass) Add a request handler for a specific request id.protected voidRemoves all event and request handlersvoidRemoves all filters from the Extensionvoiddestroy()This method is called once by the Server when the Extension must be shut down (e.g. before a server restart or before the Extension code is reloaded)voidhandleClientRequest(String requestId, User sender, ISFSObject params, TransportType txType) This method is called whenever a client sends a request to this ExtensionvoidhandleServerEvent(ISFSEvent event) Handle server eventprotected voidinitFloodFilter(com.smartfoxserver.extensions.ExtensionFloodFilterConfig cfg) Initializes the Extension anti-flood filter, which allows to configure a maximum request rate (req/s) for every custom Extension call.protected voidremoveEventHandler(SFSEventType eventType) Remove an event handlervoidremoveFilter(String filterName) Removes a filter from the Extensionprotected voidremoveRequestHandler(String requestId) Remove a request handlerMethods inherited from class com.smartfoxserver.extensions.BaseSFSExtension
addEventListener, getApi, getBuddyApi, getConfigProperties, getCurrentFolder, getExtensionFileName, getGameApi, getLevel, getLogger, getMMOApi, getName, getParentRoom, getParentZone, getPropertiesFileName, getReloadMode, getSFS, getType, handleInternalMessage, isActive, removeEventListener, removeEventsForListener, send, send, send, send, setActive, setExtensionFileName, setLevel, setName, setParentRoom, setParentZone, setPropertiesFileName, setReloadMode, setType, toString, trace, traceMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface com.smartfoxserver.extensions.ISFSExtension
init
-
Field Details
-
MULTIHANDLER_REQUEST_ID
- See Also:
-
-
Constructor Details
-
SFSExtension
public SFSExtension()
-
-
Method Details
-
destroy
public void destroy()Description copied from interface:ISFSExtensionThis method is called once by the Server when the Extension must be shut down (e.g. before a server restart or before the Extension code is reloaded)Here you can put all the necessary code to release any resources that was acquired/started in the init() method. (e.g. event listeners, threads, scheduled tasks, files etc...)
-
addRequestHandler
Add a request handler for a specific request id.Dot syntax
In order to properly organize request names in complex application we have established a convention similar to Java package naming that uses a "dot syntax". This will also allow you to group multiple requests under the same handler class.Examples:
addRequestHandler("myapp.chessGame", ChessGameHandler.class);If the ChessGameHandler is annotated with asMultiHandlerit will receive all commands starting with myapp.ChessGame or in other words myapp.ChessGame.*NOTE: multiple handlers for the same request is not supported.
- Parameters:
requestId- the requestId, optionally using dot-syntaxtheClass- the handler Class ( must implementIClientRequestHandler)
-
addRequestHandler
-
addEventHandler
Add a request handler for a specific event type. Each event can have only one handler.NOTE:Each event can have only one handler.
- Parameters:
eventType- the type of eventtheClass- the handler Class ( must implementIServerEventHandler)- See Also:
-
addEventHandler
-
removeRequestHandler
Remove a request handler- Parameters:
requestId- name of the request
-
removeEventHandler
Remove an event handler- Parameters:
eventType- the type of event
-
clearAllHandlers
protected void clearAllHandlers()Removes all event and request handlers -
handleClientRequest
public void handleClientRequest(String requestId, User sender, ISFSObject params, TransportType txType) This method is called whenever a client sends a request to this Extension- Parameters:
requestId- the request command namesender- the sender of the requestparams- the custom parameters of the request
-
handleServerEvent
Handle server event- Specified by:
handleServerEventin interfaceISFSEventListener- Overrides:
handleServerEventin classBaseSFSExtension- Parameters:
event- the Event- Throws:
Exception- See Also:
-
addFilter
Add a filter to the Extension- Parameters:
filterName- name of the filterfilter- the filter
-
removeFilter
Removes a filter from the Extension- Parameters:
filterName- name of the filter
-
clearFilters
public void clearFilters()Removes all filters from the Extension -
initFloodFilter
protected void initFloodFilter(com.smartfoxserver.extensions.ExtensionFloodFilterConfig cfg) Initializes the Extension anti-flood filter, which allows to configure a maximum request rate (req/s) for every custom Extension call.
For more information see the external documentation
- Parameters:
cfg- the filter's configuration object- See Also:
-