How to handle SFSEvent.SERVER_READY to multiple BaseServerEventHandler?

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

Moderators: Lapo, Bax

MicroEyes
Posts: 13
Joined: 24 Jun 2013, 10:22

How to handle SFSEvent.SERVER_READY to multiple BaseServerEventHandler?

Postby MicroEyes » 26 Dec 2018, 12:52

Hi,
I have a case in which i need to send SFSEvent.SERVER_READY to my 3 classes. But right now, only last registered class is getting the event.
All 3 classes are extending BaseServerEventHandler.


My ZoneExtension class code below:

Code: Select all

@Override
    public void init() {
             addEventHandler(SFSEventType.SERVER_READY, GameCreator.class);
             addEventHandler(SFSEventType.SERVER_READY, TicketHandler.class);
             addEventHandler(SFSEventType.SERVER_READY, ZoneDataHandler.class);       <----------- Only this class is receiving event.
      }
User avatar
Lapo
Site Admin
Posts: 23025
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: How to handle SFSEvent.SERVER_READY to multiple BaseServerEventHandler?

Postby Lapo » 26 Dec 2018, 15:17

Hello,
what version of SFS2X are you using?
I've tested your use case and I did not find any problems. Adding 3 event handlers for the SERVER_READY event triggers all of them.

Can you check your log files for potential errors?
Thanks
Lapo
--
gotoAndPlay()
...addicted to flash games
MicroEyes
Posts: 13
Joined: 24 Jun 2013, 10:22

Re: How to handle SFSEvent.SERVER_READY to multiple BaseServerEventHandler?

Postby MicroEyes » 26 Dec 2018, 18:21

Lapo wrote:Hello,
what version of SFS2X are you using?
I've tested your use case and I did not find any problems. Adding 3 event handlers for the SERVER_READY event triggers all of them.

Can you check your log files for potential errors?
Thanks


I am using SmartFoxServer 2X 2.13.4.
Output of below code is only "TicketHandler intialized -----------" because TicketHandler.class was registered last.


Here is my code sample:
ZoneHandlerExtension.java

Code: Select all

public class ZoneHandlerExtension extends SFSExtension {

    private SignUpAssistantComponent m_signupComp;
    //LoginAssistantComponent m_loginHandler;

    private GameCreator m_autoRoomCreator;

    @Override
    public void init() {
        trace("HelloWorld.MyZoneExtension");
        addEventHandler(SFSEventType.SERVER_READY, GameCreator.class);
        addEventHandler(SFSEventType.SERVER_READY, TicketHandler.class);
    }
}


GameCreator.java

Code: Select all

public class GameCreator extends BaseServerEventHandler {

    @Override
    public void handleServerEvent(ISFSEvent isfse) throws SFSException {
        trace("GameCreator intialized -----------------");
    }
}


TicketHandler.java

Code: Select all

public class TicketHandler extends BaseServerEventHandler {

    @Override
    public void handleServerEvent(ISFSEvent isfse) throws SFSException {
        trace("TicketHandler intialized -----------------");
    }
}


Output:

Code: Select all

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
 >> Zone: tapMe
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

23:48:08,799 INFO  [main] Extensions     - {tapMe}: HelloWorld.MyZoneExtension
23:48:08,801 INFO  [main] Extensions     - {tapMe}: SignupHandler----------------------
23:48:08,814 INFO  [main] managers.SFSRoomManager     - Room created: { Zone: --=={{{ AdminZone }}}==-- }, [ Room: AdminRoom, Id: 0, Group: default, isGame: false ], type = SFSRoom
23:48:08,858 INFO  [main] core.AdminToolService     - AdminTool Service started
23:48:09,348 INFO  [SFSWorker:Sys:1] v2.SmartFoxServer     - Listening Sockets: { 0.0.0.0:9933, (Tcp) } { 127.0.0.1:9933, (Udp) }
23:48:09,350 INFO  [SFSWorker:Sys:1] v2.SmartFoxServer     -
 _____ _____ _____    ___ __ __
|   __|   __|   __|  |_  |  |  |
|__   |   __|__   |  |  _|-   -|
|_____|__|  |_____|  |___|__|__|
 _____ _____ _____ ____  __ __
| __  |   __|  _  |    \|  |  |
|    -|   __|     |  |  |_   _|
|__|__|_____|__|__|____/  |_|
[ 2.13.4 ]

23:48:09,354 INFO  [SFSWorker:Sys:1] v2.SmartFoxServer     - SmartFoxServer 2X (2.13.4) READY!
23:48:09,361 INFO  [SFSWorker:Ext:3] Extensions     - {tapMe}: TicketHandler intialized -----------------
23:48:11,194 INFO  [main] v3.SessionFilter     - BlueBox-2X Service (3.3.0) READY.
User avatar
Lapo
Site Admin
Posts: 23025
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: How to handle SFSEvent.SERVER_READY to multiple BaseServerEventHandler?

Postby Lapo » 27 Dec 2018, 10:33

Hi,
the problem stems from the fact that you're using SFSExtension's method addEventHandler. If you check the docs it states that it works with one listener per event only. Thus by adding multiple handlers you're effectively overwriting the previous one.

There's a way around this, but it takes a little refactoring.
Take a look at this example:

Code: Select all

public class MultiReadyTestExtension extends SFSExtension
{
   @Override
   public void init()
   {
      trace("Init Success!");
      addEventListener(SFSEventType.SERVER_READY, new Listener1());
      addEventListener(SFSEventType.SERVER_READY, new Listener2());
   }
}

And relative listeners:

Code: Select all

import com.smartfoxserver.v2.core.ISFSEvent;
import com.smartfoxserver.v2.core.ISFSEventListener;
import com.smartfoxserver.v2.exceptions.SFSException;

public class Listener1 implements ISFSEventListener
{
   @Override
   public void handleServerEvent(ISFSEvent event) throws SFSException
   {
      System.out.println("---> Listener 1 Ready event");
   }
}

public class Listener2 implements ISFSEventListener
{
   @Override
   public void handleServerEvent(ISFSEvent event) throws SFSException
   {
      System.out.println("---> Listener 2 Ready event");
   }
}


This is basically invoking the SFSExtension parent class. The SFSExtension essentially extends the parent's mechanism by keeping track of all listeners and auto-removing them on Extension destroy(). However it does not support multiple listeners for the same event, so if you need the multi-listener approach you need to use the code above and manage your listeners manually. Meaning that your Extension's destroy() method will need to look like this:

Code: Select all

@Override
public void destroy()
{
   // This removes all event handlers added via addEventHandler()
   super.destroy();
   
   // Here we manually remove the listeners addede via the parent's addEventListener
   removeEventListener(...);
   ...
   ...
}

Hope it helps
Lapo

--

gotoAndPlay()

...addicted to flash games
MicroEyes
Posts: 13
Joined: 24 Jun 2013, 10:22

Re: How to handle SFSEvent.SERVER_READY to multiple BaseServerEventHandler?

Postby MicroEyes » 28 Dec 2018, 17:44

Thanks, @Lapo.
The approach is working for me.
Just wanted to know how can I use trace() function, if not extending from any base class?
User avatar
Lapo
Site Admin
Posts: 23025
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: How to handle SFSEvent.SERVER_READY to multiple BaseServerEventHandler?

Postby Lapo » 29 Dec 2018, 09:25

I would suggest to pass an instance of your main Extension to your event listener classes in the constructor and use that object to invoke trace().
Alternatively you can use standard logging, as explained here:
https://smartfoxserver.com/blog/custom- ... xtensions/

Hope it helps
Lapo

--

gotoAndPlay()

...addicted to flash games
MicroEyes
Posts: 13
Joined: 24 Jun 2013, 10:22

Re: How to handle SFSEvent.SERVER_READY to multiple BaseServerEventHandler?

Postby MicroEyes » 31 Dec 2018, 20:20

Thanks, Lapo.

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 50 guests