engine.io-client-java
engine.io-client-java copied to clipboard
Add allEvent listener to emitter
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.
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.
You're right. As the js client does not implement the wildcard case, the java client has no intend to do that.
Hi, JS client has .onAny() now, maybe you'll reconsider adding something like that? 🙂