SFSEventType ENUM and Example Programs (com.smartfoxserver.v2.core.SFSEventType)
SFSEventType
An Enum which consists the events which are raised and handled by smartfoxserver some of those are.
Sno | EventType | Description |
1 | SERVER_READY | The event is fired by SmartFox when the server engine has completed the boot phase. |
2 | USER_LOGIN | The event is fired when a User sends a login request. |
3 | USER_JOIN_ZONE | The event is fired after a successful User login. |
4 | USER_JOIN_ROOM | The event is fired after a User has joined a Room. |
5 | ROOM_VARIABLES_UPDATE | This event is fired when a one or more Room Variables are set. |
6 | USER_LEAVE_ROOM | The event is fired after a User has left a Room. |
7 | USER_DISCONNECT | The event is fired after a User disconnects or is disconnected. |
8 | PUBLIC_MESSAGE | This event is fired when a public message is sent by a client. |
9 | PRIVATE_MESSAGE | This event is fired when a private message is sent by a client. |
10 | USER_RECONNECTION_SUCCESS | The event is fired when the SRS (smart reconnection system) option is turned on and a User was successfully reconnected. |
Note:-
- All these Events are related to server related, means these event are handled at server side only where as client's will fire these type of requests
- In order to handle these events in our code/program we need to register corresponding handler class for the events. (means we need to associate a class when that type of event fired/occurred).
- Through method addEventHandler() of SFSExtension we can
register the events like
addEventHandler(SFSEventType.USER_LOGIN, MyLoginEventHandler.class).
addEventHandler(SFSEventType.USER_JOIN_ZONE, MyZoneJoinEventHandler.class). - In the above addEventHandler() signatures indicates whenever USER_LOGIN request arrived from client then "MyLoginEventHandler" will be fired similarly the other request also.
- In order to handle these events the java class must be a subclass of "BaseServerEventHandler"
- In the BaseServerEventHandler a method named public void handleServerEvent(ISFSEvent event) will be fired when event fired.
- Most of the Events will consist the parameters, these parameters will attain the information of client request.
- If the USER_LOGIN event fired what we need is "username","password" etc.. these data are came as parameter, those can be fetched using handleServerEvent(ISFSEvent event).
Sample Code Snippets:-
Registering Events With HandlerClasses
For the sake of less code lines I am using Singleclass (MyEventHandler) to handle all events.
- addEventHandler(SFSEventType.USER_LOGIN, MyEventtHandler.class);
- addEventHandler(SFSEventType.USER_LOGIN, MyEventHandler.class);
- addEventHandler(SFSEventType.USER_JOIN_ZONE, MyEventHandler.class);
MyEventHandler.java snippet
public class MyEventHandler extends BaseServerEventHandler { public void handleServerEvent(ISFSEvent event) throws SFSException { SFSEventType eventtype = event.getType(); System.out.println("Event Fired "+eventtype+"\t time"+new Date()); } }
MyEventHandler.java SNIPPET WITH PARAMETERS CONCEPT
public class MyEventHandler extends BaseServerEventHandler { public void handleServerEvent(ISFSEvent event) throws SFSException { SFSEventType eventtype = event.getType(); System.out.println("Event Fired "+eventtype+"\t time"+new Date()); switch(eventtype){ case SFSEventType.USER_LOGIN: User user = (User)event.getParameter(SFSEventParam.USER); String loginname = (String) event.getParameter(SFSEventParam.LOGIN_NAME); String password = (String) event.getParameter(SFSEventParam.LOGIN_PASSWORD); System.out.println(eventype +" Request Arrived From User "+user.getName()+"\t With LOGIN_NAME "+loginname+"\t password "+password); break; case SFSEventType.USER_DISCONNECT: user = (User)event.getParameter(SFSEventParam.USER); System.out.println(eventype +" Request Arrived From User "+user.getName()); break; case SFSEventType.USER_JOIN_ZONE: user = (User)event.getParameter(SFSEventParam.USER); System.out.println(eventype +" Request Arrived From User "+user.getName()); break; } } }