How can I send message from server to all users?

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

Moderators: Lapo, Bax

COB
Posts: 68
Joined: 28 Dec 2010, 08:54

How can I send message from server to all users?

Postby COB » 01 Jul 2011, 07:29

I'm trying to send message from server to users which has to be originated from the server. I need this because I would like to have some objects in the game that are controlled only by the server. My idea is to use:

Code: Select all

public void sendObjectMessage(Room targetRoom,
                              User sender,
                              ISFSObject message,
                              java.util.Collection<User> recipients)

from the server API. However, this function does not accept null as a sender (it hangs). How can I do this in other way? Should I create some user that will represent server? Is it even possible?
dragagon
Posts: 19
Joined: 05 Dec 2010, 00:23

Postby dragagon » 02 Jul 2011, 00:16

This is something you have to write yourself. In your extension, when someone logs into the server, you will want to add them to a list of users, and remove them when they disconnect. Then when you need to send a message to all users, you run through the list and send a message to each one.

I cover creating a World class which holds all users in my dev blog and adding the user when they first connect. You would use this same setup for broadcasting messages to all players.
COB
Posts: 68
Joined: 28 Dec 2010, 08:54

Postby COB » 04 Jul 2011, 09:13

I don't have problem with recipients. The problem is that I don't have a sender. I want to send message directly from the server. However, for the function which I mentioned I have to specify the sender...
Can you post some code which you exactly use to send such messages from the server to the clients?
appels
Posts: 464
Joined: 28 Jul 2010, 02:12
Contact:

Postby appels » 04 Jul 2011, 12:33

this.send("SpawnMe", data, userList);
You don't need a sender.
COB
Posts: 68
Joined: 28 Dec 2010, 08:54

Postby COB » 04 Jul 2011, 12:50

Ok, but I would like to send this data from the class (inside the actual extension class) that implements Runnable interface because I have some thread that should send data periodically to the users and I think that this.send will not work.
dest
Posts: 4
Joined: 13 May 2011, 13:29

Postby dest » 11 Jul 2011, 08:05

Hi!
I will give some more details... We want to send message (SFSObject) from SFS2X to all Unity clients. The problem is We would like to do this from class which implements Runnable:

Code: Select all

private class TaskRunner implements Runnable
{
   public void run()
   {
        //FROM HERE
   }
}


Unfortunately there is no method like send. The only possible functions are:

Code: Select all

getApi().sendExtensionResponse(...)
getApi().sendObjectMessage(...)
getApi().sendPublicMessage(...)
getApi().sendAdminMessage(...)
getApi().sendBuddyMessage(...)


But, We cannot find a way to make it work.
Last edited by dest on 12 Jul 2011, 09:18, edited 2 times in total.
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 12 Jul 2011, 06:41

Moved to server forum
Full Control - maker of Unity/C# and Java SFS API and indie games
Follow on twitter: http://twitter.com/thomas_h_lund
COB
Posts: 68
Joined: 28 Dec 2010, 08:54

Postby COB » 12 Jul 2011, 07:19

Thank you Thomas. We will appreciate any help. At the moment this is the only blocking point for our app. Everything else is done.
moranil
Posts: 10
Joined: 15 May 2011, 14:32
Location: India

Postby moranil » 12 Jul 2011, 12:35

the way We have done is:

from extension we do:

Code: Select all

public  class GameExtension extends SFSExtension 
{

    private List<User> userlist = new ArrayList<User>();


 public ScheduledFuture<?> scheduleTaskOnce(int delay)
    {
       SmartFoxServer sfs = SmartFoxServer.getInstance();
       return sfs.getTaskScheduler().schedule(new TaskRunner(this), delay, TimeUnit.SECONDS);   
    }

  public void send(String data)
  {
       SFSObject sfso = new SFSObject();
       sfso.putUtfString("data",data);
      send("cmd",sfso,userlist);   

   }

public class TaskRunner implements Runnable
   {      
      
      GameExtension ext= null;
      
      public TaskRunner(GameController gm)
      {
         this.ext= gm;
      }

public void run()
   {
        //FROM HERE
     this.ext.send("data");
   }

}

}


class taskrunner should have ref to the extension class when inherit send method from sfsextension.

I hope this will solve your problem
----------------------------
Jai Ho!
dest
Posts: 4
Joined: 13 May 2011, 13:29

Postby dest » 13 Jul 2011, 08:27

Code: Select all

public TaskRunner(GameController gm)

should be

Code: Select all

public TaskRunner(GameExtension gm)


Am I right?

--------

Thank you moranil. Its working :)
moranil
Posts: 10
Joined: 15 May 2011, 14:32
Location: India

Postby moranil » 13 Jul 2011, 11:59

dest wrote:Am I right?


yes, you are right. I forgot to change code at that point.
----------------------------
Jai Ho!

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 55 guests