getUserList returns an empty array when there is a user

Need help with SmartFoxServer? You didn't find an answer in our documentation? Please, post your questions here!

Moderators: Lapo, Bax

TreeTree
Posts: 12
Joined: 28 May 2010, 00:22

getUserList returns an empty array when there is a user

Post by TreeTree »

I'm trying to make a lobby which displays a list of all the players in the zone and the game rooms they are in, like the game Gunz. When I call server.getRoomList and I go through the roomList that gets returned:

Code: Select all

private function GotRooms (event:SFSEvent) {
	for (var room in event.params.roomList) {
		trace (event.params.roomList [room].getUserList ());
		trace (event.params.roomList [room].getUserCount ());
	}
}
I have 4 rooms in the zone, Lobby, Test1, Test2, and Test3. Lobby is the autoJoin room and when after I join Lobby I call getRoomList. When I trace the userList of each room I get nothing at all even though I am currently in Lobby while the userCount traces 1, 0, 0, 0, which is correct.
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

The userList is not populated for Rooms that you haven't joined.
If it were so you had to download the entire list of all users connected to the Zone which would be overwhelming.

In other words, the client only "sees" users in the Room(s) he has joined. The user count however is kept up to date by the system so that one can know the Room status from "outside"
Lapo
--
gotoAndPlay()
...addicted to flash games
TreeTree
Posts: 12
Joined: 28 May 2010, 00:22

Post by TreeTree »

If that's the case would it be possible to achieve this:
A list of game rooms, when I click or hover my mouse over each room a list of the players in that room will be displayed?
Or will I have to make the lobby and all the rooms one big room so I can get a list of every player and create my own "rooms" with my game?
BigFIsh
Posts: 1698
Joined: 25 Feb 2008, 19:26
Location: New Zealand

Post by BigFIsh »

What you can do:

1. client send str command to server clicking on a room
2. server gets all player names and then response
3. client receives the response containing list of player names, and then display them..
Smartfox's forum is my daily newspaper.
TreeTree
Posts: 12
Joined: 28 May 2010, 00:22

Post by TreeTree »

I've been trying to create the lobby but I keep getting a problem, if I log on first, the player list shows just me, then when I log on with another program that program shows both players but the first program only shows the second player.

Code: Select all

private function UpdatePlayerBox (room) {
	while (playerbox.numChildren > 1) {
		playerbox.removeChildAt (1);
	}

	var userlist:Array = room.getUserList ();
			
	for (var user:String in userlist) {
				
		var playeritem:* = new playeritem_mc ();
		playeritem.y = 18.5 * (playerbox.numChildren - 1);
		playeritem.playername.text = userlist [user].getName ();
		playerbox.addChild (playeritem);
	}
}
		
private function PlayerJoined (event:SFSEvent) {
	UpdatePlayerBox (server.getActiveRoom ());
}
		
private function PlayerLeft (event:SFSEvent) {
	UpdatePlayerBox (server.getActiveRoom ());
}
I ran traces and put the outputs in textboxes and for some reason when the second player logs on, the first player does UpdatePlayerBox but the first player isn't in the userlist. Both players are joining the same room.

Also when I close the second player's program the first player's player list becomes empty because the first player seemingly disappeared from the userlist. When I close the first player's program, the second player gets a 1088 error that the root element in the document must be well formed.[/code]
wylkyn
Posts: 18
Joined: 26 May 2010, 16:19

Post by wylkyn »

I am having this problem as well, and I can't figure out what I'm doing wrong. When I get the userlist for the room, it seems to be sending an array that contains a bunch of undefined items with the last item being the most recent person who joined. The number of these seem to accumulate as I test, as if the server is holding on to old corrupted user objects or something. I'm not sure what is causing this, but I have not been able to solve this problem.
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

wylkyn:
You probably don't.
Please read here:
http://forums.smartfoxserver.com/viewtopic.php?t=5440
Lapo
--
gotoAndPlay()
...addicted to flash games
wylkyn
Posts: 18
Joined: 26 May 2010, 16:19

Post by wylkyn »

Thanks. I will try that, though it doesn't explain why the original user no longer appears in the list. This was happening when I was using the Userlist Samrtfox bits as well, by the way. The Userlist of the one who joined second would show both users, but the Userlist of the first user would only display the one who just joined. That's why I created my own user list, so I could try to find out why that was happening. But I will use a for...in loop and see what I get.
wylkyn
Posts: 18
Joined: 26 May 2010, 16:19

Post by wylkyn »

Changing the for loop did eliminate the undefined members of the list, but I still have the problem mentioned above. Like I said, this problem was occurring when I was using the Smartfox Bits version of the Userlist as well, so I'm kind of stumped as to why this is happening.

Here is how I am retrieving the userlist when an event listener calls for it (when someone joins the room, for example):

Code: Select all

var room:Room = smartfox.getRoom(smartfox.activeRoomId);
var userList:Array = room.getUserList();
The first user joins, and I can see him in the list. The second user joins, and his list displays both users, but the first user now only sees the second user - not himself anymore. Then if the first user leaves the room, the second user gets the following error messages:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at it.gotoandplay.smartfoxserver.handlers::SysHandler/handleUserLeaveRoom()
at Function/http://adobe.com/AS3/2006/builtin::apply()
at it.gotoandplay.smartfoxserver.handlers::SysHandler/handleMessage()
at it.gotoandplay.smartfoxserver::SmartFoxClient/xmlReceived()
at it.gotoandplay.smartfoxserver::SmartFoxClient/handleMessage()
at it.gotoandplay.smartfoxserver::SmartFoxClient/handleSocketData()
TypeError: Error #1088: The markup in the document following the root element must be well-formed.
at it.gotoandplay.smartfoxserver::SmartFoxClient/xmlReceived()
at it.gotoandplay.smartfoxserver::SmartFoxClient/handleMessage()
at it.gotoandplay.smartfoxserver::SmartFoxClient/handleSocketData()
Any ideas on what is causing this?
wylkyn
Posts: 18
Joined: 26 May 2010, 16:19

Post by wylkyn »

Oh, and the admin tool shows both users in the room, so it's not that the first user is getting dropped or anything. They are both there. But for some reason they are getting two different userlist arrays from the server or something.
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

wylkyn
I would recommend to take a look at one of our examples like the SimpleChat for AS3. This way you can compare your code and see what you are doing wrong. I can assure you that the Users should all appear as they do in the Simple Chat.

As regards the SFSBits UserList the fact that the connected user doesn't show up in the list is just normal. That's the way the component works, it shows all the other users in the Room, not your user.
Lapo
--
gotoAndPlay()
...addicted to flash games
wylkyn
Posts: 18
Joined: 26 May 2010, 16:19

Post by wylkyn »

Lapo
Thanks for your reply. I looked for an example of the SimpleChat for AS3 but I can only seem to find one for AS2. Is there a download link?

I'm a Flash programmer, and I am trying to program this as actual object-oriented AS3, with external AS3 class definitions and packages. So far, I haven't seen any examples that do that - all of the examples I've seen are either Flex or extremely old-style Flash programming updated with AS3 syntax. Frankly, if I have to copy that AS1 frame script style of programming to get this to work, I don't really see the point of using this. It doesn't seem to be translating well to real classes and packages, or I'm just not understanding well enough how to make it work. I'm certainly willing to believe the latter, but without proper documentation and examples, I'm running out of ideas.

Sorry if this comes off as frustrated - I've been doing nothing but sifting through the examples, trying to find the problem for the past week. I'm sure it's something stupid that I have done. I just can't figure out what that stupid mistake is.
wylkyn
Posts: 18
Joined: 26 May 2010, 16:19

Post by wylkyn »

I tried using the code from the AS3 Flex SmartFoxChat example verbatim, but I am still having the same issues. One thing I find very confusing is that I have been told that I should expect blank members in the associative array that comes back from the getUserList method. And yet the following code for the Flex chat:

Code: Select all

// Cycle through all users in the list and add them to the provider
for each(var u:User in room.getUserList())
{
	provider.addItem( {label:u.getName(), data:u.getId()} )
}
seems to assume that it will get back User objects only. I haven't used the "for..each" loop at all, but all the examples I have seen for its use talk about how it lists all the values in the target array. Of course, their iterative variable is usually not typed, unlike u:User in the above code. Does that typing exclude the non-User members from the loop execution? Or does it try to cast every member as a User object? I can't figure out if this is sloppy coding, or if I just don't understand this particular use of this particular loop. Regardless, it doesn't work any better than the way I had it looping through the members of the array and testing to make sure they are User objects before trying to use them. I am still encountering the same errors. Even if I simply try to trace the User object, I get the Error#1009 I posted above. It is bizarre.
wylkyn
Posts: 18
Joined: 26 May 2010, 16:19

Post by wylkyn »

Okay, so today as a test I took the AvatarChat example and simply modified it to point to our server and zone, just to see what would happen. Again, the fact that it takes in the userlist and expects every item in the list to be a User object is throwing up errors. Here is the relevant code from your own example:

Code: Select all

var userList:Object = roomObj.getUserList()
for ( var i:String in userList )
{
	var user:User = userList[ i ]
	var uName:String = user.getName()
	var uId:Number = user.getId()
	if ( uName != myName )
	{
		var uVars:Object = user.getVariables()
		var mc:Avatar = new Avatar
		mc.id = "avatar_" + uId
		mc.name = "avatar_" + uId
		avatarMC.addChild( mc )
		mc.x = uVars.px
		mc.y = uVars.py
		mc.disc.gotoAndStop( uVars.col )
		mc.uname.text = uName
	}
}
There is no type checking to make sure that what it is receiving from that iteration of the userlist is even an object. I have been told that empty list items should be expected - so I don't get how this is a good example of how to use your product with Flash AS3. Isn't this prone to errors as soon as it encounters an empty list member? That's certainly what happened in my test. It assumes an object with properties and finds nothing.

Also, you said:
As regards the SFSBits UserList the fact that the connected user doesn't show up in the list is just normal. That's the way the component works, it shows all the other users in the Room, not your user.
That's not the case with this example. Before it threw up a #1009 error, it definitely showed both users in the SFSBits Userlist. So...???

Just for sake of perspective, the programmer who set up our server also programmed a test interaction for the iPhone, and that is working just fine. Users are able to sign into the room, see themselves and each other, and interact. The problems we are encountering only seem to be happening with the Flash API. He is just as puzzled as I am.
wylkyn
Posts: 18
Joined: 26 May 2010, 16:19

Post by wylkyn »

I really though I had found the problem when I discovered a thread that talked about having to set the myUserId variable when a custom login is being used. I added that code, but unfortunately that didn't solve my problem. It did solve the Error #1009 problem, because the userId was coming back as -1. But I still have the problem where one user is seeing both users in the list, and the original user is replaced by the latest user to log in. Then if he drops out, the other user gets the 1088 error I mentioned above.
Post Reply