Skip to content

Commit

Permalink
Add support for event interceptors which catch all events
Browse files Browse the repository at this point in the history
  • Loading branch information
yosiat committed Sep 16, 2019
1 parent 00727fc commit f3621a9
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/main/java/com/corundumstudio/socketio/SocketIOServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,13 @@ public <T> void addEventListener(String eventName, Class<T> eventClass, DataList
mainNamespace.addEventListener(eventName, eventClass, listener);
}

@Override
public void addEventInterceptor(EventInterceptor eventInterceptor) {
mainNamespace.addEventInterceptor(eventInterceptor);

}


@Override
public void removeAllListeners(String eventName) {
mainNamespace.removeAllListeners(eventName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@
*/
package com.corundumstudio.socketio.listener;


public interface ClientListeners {

void addMultiTypeEventListener(String eventName, MultiTypeEventListener listener, Class<?> ... eventClass);

<T> void addEventListener(String eventName, Class<T> eventClass, DataListener<T> listener);

void addEventInterceptor(EventInterceptor eventInterceptor);

void addDisconnectListener(DisconnectListener listener);

void addConnectListener(ConnectListener listener);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.corundumstudio.socketio.listener;

import com.corundumstudio.socketio.AckRequest;
import com.corundumstudio.socketio.transport.NamespaceClient;
import java.util.List;

public interface EventInterceptor {
void onEvent(NamespaceClient client, String eventName, List<Object> args, AckRequest ackRequest);
}
10 changes: 10 additions & 0 deletions src/main/java/com/corundumstudio/socketio/namespace/Namespace.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public class Namespace implements SocketIONamespace {
private final Queue<ConnectListener> connectListeners = new ConcurrentLinkedQueue<ConnectListener>();
private final Queue<DisconnectListener> disconnectListeners = new ConcurrentLinkedQueue<DisconnectListener>();
private final Queue<PingListener> pingListeners = new ConcurrentLinkedQueue<PingListener>();
private final Queue<EventInterceptor> eventInterceptors = new ConcurrentLinkedQueue<EventInterceptor>();

private final Map<UUID, SocketIOClient> allClients = PlatformDependent.newConcurrentHashMap();
private final ConcurrentMap<String, Set<UUID>> roomClients = PlatformDependent.newConcurrentHashMap();
Expand Down Expand Up @@ -126,6 +127,11 @@ public <T> void addEventListener(String eventName, Class<T> eventClass, DataList
jsonSupport.addEventMapping(name, eventName, eventClass);
}

@Override
public void addEventInterceptor(EventInterceptor eventInterceptor) {
eventInterceptors.add(eventInterceptor);
}

@SuppressWarnings({"rawtypes", "unchecked"})
public void onEvent(NamespaceClient client, String eventName, List<Object> args, AckRequest ackRequest) {
EventEntry entry = eventListeners.get(eventName);
Expand All @@ -139,6 +145,10 @@ public void onEvent(NamespaceClient client, String eventName, List<Object> args,
Object data = getEventData(args, dataListener);
dataListener.onData(client, data, ackRequest);
}

for (EventInterceptor eventInterceptor : eventInterceptors) {
eventInterceptor.onEvent(client, eventName, args, ackRequest);
}
} catch (Exception e) {
exceptionListener.onEventException(e, args, client);
if (ackMode == AckMode.AUTO_SUCCESS_ONLY) {
Expand Down

0 comments on commit f3621a9

Please sign in to comment.