get All Users In a Zone

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

Moderators: Lapo, Bax

proschigom
Posts: 32
Joined: 09 Feb 2006, 12:12

get All Users In a Zone

Postby proschigom » 01 May 2006, 22:13

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

Postby Lapo » 02 May 2006, 10:59

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.

:)
Lapo
--
gotoAndPlay()
...addicted to flash games
proschigom
Posts: 32
Joined: 09 Feb 2006, 12:12

Postby proschigom » 02 May 2006, 14:24

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

Postby Lapo » 02 May 2006, 14:40

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
Lapo

--

gotoAndPlay()

...addicted to flash games
winyman
Posts: 7
Joined: 21 Jul 2008, 09:08
Location: Cologne, Germany

getAllUsersinZone

Postby winyman » 21 Jul 2008, 09:14

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

Postby Lapo » 21 Jul 2008, 11:17

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
Lapo

--

gotoAndPlay()

...addicted to flash games
winyman
Posts: 7
Joined: 21 Jul 2008, 09:08
Location: Cologne, Germany

Postby winyman » 21 Jul 2008, 11:52

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
winyman
Posts: 7
Joined: 21 Jul 2008, 09:08
Location: Cologne, Germany

Postby winyman » 22 Jul 2008, 08:57

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

Postby Lapo » 22 Jul 2008, 12:15

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
Lapo

--

gotoAndPlay()

...addicted to flash games
User avatar
ptdgames
Posts: 39
Joined: 10 Mar 2008, 21:55

Postby ptdgames » 30 Jul 2008, 20:44

The docs (at least for the Java API) show a function called Zone.getUserList(). Call that.
Jipii
Posts: 72
Joined: 18 Aug 2008, 08:53
Location: Frankfurt, Germany

Postby Jipii » 10 May 2009, 18:42

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. :)
Thanks!
User avatar
Lapo
Site Admin
Posts: 22999
Joined: 21 Mar 2005, 09:50
Location: Italy

Postby Lapo » 11 May 2009, 06:08

Can you please show the code you are using?
Lapo

--

gotoAndPlay()

...addicted to flash games
duke
Posts: 31
Joined: 16 Apr 2009, 11:23

Postby duke » 12 May 2009, 11:52

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

Postby Lapo » 12 May 2009, 13:20

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 :)
Lapo

--

gotoAndPlay()

...addicted to flash games
Jipii
Posts: 72
Joined: 18 Aug 2008, 08:53
Location: Frankfurt, Germany

Postby Jipii » 12 May 2009, 17:14

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
}

Thanks!

Return to “Server Side Extension Development”

Who is online

Users browsing this forum: No registered users and 23 guests