Page 1 of 2

get All Users In a Zone

Posted: 01 May 2006, 22:13
by proschigom
hi,

is there any simple way to get a list of all the users in a zone, regardless of which room they are in. i know one way is to get the list of rooms and then the list of users. but a simpler way to keep track would be nice.

best regards

Posted: 02 May 2006, 10:59
by Lapo
No, there isn't a direct method to do so.
However you cycle through all the available rooms in the Zone and call the getAllUsers() method, which returns an array of users.

The array returned is not an Actionscript array, but a Java array.
This doesn't change things too much, but a Java array does not provide any of the typical AS array (join, push, pop, sort ...)

Alternatively you could try this:
there's an undocumented Zone method called getAllUsersInZone()
(The reason why it is "hidden" is because it works well in Java and returns Java objects.)

The method returns a java.util.LinkedList filled with all SocketChannels of all users in the Zone.

With this code you could quickly create the full list of users in Actionscript:

Code: Select all

function getAllUsersInZone()
{
	// Grab current extension zone
	var zone = _server.getCurrentZone()
	
	// Get the java list of socket channels
	var listOfChannels = zone.getAllUsersInZone()
	
	// the list of users
	var allUsers = []
	var socketChan = null
	
	for (var i = 0; i < listOfChannels.size(); i++)
	{
		socketChan = listOfChannels.get(i)
		allUsers.push( _server.getUserByChannel( socketChan ) )
	}
	
	return allUsers
}
I haven't tested it, but it should work.

:)

Posted: 02 May 2006, 14:24
by proschigom
interesting,

im actually trying to do this on the client side, can u plz help me out with my code or suggest me a simpler way, here is what ive written but it seems that it does not enter the for(var i in userList), please correct me where im worng:

Code: Select all

function fillUpUserList() {
	for (var i1:Number = 0; i1<roomList_lb.length; i1++) {
		var item:Object = roomList_lb.getItemAt(i1);
		trace("item.ID: "+item.ID);
		thisRoom = smartfox.getRoom(item.ID);
		var userList = thisRoom.getUserList();
		trace("User Count: "+thisRoom.getUserCount());
		trace("User List: "+userList);
		for (var i in userList) {
			trace("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
			var user:User = userList[i];
			var uName:String = user.getName();
			var uId:Number = user.getId();
			if (uId != smartfox.myUserId) {
				userList_lb.addItem(user.getVariable("firstName")+" "+user.getVariable("lastName"), uId);
			}
		}
	}
}
Im getting the item.ID from a datagrid of rooms list. This piece of code is very similar to the one used in sfsTris to get the usersList of the currently active room

Posted: 02 May 2006, 14:40
by Lapo
nay, you can't do that on the client side.
The reason is that you don't have the full list of all users in all rooms available... you only keep the user list of the room(s) that you joined.

Keeping the full list of users on the client side would be an overkill

You will need to use an extension

getAllUsersinZone

Posted: 21 Jul 2008, 09:14
by winyman
Hy,

please help me, I am a SmartFoxServer-Newbie... ;-)

I am looking for a Function like "getAllUsersinZone" so I need to List all logged-in Users from a Zone.

What should I do to get a complete Userlist with this function?
I am programming with AS2 using the "RoomVariables Example" with the Pro-Version.

Please help me.

Thanks and Greetings,

Thorsten

Posted: 21 Jul 2008, 11:17
by Lapo
You can do that on the server side, in your extension code.
Extensions can be developed in Actionscript, Python and Java. Check the documentation for more informations.
Specifically take a look at the Zone object

Posted: 21 Jul 2008, 11:52
by winyman
Hello again,

in the documentation I have found an AS-Template for Extensions.
These has four Blocks:
- init
- destroy
- handleRequest
- handleInternalEvent

The Function "getAllUsersInZone()" seems to be called from the Client and gave "allUsers" back.

Should I wrote the complete Function in the Block "init" or in "handleRequest". Or should I split the Function. What should I do in my FLA-File to call the Function (except "var extensionName:String = "getAllUsersInZone"")?

Please help me and give response.

Thank you and greetings from Germany.

Thorsten

Posted: 22 Jul 2008, 08:57
by winyman
Hello!

I am stepping forward so I am getting some input from the Server by this function now.
But the responded users from the server are looking not very representative to put in my Flash-Application:

it.gotoandplay.smartfoxserver.data.User@36eb66
it.gotoandplay.smartfoxserver.data.User@1988f4b

Can you show me a way to format the users like:

Username1
Username2

Best regards,

Thorsten (from Cologne, Germany)

Posted: 22 Jul 2008, 12:15
by Lapo
You're trying to send the User server side object to the client. It is not possible.
Simply send the name and id of the user by using the getUserId() and getName() methods

Posted: 30 Jul 2008, 20:44
by ptdgames
The docs (at least for the Java API) show a function called Zone.getUserList(). Call that.

Posted: 10 May 2009, 18:42
by Jipii
Lapo wrote:

Code: Select all

function getAllUsersInZone()
{
//...
}
I haven't tested it, but it should work.

:)
It works, but I get a warning: Error in extension: TypeError: Cannot find function getChannel. Internal: -1012
How can I fix this msg?

Thanks anyway. :)

Posted: 11 May 2009, 06:08
by Lapo
Can you please show the code you are using?

Posted: 12 May 2009, 11:52
by duke
Lapo wrote:No, there isn't a direct method to do so.
However you cycle through all the available rooms in the Zone and call the getAllUsers() method, which returns an array of users.

The array returned is not an Actionscript array, but a Java array.
This doesn't change things too much, but a Java array does not provide any of the typical AS array (join, push, pop, sort ...)

Alternatively you could try this:
there's an undocumented Zone method called getAllUsersInZone()
(The reason why it is "hidden" is because it works well in Java and returns Java objects.)

The method returns a java.util.LinkedList filled with all SocketChannels of all users in the Zone.

With this code you could quickly create the full list of users in Actionscript:

Code: Select all

function getAllUsersInZone()
{
	// Grab current extension zone
	var zone = _server.getCurrentZone()
	
	// Get the java list of socket channels
	var listOfChannels = zone.getAllUsersInZone()
	
	// the list of users
	var allUsers = []
	var socketChan = null
	
	for (var i = 0; i < listOfChannels.size(); i++)
	{
		socketChan = listOfChannels.get(i)
		allUsers.push( _server.getUserByChannel( socketChan ) )
	}
	
	return allUsers
}
I haven't tested it, but it should work.

:)
Mind posting a java version of that? :)

Posted: 12 May 2009, 13:20
by Lapo
Here we go:

Code: Select all

ExtensionHelper helper = ExtensionHelper.getInstance();

public List<Users> getAllUsersInZone()
{
	// Grab current extension zone
   	Zone zone = helper.getZone(this.getOwnerZone());
   
   	// Get the java list of socket channels
   	List listOfChannels = zone.getAllUsersInZone();
   
   	// the list of users
	List<Users> allUsers = new ArrayList<Users>();

   	SocketChannel socketChan;
   
   	for (Object obj : listOfChannels)
   	{
	  	socketChan = (SocketChannel) obj;
      	allUsers.push( helper.getUserByChannel(socketChan) );
   	}
   
   return allUsers
}
There's a little mix of generic collections and non-generic collections. This is due to the fact that SFS was started before Java 5 was out.

Next major update will be fully Java 5+ compliant :)

Posted: 12 May 2009, 17:14
by Jipii
Jipii wrote: It works, but I get a warning: Error in extension: TypeError: Cannot find function getChannel. Internal: -1012
How can I fix this msg?

Thanks anyway. :)
Lapo wrote:Can you please show the code you are using?
I used the same code you posted for server side .as above.
With this code you could quickly create the full list of users in Actionscript:

Code: Select all

function getAllUsersInZone()
{
	// Grab current extension zone
	var zone = _server.getCurrentZone()
	
	// Get the java list of socket channels
	var listOfChannels = zone.getAllUsersInZone()
	
	// the list of users
	var allUsers = []
	var socketChan = null
	
	for (var i = 0; i < listOfChannels.size(); i++)
	{
		socketChan = listOfChannels.get(i)
		allUsers.push( _server.getUserByChannel( socketChan ) )
	}
	
	return allUsers
}