io.femo.ws.lib.WebSocketLibraryHandler Maven / Gradle / Ivy
package io.femo.ws.lib;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import io.femo.http.HttpHandler;
import io.femo.support.jdk7.Supplier;
import io.femo.ws.Constants;
import io.femo.ws.WebSocketConnection;
import io.femo.ws.WebSocketEventHandler;
import io.femo.ws.WebSocketHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by felix on 6/8/16.
*/
public class WebSocketLibraryHandler implements WebSocketEventHandler, Supplier {
private static final Logger LOGGER = LoggerFactory.getLogger("HTTP-WS");
private JsonParser jsonParser;
private Map> handlers;
private WebSocketHandler parent;
private List onConnectHandlers;
public WebSocketLibraryHandler(WebSocketHandler parent) {
this.parent = parent;
this.parent.handler(this).connect(this);
this.handlers = new HashMap<>();
this.onConnectHandlers = new ArrayList<>();
}
@Override
public void handleMessage(Constants.WEBSOCKET.FRAME.DataType dataType, byte[] data, WebSocketConnection webSocketConnection) {
if(dataType == Constants.WEBSOCKET.FRAME.DataType.TEXT) {
if (jsonParser == null) {
jsonParser = new JsonParser();
}
String _json = new String(data);
JsonElement jsonElement = jsonParser.parse(_json);
if (jsonElement.isJsonObject()) {
JsonObject sdata = jsonElement.getAsJsonObject();
if (sdata.has("type") && sdata.get("type").isJsonPrimitive()) {
String type = sdata.get("type").getAsString();
if (handlers.containsKey(type)) {
List handlers = this.handlers.get(type);
for (Handler h : handlers) {
h.handle(sdata.get("data"), new WebSocketLibraryConnection(webSocketConnection));
}
}
} else {
LOGGER.warn("Received invalid frame on connection " + webSocketConnection.getName());
LOGGER.warn("Data");
LOGGER.warn(new String(data));
}
} else {
LOGGER.warn("Received invalid frame on connection " + webSocketConnection.getName());
LOGGER.warn("Data");
LOGGER.warn(new String(data));
}
} else if (dataType == Constants.WEBSOCKET.FRAME.DataType.CONNECT) {
for(Handler h : onConnectHandlers) {
try {
h.handle(null, new WebSocketLibraryConnection(webSocketConnection));
} catch (Throwable t) {
LOGGER.error("Error while notifying handler of new connection", t);
}
}
} else if (dataType != Constants.WEBSOCKET.FRAME.DataType.CLOSE) {
LOGGER.warn("Received invalid frame on connection " + webSocketConnection.getName());
LOGGER.warn("Type: " + dataType);
LOGGER.warn("Data");
LOGGER.warn(new String(data));
}
}
public HttpHandler get() {
return parent;
}
public interface Handler {
void handle(JsonElement data, WebSocketLibraryConnection con);
}
public WebSocketLibraryHandler on(String type, Handler handler) {
if(!handlers.containsKey(type)) {
handlers.put(type, new ArrayList());
}
handlers.get(type).add(handler);
return this;
}
public WebSocketLibraryHandler connect(Handler handler) {
this.onConnectHandlers.add(handler);
return this;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy