Sending message to all users of a Room created from server side

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

Moderators: Lapo, Bax

Sahadev
Posts: 6
Joined: 25 Nov 2019, 16:28

Sending message to all users of a Room created from server side

Postby Sahadev » 25 Nov 2019, 16:43

Hi All,
I am very new to SFS and just exploring things.
I have created a game room dynamically from server code and willing to send a message to all players joined to the room (room.getExtension().send("start", data, room.getUserList());).
What I observe here is, the message is not sent to all players in the room. It is having intermittent behavior where some times the message is sent to only 1 person and some times it is sent to none (here, I have got only two players in the room). When I do same with static room (i.e., room is created in a zone via admin portal), the message is sent to all players. Am I missing something here with my server side room creation? How do I send the message to all players in the room? Thanks in advance of the help.
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Sending message to all users of a Room created from server side

Postby Lapo » 26 Nov 2019, 09:18

Hello,
Sahadev wrote:Hi All,
I am very new to SFS and just exploring things.
I have created a game room dynamically from server code and willing to send a message to all players joined to the room (room.getExtension().send("start", data, room.getUserList());).

just use send() without the rest. Example:

Code: Select all

send("start", data, getParentRoom().getUserList());


What I observe here is, the message is not sent to all players in the room. It is having intermittent behavior where some times the message is sent to only 1 person and some times it is sent to none (here, I have got only two players in the room).

Make sure to use the code above.
Also make sure the users you expect to receive the message are actually joined in the Room. (You can check via the AdminTool > Zone Monitor and see how's in the Room)

Cheers
Lapo
--
gotoAndPlay()
...addicted to flash games
Sahadev
Posts: 6
Joined: 25 Nov 2019, 16:28

Re: Sending message to all users of a Room created from server side

Postby Sahadev » 26 Nov 2019, 13:58

Thanks Lapo for the quick response and guidance.

Here, I had to use the room.getExtension() to access the send method as the call is made somewhere inside of my own class.
I'll check and let you know whether that is causing the problem, by doing the way you suggested.

I have checked that the users have joined the room.

Once again, thanks for the help.
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Sending message to all users of a Room created from server side

Postby Lapo » 26 Nov 2019, 14:33

Sahadev wrote:Here, I had to use the room.getExtension() to access the send method as the call is made somewhere inside of my own class.

Fair enough.
In that case no problem. You can do it, and it will work.
If it still doesn't make sure to check the server logs for errors.

Let us know
Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
Sahadev
Posts: 6
Joined: 25 Nov 2019, 16:28

Re: Sending message to all users of a Room created from server side

Postby Sahadev » 26 Nov 2019, 18:27

Hi Lapo, in this case, I see the issue is because of setting the room type flag as game (i.e.,

Code: Select all

rs.setGame(true);
).
Actually, I overlooked the issue with static room creation and thought that it is available only in dynamic room creation.
Now I am able to reproduce the scenario even in static room creation also (i.e., creating room in admin portal manually).
After removing this room setting in dynamic room creation, I am able to send custom message to all joined users.

But I need to set this flag as I need spectators and player ids for the players.

Am I missing any basic concept here? Could you please suggest any other approach?

Thanks in advance.
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Sending message to all users of a Room created from server side

Postby Lapo » 27 Nov 2019, 10:17

Hi,
I should see what your code does exactly.
As regards setting the isGame flag, I can't say it makes any differences.

The send() method operates the same regardless of the type of Room: it takes an SFSObject and a list of recipients. If you call room.getUserList() you will get the complete lists of clients joined in that Room. It doesn't matter if the Room is dynamic or static, game or regular.

I think the problem is somewhere else, but without the details I have no idea where it might be.
Thanks
Lapo

--

gotoAndPlay()

...addicted to flash games
Sahadev
Posts: 6
Joined: 25 Nov 2019, 16:28

Re: Sending message to all users of a Room created from server side

Postby Sahadev » 27 Nov 2019, 16:40

Hi Lapo, thanks for giving your time on my issue.

I'll try to explain what I want to do and doing, briefly here.

I want to build a server side logic for small turn based game where people join in an available room.
Here, I have two extensions. One for Zone and another for Room. In the init method of Zone extension, I've registered an event USER_JOIN_ZONE. When a user logs in to the zone, the event handler looks for an available room and adds the user to the room (i.e., join), otherwise, it creates a room dynamically and then adds the player to that room. At the time of creation of room, room extension is set. Here, initially, no room is available. I mean, no static room in zone.
Join room:

Code: Select all

getApi().joinRoom(user, theRoom, null, isSpectator, null);

Create a new room:

Code: Select all

private Room makeNewRoom() throws SFSCreateRoomException {
        Room room = null;
        MyZoneExtension gameExt = (MyZoneExtension) getParentExtension();

        CreateRoomSettings rs = new CreateRoomSettings();
        rs.setGame(true);
        rs.setDynamic(true);
        rs.setName("Room_" + gameExt.getNextRoomId()); //getNextRoomId() gives an incremented AtomicInteger
        rs.setMaxUsers(gameExt.getMaxPlayerCount()); // getMaxPlayerCount() returns a final field say; 5
        rs.setGroupId("Play");
        rs.setMaxSpectators(gameExt.getMaxPlayerCount());
        rs.setExtension(new CreateRoomSettings.RoomExtensionSettings("MyRoomExtension", "sfs2x.extensions.games.myroom.MyRoomExtension"));

        try {
            room = getApi().createRoom(gameExt.getParentZone(), rs, null, false, null, false, false);
        } catch (SFSCreateRoomException ex) {
            // ... handle exception
        }

        return room;
    }

This is it zone extension.


Now, in the room extension jar, I'm trying to bring the turn based game logic. Here, I had cut down all the partially existing logic to reproduce the issue with minimal code.

Code: Select all

public class MyRoomExtension extends SFSExtension {

    private Runner _runner;
    private ScheduledFuture<?> _runnerTask;

    @Override
    public void init() {

        addRequestHandler("ack", UserRepliedEventHandler.class);

        _runner = new Runner();

        try {
            simulateGame();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void destroy() {
        super.destroy();

        // Destroy RunnerTask
        if (_runnerTask != null)
            _runnerTask.cancel(true);
    }

    private void simulateGame() {
        // Start NPC Task
        _runnerTask = SmartFoxServer.getInstance().getTaskScheduler().scheduleAtFixedRate(_runner, 1000, // initial delay
                1000, // run every 1s
                TimeUnit.MILLISECONDS);

    }

    private class Runner implements Runnable {

        public Runner() {
        }

        @Override
        public void run() {
            try {
                processMatchLoop();
            } catch (SFSRoomException e) {
                e.printStackTrace();
            }
        }
    }

    private void processMatchLoop() throws SFSRoomException {
        Room room = getParentRoom();
        this.send("start", null, room.getUserList());
    }


I am able reproduce the issue with this code. Only partial users (mostly 1 user) are receiving the message.
If I set game flag as false, it's working. I mean, all users receive message.

What I notice here is, server is sending the message to all connected users but not all clients are receiving it.
Could you please let me know whether my approach is in right direction? Do you see any issues in my approach?

Thanks in advance for your time.
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Sending message to all users of a Room created from server side

Postby Lapo » 28 Nov 2019, 10:36

Hi,
I copied your Room Extension and ran a test with a game Room and a non-game Room (both running the same Extension)
As expected I cannot reproduce your problem. Every User joined in the Room receives the Extension messages once every second. Also, I've tested joining both as a player and as a spectator.

Can you reproduce this issue locally?
Are you sure you have no errors in the server logs?
What version of SFS2X are you running?

Could you please let me know whether my approach is in right direction? Do you see any issues in my approach?

Sure, your approach is correct and you should not see any problems.

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
Sahadev
Posts: 6
Joined: 25 Nov 2019, 16:28

Re: Sending message to all users of a Room created from server side

Postby Sahadev » 29 Nov 2019, 17:29

Thanks Lapo for giving your time and helping me.

As of now, I am only checking in my local windows 10 system.
I don't see any error in server log.
I am using version 2.13.0, community edition.

The behavior what I see here is an intermittent behavior. Some times, all players get extension messages, and some times only few players get messages.

I may have to verify in some other machine then.

Thanks for confirming that the approach is correct.

I'll get back to you if I still see the problem.
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Sending message to all users of a Room created from server side

Postby Lapo » 30 Nov 2019, 17:08

In a local environment it should never happen. Over the internet it may happen only to very few clients whose connection is really bad.
Let us know.
Lapo

--

gotoAndPlay()

...addicted to flash games
Sahadev
Posts: 6
Joined: 25 Nov 2019, 16:28

Re: Sending message to all users of a Room created from server side

Postby Sahadev » 01 Dec 2019, 15:51

Hi Lapo, it's my bad that I had a bug in my unity client. mistakenly, there was an un-registering all the events statement

Code: Select all

sfs.RemoveAllEventListeners();
in ROOM_JOIN event handler.
When this event was triggered, things were going wrong. Otherwise, it was working fine.
But, I have a followup question here. When the user logs in successfully, I am registering the following events.

Code: Select all

      sfs.AddEventListener(SFSEvent.CONNECTION_LOST, OnConnectionLost);
      sfs.AddEventListener(SFSEvent.PUBLIC_MESSAGE, OnPublicMessage);
      sfs.AddEventListener(SFSEvent.ROOM_JOIN, OnRoomJoin);
      sfs.AddEventListener(SFSEvent.ROOM_JOIN_ERROR, OnRoomJoinError);
      sfs.AddEventListener(SFSEvent.USER_ENTER_ROOM, OnUserEnterRoom);
      sfs.AddEventListener(SFSEvent.USER_EXIT_ROOM, OnUserExitRoom);
      sfs.AddEventListener(SFSEvent.EXTENSION_RESPONSE, OnExtensionResponse);
      sfs.AddEventListener(SFSEvent.ROOM_VARIABLES_UPDATE, roomVarUpdate);


And, according to the sever logic, the successfully logged in user is kept in a room. So, all the time, the callback OnRoomJoin should be called.
But, for some of the users, this call back is not at all called (not sure any other missing event here is fired when the user enters into a room). Why is it so?
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Sending message to all users of a Room created from server side

Postby Lapo » 02 Dec 2019, 14:25

Sahadev wrote:But, I have a followup question here. When the user logs in successfully, I am registering the following events.

It's best to register all the required events before connecting. And removing the listeners after the disconnection.

And, according to the sever logic, the successfully logged in user is kept in a room.

What do you mean by "kept"? Joined?

So, all the time, the callback OnRoomJoin should be called.
But, for some of the users, this call back is not at all called (not sure any other missing event here is fired when the user enters into a room). Why is it so?

There are two login events. SFSEvent.LOGIN and SFSEvent.LOGIN_ERROR, make sure those are both handled.

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 55 guests