MySQL Query in USER_JOIN_ROOM = missing UV in Client?

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

User avatar
Zelek
Posts: 36
Joined: 24 Jun 2010, 22:36

MySQL Query in USER_JOIN_ROOM = missing UV in Client?

Postby Zelek » 31 Aug 2011, 04:42

I've run into a strange issue where executing a MySQL query of any sort in the USER_JOIN_ROOM event on the server is causing the client not to receive UserVariables in the SFSEvent.ROOM_JOIN callback. I've put trace statements all through the server code, and everything executes normally and the correct values are sent to the client. What am I missing here?

Code: Select all

public void handleServerEvent(ISFSEvent event) throws SFSException {
        User theUser = (User) event.getParameter(SFSEventParam.USER);

        try {
            Connection conn = getParentExtension().getParentZone().getDBManager().getConnection();
            PreparedStatement sql = conn.prepareStatement("SELECT 1");
            ResultSet result = sql.executeQuery();
            conn.close();
        } catch (Exception e) {
            trace("Exception: " + e);
        }

        // The name UserVariable is a regular UserVariable
        UserVariable name = new SFSUserVariable("name", theUser.getProperty(AppZone.PLAYER_NAME));

        // Set the variables
        List<UserVariable> vars = Arrays.asList(name);
        getApi().setUserVariables(theUser, vars);
    }
}


On the client side, "name" will be null. If I remove the try/catch portion of the code, name is no longer null in the client callback. The SQL executes appropriately, though. Anyone have an idea?
User avatar
rjgtav
Posts: 2813
Joined: 19 Apr 2009, 11:31
Location: Lisbon, Portugal

Postby rjgtav » 31 Aug 2011, 08:16

hi. Are you using the latest sfs (sfs2x RC3)? If not, please upgrade and check if the problem persists.

Also, does the sql throw an exception? Because if it throws, i think it wont continue the code and wont set the variable.

Finally, does the variable appear on the adminTool (in the case where it.s null on the client)?
Skills: SFS Pro, SFS2X, AS2.0/AS3.0, Java, HTML5/CSS3/JS, C#
Portfolio: https://rjgtav.wordpress.com/
SFS Tutorials: http://sfs-tutor.blogspot.com/ - Discontinued. Some examples may be bugged.
User avatar
Zelek
Posts: 36
Joined: 24 Jun 2010, 22:36

Postby Zelek » 31 Aug 2011, 14:06

I am using SFS2x RC3. The SQL doesn't throw an exception - everything appears correct on the server side. I am able to see the variable on the user in the server monitor.

I am using Unity on the client side. I have a pretty standard ROOM_JOIN event.

Code: Select all

smartFox.AddEventListener(SFSEvent.ROOM_JOIN, OnJoinRoom);

...

private static void OnJoinRoom (BaseEvent evt) {
      D.log("OnJoinRoom Event");
      Room room = (Room)evt.Params["room"];
      D.log("room: " + room);
      D.log("name:" + smartFox.MySelf.GetVariable("name"));
      D.log("null? " + (smartFox.MySelf.GetVariable("name") == null));

...
}
User avatar
rjgtav
Posts: 2813
Joined: 19 Apr 2009, 11:31
Location: Lisbon, Portugal

Postby rjgtav » 31 Aug 2011, 14:22

Well, if you set the variables only when the USER_JOIN_ROOM event is fired, that means that the user has already joined the room, so in the client you are trying to get the variables before they are set.

This is the current flow you're doing:

1. User joins Zone
2. User joins a room - USER_JOIN_ROOM event fired on the server
3. (Client) trying to retrieve the variables, of course it returns null
4. Server sets the variables.

So you need to listen to the USER_VARIABLES_UPDATE event to get them correctly.

Or you can set the variables at the USER_JOIN_ZONE event, before the user joins the default room.
Skills: SFS Pro, SFS2X, AS2.0/AS3.0, Java, HTML5/CSS3/JS, C#
Portfolio: https://rjgtav.wordpress.com/
SFS Tutorials: http://sfs-tutor.blogspot.com/ - Discontinued. Some examples may be bugged.
mente
Posts: 73
Joined: 14 Apr 2011, 14:27

Postby mente » 31 Aug 2011, 14:42

I don't know if it helps, but try using ArrayList(1) and add(name) instead of Arrays.asList(name).
User avatar
Zelek
Posts: 36
Joined: 24 Jun 2010, 22:36

Postby Zelek » 01 Sep 2011, 02:32

rjgtav wrote:This is the current flow you're doing:

1. User joins Zone
2. User joins a room - USER_JOIN_ROOM event fired on the server
3. (Client) trying to retrieve the variables, of course it returns null
4. Server sets the variables.


Thanks for helping me to see this, rjgtav! I was expecting the client to try and retrieve the variables after the server finished handling USER_JOIN_ROOM, but I guess they were actually happening in parallel. Maybe trying to query the database was introducing just enough delay that the client was trying to access the variables before the server had finished handling the event.

In any event, I took your advice and moved things into USER_JOIN_ZONE, and things seem to be happy. Thanks again! :D

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 75 guests