Page 1 of 1

Issue with Viewing Persistent Buddy Variables on Client Side

Posted: 26 Aug 2024, 13:26
by ciaoamigos
Hello everyone,

I'm encountering a problem with viewing a persistent Buddy variable on the client side in SmartFoxServer. I’ve written a server-side function to save the last login time of a user when they disconnect, using a Buddy variable named $lastlogin. Here is the server-side function I’m using:

Code: Select all

function onUserDisconnect(event) {
    trace("onUserDisconnect");
    var user = event.getParameter(SFSEventParam.USER);
    var timeuscita = new Date().toISOString();
    var lastlogin = new SFSBuddyVariable("$lastlogin", timeuscita, VariableType.STRING);
    getBuddyApi().setBuddyVariables(user, [lastlogin], true, true);
}

The issue is that, on the client side, when I retrieve the buddy list, I cannot see the $lastlogin variable whether the user is online or offline. Here is the code I use to get the buddy list:


Code: Select all

var buddies = sfs.buddyManager.getBuddyList();
console.log("SFS2X BUDDY_LIST_INIT buddies", buddies);

for (var i = 0; i < buddies.length; i++) {
    var buddy = buddies[i];
    var lastLogin = buddy.getVariable("$lastlogin");
   
    if (lastLogin != null) {
        console.log("Buddy:", buddy.name, "Last Login:", lastLogin.value);
    } else {
        console.log("Buddy:", buddy.name, "Last Login: not available");
    }
}

Even though the variable is correctly set on the server side (as confirmed by the trace), I cannot see it on the client side, regardless of whether the user is online or offline.

Re: Issue with Viewing Persistent Buddy Variables on Client Side

Posted: 27 Aug 2024, 09:42
by Lapo
Hi,
the problem I see is that you're trying to set a BuddyVariable when the User has disconnected. I wouldn't expect it to work, because that user no longer exists.

Also the name of the variable seems inappropriate: the "loginTime" should be the instant in which the User logged in, not when he logged out.

If you're trying to implement a "last seen" variable that tells you the last time a Buddy was seen online I think you should use a different approach, because you can't predict when the User will disconnect, and you can't set its variables when it's already gone.

I would suggest to use a Task that runs indefinitely every few minutes and updates the "last seen" variable, for all online users.

Cheers