sending userList to extension response

Post here your questions about Actionscript and Java server side extensions development.

Moderators: Lapo, Bax

mixart
Posts: 95
Joined: 08 Aug 2007, 20:32

sending userList to extension response

Postby mixart » 11 May 2008, 07:13

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?
User avatar
BigFIsh
Posts: 1698
Joined: 25 Feb 2008, 19:26
Location: New Zealand

Postby BigFIsh » 11 May 2008, 20:04

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])
mixart
Posts: 95
Joined: 08 Aug 2007, 20:32

Postby mixart » 19 May 2008, 18:04

Thanks BigFIsh I'll give it a try - makes sense this would work.
ramindeja
Posts: 74
Joined: 31 Mar 2008, 17:54

Postby ramindeja » 22 May 2008, 18:59

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]
User avatar
Lapo
Site Admin
Posts: 23026
Joined: 21 Mar 2005, 09:50
Location: Italy

Postby Lapo » 26 May 2008, 08:36

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.
Lapo
--
gotoAndPlay()
...addicted to flash games
ramindeja
Posts: 74
Joined: 31 Mar 2008, 17:54

Postby ramindeja » 26 May 2008, 12:29

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.
User avatar
3eka
Posts: 9
Joined: 09 Jun 2008, 12:50
Contact:

Postby 3eka » 22 Oct 2008, 11:13

ramindeja, thanx a lot. This helped me too (with iterator).

Return to “Server Side Extension Development”

Who is online

Users browsing this forum: No registered users and 21 guests