Page 1 of 1

sending userList to extension response

Posted: 11 May 2008, 07:13
by mixart
I'm having a brain freeze :)

Can someone help with this...

I have an serverside extension calling a cmd: getZoneUsers
I want to return a list of all users in a zone to the extension response...

Code: Select all

if(cmd == "getZoneUsers"){
trace(_server.getCurrentZone().getUserList());
_server.sendResponse({cmd: "getZoneUsers", zoneUserList:_server.getCurrentZone().getUserList()}, -1, null, [user]); 
}
The trace above in the admin tool looks like this:
[it.gotoandplay.smartfoxserver.data.User@1a1af15, it.gotoandplay.smartfoxserver.data.User@1edca15, it.gotoandplay.smartfoxserver.data.User@11d56b5, it.gotoandplay.smartfoxserver.data.User@6e5cfa, it.gotoandplay.smartfoxserver.data.User@19a5185]

which looks like an array of user objects?

However when I send this back to the extension response I can't access the object data. Here's me trying to loop through the data returned...

Code: Select all

var zoneUsers = resObj;
for (var i in zoneUsers){
	trace(i+":"+resObj[i]);
}
How do I return this data to Flash, then access the usernames?

Posted: 11 May 2008, 20:04
by BigFIsh
That didn't work for me either.

Try this in the meanwhile:

Code: Select all

zone = _server.getCurrentZone()
rooms = zone.getRooms()
myUsersArray = new Array()
for (var r in rooms) {
     var users = rooms[r].getAllUsers();
     for (var u in users) {
          trace("Name: " + users[u].name + " from " + rooms[r].name)
          myUsersArray.push(users[u])
     }
}
_server.sendResponse({cmd: "getZoneUsers", zoneUserList: myUsersArray, -1, null, [user])

Posted: 19 May 2008, 18:04
by mixart
Thanks BigFIsh I'll give it a try - makes sense this would work.

Posted: 22 May 2008, 18:59
by ramindeja
Hey mixart,

I don't know if you care anymore or not, my solution is similar to BigFish in that I go through the user list and add them inside an array. This step is necessary because the return value of Zone.getUserList() is of type java.util.List. If send the result as is, the list cannot be serialized.

so:

Code: Select all

function getZoneUsers()
{
   var userList = _server.getCurrentZone().getUserList();
   var iterator = userList.iterator();
   var users = [];

   while (iterator.hasNext()) {
      users.push(iterator.next());
   }

   trace(users);

   // this is my function, so you can simply send a response here
   
   var result = {};
   result.data = users;

   return result;

};

BTW, I have quite a bit of problem when sending responses to the client using _server.PROTOCOL_JSON!!!!!! Lapo still hasn't answered but I wonder if I shouldn't stick to the default XML protocol. I thought I was being more efficientby using JSON ... guess is not worth it!

Ciao.[/code]

Posted: 26 May 2008, 08:36
by Lapo
The code you have shown won't work in XML, JSON or anything else because we don't support transferring User objects directly on the client side.

The (Java) User class describes a connected user on the server side and contains a lot of fields that can't be serialized.

Additionally if you simply need the names of the users why trying to send the whole object? Simply create an array with the NAMES of those users.

Posted: 26 May 2008, 12:29
by ramindeja
I did the sample code out of curiosity and I got mislead when the trace of the returned objects on the client-side showed it.gotoandplay.smartfoxserver.data.User. I assumed that the objects returned by the server were already casted to the proper class.

But I agree that merely names/ids will be sufficient.

Posted: 22 Oct 2008, 11:13
by 3eka
ramindeja, thanx a lot. This helped me too (with iterator).