Problem with the event SFSEvent.USER_ENTER_ROOM

Post here your questions about the Java client / Android API for SFS2X

Moderators: Lapo, Bax

hoanghuybao
Posts: 35
Joined: 22 Aug 2015, 16:20

Problem with the event SFSEvent.USER_ENTER_ROOM

Postby hoanghuybao » 27 Dec 2015, 08:26

Hi admin,
I read about the event USER_ENTER_ROOM at http://docs2x.smartfoxserver.com/api-docs/jsdoc/symbols/SFS2X.SFSEvent.html#.USER_ENTER_ROOM
I want to that when an user enter room, I can get more information such as level, money...from the user. I think so we can use method user.setVariable(). But where do we can set the variables? How to implement that?
I try to implementing code in server. But when the event USER_ENTER_ROOM is fired, I cannot get the variables. :(

Code: Select all

addEventHandler(SFSEventType.USER_JOIN_ROOM, JoinRoomHandler.class);


Code: Select all

public class JoinRoomHandler extends BaseServerEventHandler {

    @Override
    public void handleServerEvent(ISFSEvent event) throws SFSException {
        User sender = (User) event.getParameter(SFSEventParam.USER);
        List<UserVariable> userVars = new ArrayList<UserVariable>();
        userVars.add(new SFSUserVariable("avatarType", "SwedishCook"));
        userVars.add(new SFSUserVariable("country", "Sweden"));
        sender.setVariables(userVars);
    }

}


Could you please give me some solution?

Cheer,
User avatar
Lapo
Site Admin
Posts: 22999
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Problem with the event SFSEvent.USER_ENTER_ROOM

Postby Lapo » 27 Dec 2015, 20:35

Hi,
you have linked a page from the Javascript documentation, but you have posted under Java/Android. What is your development platform?

The USER_ENTER_ROOM event on client side fires every time a user joins the same Room where the player is now joined. Once you receive the event you're passed the User object and you can definitely access all of the UserVariables, if you have set them.

In your server side code you're calling sender.setVariable(...) which works only locally on the server side without sending any updates to other clients.

We always recommend using the main SFSApi object instead which you can access in your Extension code by calling getApi()
Like this:

Code: Select all

public class JoinRoomHandler extends BaseServerEventHandler {

    @Override
    public void handleServerEvent(ISFSEvent event) throws SFSException {
        User sender = (User) event.getParameter(SFSEventParam.USER);
        List<UserVariable> userVars = new ArrayList<UserVariable>();
        userVars.add(new SFSUserVariable("avatarType", "SwedishCook"));
        userVars.add(new SFSUserVariable("country", "Sweden"));
       
       getApi().setUserVariables(sender, userVars);
    }


I would recommend reading the first part of this article:
http://smartfoxserver.com/blog/?p=329

cheers
Lapo
--
gotoAndPlay()
...addicted to flash games

Return to “SFS2X Java / Android API”

Who is online

Users browsing this forum: No registered users and 9 guests