engine.io-client-java icon indicating copy to clipboard operation
engine.io-client-java copied to clipboard

Add allEvent listener to emitter

Open tdhman opened this issue 6 years ago • 3 comments

Hello,

I'm using socket.io java client for my android application. My application (1) is a wrapper of another application (2) that listens to socket.io events. From the view of my wrapper application, I don't know the list of specific events that the application (2) listens to. However, I need to access to the returned params of the registered event handlers of the application (2). Hence I wonder if we can add a special event, e.g. *, that once it's registered by Emitter.on("*", callback), will transfer the response of all event to the * event callback ?

Regarding to the source code, I found out that it's easy to do so by just adding some line of code in the Emitter:

public Emitter emit(String event, Object... args) {
        ConcurrentLinkedQueue<Listener> callbacks = this.callbacks.get(event);
        if (callbacks != null) {
            for (Listener fn : callbacks) {
                fn.call(args);
            }
        }
        // Emit to all event handlers if it is registered
        ConcurrentLinkedQueue<Listener> allEventCallbacks = this.callbacks.get("*");
        if (allEventCallbacks != null) {
            for (Listener fn : allEventCallbacks) {
                fn.call(event, args);
            }
        }
        return this;
    }

What do you think if it's possible to do that ?

Thanks for any help.

tdhman avatar Jul 30 '18 13:07 tdhman

It seems the position was to mimic the JS client, which does not handle wildcards: https://github.com/socketio/socket.io-client-java/issues/243#issuecomment-155670974. I'm not sure whether it has changed since then.

darrachequesne avatar Jul 31 '18 21:07 darrachequesne

You're right. As the js client does not implement the wildcard case, the java client has no intend to do that.

tdhman avatar Aug 06 '18 07:08 tdhman

Hi, JS client has .onAny() now, maybe you'll reconsider adding something like that? 🙂

pauljackals avatar Apr 18 '22 21:04 pauljackals