
com.github.gkutiel.flip.web.FlipSocket Maven / Gradle / Ivy
package com.github.gkutiel.flip.web;
import java.io.IOException;
import java.util.logging.Logger;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import org.eclipse.jetty.websocket.WebSocket.Connection;
import com.github.gkutiel.flip.utils.Utils;
public class FlipSocket {
static class Event {
final String type;
final Object data;
public Event(final String type, final Object data) {
this.type = type;
this.data = data;
}
}
private static final Logger LOGGER = Logger.getLogger(FlipSocket.class.getCanonicalName());
private final Cookie[] cookies;
private final String info;
private Connection con;
FlipSocket(final HttpServletRequest req, final String info) {
cookies = req.getCookies();
this.info = info;
}
public void close() {
con.disconnect();
}
public final T get(final Key key) {
final String val = HttpUtils.get(cookies, key.name());
if (val == null) return null;
return HttpUtils.verify(val, key.valType());
}
public String getInfo() {
return info;
}
public void notify(final String eventType, final Object data) throws IOException {
final String event = Utils.Json.toString(new Event(eventType, data));
LOGGER.info("NOTIFYING: " + event);
con.sendMessage(event);
}
void setConnection(final Connection con) {
this.con = con;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy