Skip to content

Introduction

Ta Van Dung edited this page Jun 17, 2016 · 4 revisions

[EzyFox] (https://github.com/youngmonkeys/ezyfox) is a java framework base on SmartFox Server API. It tries to help developer make source code easier and quicker, it also guarantees application design is loose coupling.

Example:

1. Create extension class

  • With SmartFox:
public class GameApplication extends SFSExtension {

    @Override
    public void init() {
        addEventHandler(SFSEventType.USER_JOIN_ZONE, UserJoinZoneEventHandler.class);
        addEventHandler(SFSEventType.USER_LOGIN, UserLoginEventHandler.class);
        addEventHandler(SFSEventType.USER_JOIN_ROOM, UserJoinRoomEventHandler.class);

        // and more source code
    }

}
  • With EzyFox:
@AppContextConfiguration(clazz = AppConfig.class)
public class GameApplication extends ZoneExtension {
}

2. Handle user login event

  • With SmartFox:
public class UserLoginEventHandler extends BaseServerEventHandler {

    @Override
    public void handleServerEvent(ISFSEvent event) throws SFSException {
        String username = (String)event.getParameter(SFSEventParam.LOGIN_NAME);
        String password = (String)event.getParameter(SFSEventParam.LOGIN_PASSWORD);
        ISFSObject loginData = (ISFSObject)event.getParameter(SFSEventParam.LOGIN_IN_DATA);
        String gameVersion = loginData.getUtfString("gvs");

        // do something
    }
}
  • With EzyFox:
@Data
@ServerEventHandler(event = ServerEvent.USER_LOGIN)
public class UserLoginHandler {

    @RequestParam("1")
    private String version;
    
    public void handle(AppContext context, String username, String password) throws Exception {
        // do something
    }
    
}

3. Listen and response an client request

  • With SmartFox you must pass to 2 steps:

Step 1. Register request listener to the extension

public class GameApplication extends SFSExtension {

    @Override
    public void init() {
        // some config
        addRequestHandler("sum", SumRequestListener.class);
        // some config
    }
}

Step 2. Create the listener class

public class SumRequestListener extends BaseClientRequestHandler {

    @Override
    public void handleClientRequest(User user, ISFSObject param) {
        Integer a = param.getInt("a");
        Integer b = param.getInt("b");
        Integer sum = a + b;
        ISFSObject responseParams = new SFSObject();
        responseParams.putInt("result", sum);
        send("sum", responseParams, user);
    }

}
  • With EzyFox everything is automatic:
@Data
@ClientResponseHandler
@ClientRequestListener(command = "sum")
public class SumRequestListener {
    
    @RequestParam private int a;
    @RequestParam private int b;
    @ResponseParam private int total;
    
    public void execute(AppContext context, GameUser user) {
        total = a + b;
    }
    
}

Hello World

Clone this wiki locally