How to pass/handle List object from server to client?

Post here your questions about the Flash / Flex / Air API for SFS2X

Moderators: Lapo, Bax

Rashomon
Posts: 72
Joined: 11 Aug 2010, 19:48

How to pass/handle List object from server to client?

Postby Rashomon » 16 Jan 2012, 14:32

I'm trying to pass a List or (multi-dimensional array, if that would work better) from my server extension back to my client, but I cannot figure out which objects and putXYZ to use to pass it back to my client.

Here's what I have so far:

Code: Select all

List<String[]> wordList = new ArrayList<String[]>();
...
// Read from database to get res
...
// Cycle through all database results and add to wordList
wordList.add(new String[] {
res.getString("var1"),
res.getString("var2"),
res.getString("var3") });
...
ISFSObject returnObj = new SFSObject();

// ??? Which put/object should I use here ???
returnObj.putXYZ("wordList", wordList);

send("wordList", returnObj, player);


I tried putSFSObject and putSFSArray, but I get syntax and runtime errors.

I was able to use putUtfString with wordList.toString(), but I had problems converting it back to an array/dictionary in AS3. This approach feels like a clunky work-around, too. I want to make sure I'm doing it "the right way".

So... How can I pass a List or multi-dimensional array from my server extension to my client? How can I process the received object as a Dictionary? (I don't see a .toDictionary() method.)
User avatar
Lapo
Site Admin
Posts: 23007
Joined: 21 Mar 2005, 09:50
Location: Italy

Postby Lapo » 17 Jan 2012, 10:32

It looks like you are trying to send an array/list of Strings to the client side. If this is the case you should use the UtfStringArray option:

Code: Select all

SFSObject sfso = new SFSObject()
sfso.putUtfStringArray("mydata", theArrayOfStrings)

On the client side (AS3) the getUtfStringArray() method will return an Array containing those Strings.
Lapo
--
gotoAndPlay()
...addicted to flash games
secret007
Posts: 58
Joined: 29 May 2009, 06:27

Re: How to pass/handle List object from server to client?

Postby secret007 » 13 Mar 2012, 10:25

I m having the same problem, m getting rooms list from a specified zone, but cant figure out how to send it to the client, i tried this:


Code: Select all

ISFSObject sfsObject = SFSObject.newInstance();
sfsObject.putSFSArray("TR", (ISFSArray)SmartFoxServer.getInstance().getZoneManager().getZoneByName("SportsGames").getRoomList());


but this way server is not sending any response to the client end, and if i convert the list into "toString()" than on client end its impossible to convert back this string to array ...
======================================================
Choose a job of your choice and you will never have to work in life !!!
User avatar
Lapo
Site Admin
Posts: 23007
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: How to pass/handle List object from server to client?

Postby Lapo » 13 Mar 2012, 10:53

You don't have to send a room list manually.
Room lists are sent to you automatically as soon as you login into a Zone with the SFSEvent.LOGIN event.
Check any of our examples (Tris game, Simple Chat, Game Lobby...) for all the details...
And this too:
http://docs2x.smartfoxserver.com/Develo ... ogin-phase
Lapo

--

gotoAndPlay()

...addicted to flash games
secret007
Posts: 58
Joined: 29 May 2009, 06:27

Re: How to pass/handle List object from server to client?

Postby secret007 » 13 Mar 2012, 11:28

yes you are right but in my case i have four rooms in a zone and all have different group ids i.e.


Code: Select all

MainLobby (GroupID:default)
BasketBall 1 (GroupID:B1)
BasketBall 2 (GroupID:B2)
BasketBall 3 (GroupID:B3)



now when a user enters into a system so he/she is joining MainLobby the defaut group (without subscription), and at that time i want room list, so that i can place movieclips of all rooms so that user can now see and click anyone of them to join the lobby (say BasketBall 1 lobby),

but when i m catching room list its only traces MainLobby :

Code: Select all

 trace(_sfs.roomManager.getRoomList());


it means its only getting that room which is joined by user not all rooms that are exists in the zone, so i have to ping server for that ???

if i m on the wrong path than please suggest me the solution,

THANKS.
======================================================
Choose a job of your choice and you will never have to work in life !!!
secret007
Posts: 58
Joined: 29 May 2009, 06:27

Re: How to pass/handle List object from server to client?

Postby secret007 » 13 Mar 2012, 11:39

oh my bad, i forgot to add all groups in the zone's default group setting :D, now its working fine,

thanks for the help, but still i want to know that if there is a way that i want to send server side java list to client, than what "putXYZ" method will be used ??
======================================================
Choose a job of your choice and you will never have to work in life !!!
User avatar
Bax
Site Admin
Posts: 4609
Joined: 29 Mar 2005, 09:50
Location: Italy
Contact:

Re: How to pass/handle List object from server to client?

Postby Bax » 13 Mar 2012, 15:15

secret007 wrote:still i want to know that if there is a way that i want to send server side java list to client, than what "putXYZ" method will be used ??

There isn't a generic method to do this. Lists are passed to the client as SFSArray objects. They can be arrays of bytes, of strings, etc. Based on the methods you have on the SFSObject class, you have to transform your list into something compatible.
Paolo Bax
The SmartFoxServer Team
secret007
Posts: 58
Joined: 29 May 2009, 06:27

Re: How to pass/handle List object from server to client?

Postby secret007 » 14 Mar 2012, 07:40

Bax wrote:
secret007 wrote:still i want to know that if there is a way that i want to send server side java list to client, than what "putXYZ" method will be used ??

There isn't a generic method to do this. Lists are passed to the client as SFSArray objects. They can be arrays of bytes, of strings, etc. Based on the methods you have on the SFSObject class, you have to transform your list into something compatible.



got it thanks ...
======================================================
Choose a job of your choice and you will never have to work in life !!!

Return to “SFS2X ActionScript 3 API”

Who is online

Users browsing this forum: No registered users and 17 guests