send request to all users (java)

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

FireSmartFox
Posts: 33
Joined: 28 Sep 2018, 19:47

send request to all users (java)

Postby FireSmartFox » 18 Feb 2019, 17:30

Hi,

is this code in general a good idea if I want to send something to ALL currently connected users?

Code: Select all

public void sendToAll(String pCommand, ISFSObject resObj) {
      List<User> userlist2 = new ArrayList<>();

      Collection<User> userliste = this.getParentZone().getUserList();
      for (User derUser : userliste) {
         userlist2.add(derUser);
      }

      send(pCommand, resObj, userlist2);
   }

I found out I can't put this.getParentZone().getUserList() to send() so I have to put every user to a List<>.
Thanks for any hint.
User avatar
Lapo
Site Admin
Posts: 23026
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: send request to all users (java)

Postby Lapo » 18 Feb 2019, 17:51

Hi,
you don't need to copy the Users to a new list. Instead you can simply do this:

Code: Select all

public void sendToAll(String pCommand, ISFSObject resObj)
{
      send(pCommand, resObj, (List<User>) getParentZone().getUserList());
}

This way you don't waste any memory ;)
Hope it helps
Lapo
--
gotoAndPlay()
...addicted to flash games
FireSmartFox
Posts: 33
Joined: 28 Sep 2018, 19:47

Re: send request to all users (java)

Postby FireSmartFox » 18 Feb 2019, 18:04

I see, thanks a lot.
Even this is already just Java related, I am wondering why this is taking so much more memory because I thought I am passing just references into a new List?
Apart from it, I now took your version. :D
User avatar
Lapo
Site Admin
Posts: 23026
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: send request to all users (java)

Postby Lapo » 19 Feb 2019, 07:56

Hi,
the code you posted here doesn't necessarily use "so much more memory". As you said you're just copying references which is not memory intensive, however when the server is very active and with lots of traffic you may be creating hundreds of lists and copying thousands of references every second which indeed wastes a considerable amount of memory and forces the garbage collector to work more often.

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
FireSmartFox
Posts: 33
Joined: 28 Sep 2018, 19:47

Re: send request to all users (java)

Postby FireSmartFox » 25 Mar 2019, 15:08

I created a HashMap to have an own user object called "Player". Every Place instance has a reference to the SFS user. I do this to assign some properties and other stuff to the player.
But sometimes when I literate through the list to send something to all players with a certain property, I get a ConcurrentModificationException.

Is there a way to send data to all users in a zone with a certain property / variable and having a performant filter? For example if I use the properties assigned to users?
For security reason, I don't want to submit the data to all users in the zone.
User avatar
Lapo
Site Admin
Posts: 23026
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: send request to all users (java)

Postby Lapo » 25 Mar 2019, 16:06

FireSmartFox wrote:I created a HashMap to have an own user object called "Player". Every Place instance has a reference to the SFS user. I do this to assign some properties and other stuff to the player.

If you need to assign custom properties to a User you can use the User.getProperty()/setProperty() without having to rely on another data structure. Also you can use hidden UserVariables (they exist only on the server side and are not sent to the client).

But sometimes when I literate through the list to send something to all players with a certain property, I get a ConcurrentModificationException.

That's because you're probably using a non thread-safe collection, such a HashMap. Use a ConcurrentHashMap instead.

Is there a way to send data to all users in a zone with a certain property / variable and having a performant filter? For example if I use the properties assigned to users?
For security reason, I don't want to submit the data to all users in the zone.

You can use MatchExpressions to filter Rooms or Users in the Zone based on custom parameters.
If you choose to do so, which I would recommend, you can use UserVariables to set the user's custom properties (setting them as "hidden", so they exist on server side only) and then you can filter the User via --> SFSApi.findUsers()

You can learn more on how to use the system here:
http://docs2x.smartfoxserver.com/Advanc ... i#matchExp
Also here:
http://docs2x.smartfoxserver.com/api-do ... ssion.html

Hope it helps
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 60 guests