Can i add multiple event handlers for the same event?

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

Moderators: Lapo, Bax

Jochanan
Posts: 79
Joined: 11 May 2018, 09:12

Can i add multiple event handlers for the same event?

Postby Jochanan » 01 Oct 2019, 09:11

Hi,
i have multiple handlers for SFSEventType.USER_DISCONNECT event, but i have noticed, than only the last one is processed.

Is the problem, that only one handler may be used or there is another kind of problem?

Here is my code snippet

Code: Select all

addEventHandler(SFSEventType.USER_DISCONNECT, OnUserDisconnect.class);
addEventHandler(SFSEventType.USER_DISCONNECT, OnRoomLeave.class);


Only handler inside OnRoomLeave.class is processed right now
User avatar
Lapo
Site Admin
Posts: 23025
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Can i add multiple event handlers for the same event?

Postby Lapo » 01 Oct 2019, 15:48

Hi,
sorry I can't reproduce this. I've added two handlers in a simple extension for the USER_DISCONNECT event and I see them both triggering.
This is valid for all events, by the way, as they all support multiple listeners.

What SFS2X version are you using? Can we see the full code of the test that doesn't work?
Thanks
Lapo
--
gotoAndPlay()
...addicted to flash games
User avatar
Rob
Posts: 53
Joined: 01 Jul 2017, 07:33

Re: Can i add multiple event handlers for the same event?

Postby Rob » 01 Oct 2019, 20:56

Uhh... Aren't you supposed to only have ONE handler per event?

The docs for addEventHandler even specifically says so: "NOTE: Each event can have only one handler."

http://docs2x.smartfoxserver.com/api-do ... ang.Class-
User avatar
Rob
Posts: 53
Joined: 01 Jul 2017, 07:33

Re: Can i add multiple event handlers for the same event?

Postby Rob » 02 Oct 2019, 07:53

Lapo wrote:There's been a misunderstanding. I thought you were referring to server-side events :) which support multiple handlers.

I'm not OP so I'm not 100% sure what was meant, but I was also referring to server-side. The docs I linked are for the SFSExtension class, which is only server-side, right?
User avatar
Lapo
Site Admin
Posts: 23025
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Can i add multiple event handlers for the same event?

Postby Lapo » 02 Oct 2019, 08:22

There are two ways to add event handlers in an Extension, using SFSExtension as the base class.

Code: Select all

addEventHandler(SFSEventType eventType, Class<?> theClass)

and

Code: Select all

addEventListener(SFSEventType eventType, IServerEventHandler handler)


The former takes a Class which will be auto-instantiated when needed, and it does not support multiple handlers.
The latter takes an object instance and it does support multiple event handlers.

So what Jochanan reported is correct and it is expected. When I tested however I was not able to reproduce the problem because I used the 2nd approach.

Sorry for the misunderstanding.
Lapo

--

gotoAndPlay()

...addicted to flash games
Jochanan
Posts: 79
Joined: 11 May 2018, 09:12

Re: Can i add multiple event handlers for the same event?

Postby Jochanan » 02 Oct 2019, 12:04

Rob wrote:Uhh... Aren't you supposed to only have ONE handler per event?

The docs for addEventHandler even specifically says so: "NOTE: Each event can have only one handler."

http://docs2x.smartfoxserver.com/api-do ... ang.Class-


I ment this variant.
So, if i understand it correctly, i will change it from

Code: Select all

addEventHandler(SFSEventType.USER_DISCONNECT, OnUserDisconnect.class);
addEventHandler(SFSEventType.USER_DISCONNECT, OnRoomLeave.class);


to

Code: Select all

addEventHandler(SFSEventType.USER_DISCONNECT, new OnUserDisconnect());
addEventHandler(SFSEventType.USER_DISCONNECT, new OnRoomLeave());


And i will answer to myself - NOPE, this does not help. How do i use the second, working approach? I need both handlers to be executed. Thanks :)
User avatar
Lapo
Site Admin
Posts: 23025
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Can i add multiple event handlers for the same event?

Postby Lapo » 02 Oct 2019, 13:04

Use this:

Code: Select all

addEventListener(SFSEventType.USER_DISCONNECT, new OnUserDisconnect());
addEventListener(SFSEventType.USER_DISCONNECT, new OnRoomLeave());

Sorry, this is not part of the documented/official API.
Lapo

--

gotoAndPlay()

...addicted to flash games
User avatar
Lapo
Site Admin
Posts: 23025
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Can i add multiple event handlers for the same event?

Postby Lapo » 02 Oct 2019, 13:08

Also you will have to remove each event listener manually in the destroy method via the corresponding removeEventListener() method.
Lapo

--

gotoAndPlay()

...addicted to flash games
Jochanan
Posts: 79
Joined: 11 May 2018, 09:12

Re: Can i add multiple event handlers for the same event?

Postby Jochanan » 02 Oct 2019, 15:18

Lapo wrote:Use this:

Code: Select all

addEventListener(SFSEventType.USER_DISCONNECT, new OnUserDisconnect());
addEventListener(SFSEventType.USER_DISCONNECT, new OnRoomLeave());

Sorry, this is not part of the documented/official API.


I have tried it i still, OnUserDisconnect handler have not been called. I have not add removeEventListener though.

May i ask you for a code snippet you used to test it out? Thanks
User avatar
Lapo
Site Admin
Posts: 23025
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Can i add multiple event handlers for the same event?

Postby Lapo » 02 Oct 2019, 16:56

Here you go:

Code: Select all

public class MultiEventExtension extends SFSExtension
{
   private static final class EvtHandler implements ISFSEventListener
   {
      @Override
      public void handleServerEvent(ISFSEvent event) throws Exception
      {
         System.out.println("---> Event: " + event);
      }
   }
   
   private EvtHandler evh1 = new EvtHandler();
   private EvtHandler evh2 = new EvtHandler();
   
   @Override
   public void init()
   {
      addEventListener(SFSEventType.SERVER_READY, evh1);
      addEventListener(SFSEventType.SERVER_READY, evh2);
   }
}
Lapo

--

gotoAndPlay()

...addicted to flash games
Jochanan
Posts: 79
Joined: 11 May 2018, 09:12

Re: Can i add multiple event handlers for the same event?

Postby Jochanan » 03 Oct 2019, 14:19

I have overlooked, that the function is named differently.

What is the difference between addEventListener and addEventHandler and where we should use one or the other?

Only difference i have noticed so far is, that if we derivate from BaseServerEventHandler (which implements IServerEventHandler), than there are logging functions included. Plus the fact, that only last registered function/class in addEventHandler is called
User avatar
Lapo
Site Admin
Posts: 23025
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Can i add multiple event handlers for the same event?

Postby Lapo » 03 Oct 2019, 15:03

Jochanan wrote:I have overlooked, that the function is named differently.

What is the difference between addEventListener and addEventHandler and where we should use one or the other?

As i mentioned we've only documented the addEventHandler() method in our docs.
It's easier to use because you don't have to manually remove event handlers but it doesn't support multiple handlers, which is usually not a problem.

In your case you can use the addEventListener() method which is documented but can be used, if necessary. It's up to you... you might also not use it and simply dispatch your multiple events internally (via method calls). All this depends a lot on the specific requirements.
Only difference i have noticed so far is, that if we derivate from BaseServerEventHandler (which implements IServerEventHandler), than there are logging functions included.

Yes you're also missing those features. In alternative you can pass a reference of your Extension's logger to the listener.
Example:

Code: Select all

private EvtHandler evh1 = new EvtHandler(this.getLogger());

Then use the logger in your listener:

Code: Select all

logger.warn("Hello world");


Hope it helps
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X Questions”

Who is online

Users browsing this forum: Baidu [Spider], DonaldVed and 48 guests