Page 1 of 1
How can I send message from server to all users?
Posted: 01 Jul 2011, 07:29
by COB
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?
Posted: 02 Jul 2011, 00:16
by dragagon
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.
Posted: 04 Jul 2011, 09:13
by COB
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?
Posted: 04 Jul 2011, 12:33
by appels
this.send("SpawnMe", data, userList);
You don't need a sender.
Posted: 04 Jul 2011, 12:50
by COB
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.
Posted: 11 Jul 2011, 08:05
by dest
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.
Posted: 12 Jul 2011, 06:41
by ThomasLund
Moved to server forum
Posted: 12 Jul 2011, 07:19
by COB
Thank you Thomas. We will appreciate any help. At the moment this is the only blocking point for our app. Everything else is done.
Posted: 12 Jul 2011, 12:35
by moranil
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
Posted: 13 Jul 2011, 08:27
by dest
Code: Select all
public TaskRunner(GameController gm)
should be
Code: Select all
public TaskRunner(GameExtension gm)
Am I right?
--------
Thank you
moranil. Its working

Posted: 13 Jul 2011, 11:59
by moranil
dest wrote:
Am I right?
yes, you are right. I forgot to change code at that point.