Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/main/java/com/corundumstudio/socketio/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,20 @@
package com.corundumstudio.socketio;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.net.ssl.KeyManagerFactory;

import com.corundumstudio.socketio.handler.SuccessAuthorizationListener;
import com.corundumstudio.socketio.listener.DefaultExceptionListener;
import com.corundumstudio.socketio.listener.EventInterceptor;
import com.corundumstudio.socketio.listener.ExceptionListener;
import com.corundumstudio.socketio.protocol.JsonSupport;
import com.corundumstudio.socketio.store.MemoryStoreFactory;
import com.corundumstudio.socketio.store.StoreFactory;

import javax.net.ssl.KeyManagerFactory;

public class Configuration {

private ExceptionListener exceptionListener = new DefaultExceptionListener();
Expand Down Expand Up @@ -85,6 +87,8 @@ public class Configuration {
private boolean httpCompression = true;

private boolean websocketCompression = true;

private List<EventInterceptor> eventInterceptors = new ArrayList<EventInterceptor>();

public Configuration() {
}
Expand Down Expand Up @@ -151,6 +155,7 @@ public Configuration() {

setHttpCompression(conf.isHttpCompression());
setWebsocketCompression(conf.isWebsocketCompression());
setEventInterceptors(conf.getEventInterceptors());
}

public JsonSupport getJsonSupport() {
Expand Down Expand Up @@ -573,5 +578,20 @@ public void setWebsocketCompression(boolean websocketCompression) {
public boolean isWebsocketCompression() {
return websocketCompression;
}

/**
* Add interceptor will run when onEvent.
* @param eventInterceptor
*/
public void addEventInterceptor(EventInterceptor eventInterceptor){
this.eventInterceptors.add(eventInterceptor);
}
public List<EventInterceptor> getEventInterceptors(){
List<EventInterceptor> interceptors = new ArrayList<EventInterceptor>(eventInterceptors);
return interceptors;
}
private void setEventInterceptors(List<EventInterceptor> eventInterceptors){
this.eventInterceptors = eventInterceptors;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.corundumstudio.socketio.listener;

import java.util.List;

import com.corundumstudio.socketio.AckRequest;
import com.corundumstudio.socketio.transport.NamespaceClient;

public interface EventInterceptor {

void onEvent(NamespaceClient client, String eventName, List<Object> args, AckRequest ackRequest);

}
13 changes: 13 additions & 0 deletions src/main/java/com/corundumstudio/socketio/namespace/Namespace.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.corundumstudio.socketio.listener.ConnectListener;
import com.corundumstudio.socketio.listener.DataListener;
import com.corundumstudio.socketio.listener.DisconnectListener;
import com.corundumstudio.socketio.listener.EventInterceptor;
import com.corundumstudio.socketio.listener.ExceptionListener;
import com.corundumstudio.socketio.listener.MultiTypeEventListener;
import com.corundumstudio.socketio.protocol.JsonSupport;
Expand Down Expand Up @@ -72,6 +73,7 @@ public class Namespace implements SocketIONamespace {
private final JsonSupport jsonSupport;
private final StoreFactory storeFactory;
private final ExceptionListener exceptionListener;
private final List<EventInterceptor> eventInterceptors;

public Namespace(String name, Configuration configuration) {
super();
Expand All @@ -80,6 +82,7 @@ public Namespace(String name, Configuration configuration) {
this.storeFactory = configuration.getStoreFactory();
this.exceptionListener = configuration.getExceptionListener();
this.ackMode = configuration.getAckMode();
this.eventInterceptors = configuration.getEventInterceptors();
}

public void addClient(SocketIOClient client) {
Expand Down Expand Up @@ -137,6 +140,7 @@ public void onEvent(NamespaceClient client, String eventName, List<Object> args,
}

try {
runEventInterceptors(client, eventName, args, ackRequest);
Queue<DataListener> listeners = entry.getListeners();
for (DataListener dataListener : listeners) {
Object data = getEventData(args, dataListener);
Expand Down Expand Up @@ -360,4 +364,13 @@ public SocketIOClient getClient(UUID uuid) {
return allClients.get(uuid);
}

private void runEventInterceptors(NamespaceClient client, String eventName, List<Object> args, AckRequest ackRequest){
for(EventInterceptor interceptor:eventInterceptors){
try{
interceptor.onEvent(client, eventName, args, ackRequest);
}catch(Exception e){
e.printStackTrace();
}
}
}
}