use of org.projectodd.sockjs.SockJsServer in project AngularBeans by bessemHmidi.
the class SockJsServlet method configuratorFor.
private ServerEndpointConfig.Configurator configuratorFor(final String prefix, final boolean isRaw) {
return new ServerEndpointConfig.Configurator() {
@Override
public <T> T getEndpointInstance(Class<T> endpointClass) throws InstantiationException {
try {
return endpointClass.getConstructor(SockJsServer.class, String.class, String.class).newInstance(sockJsServer, getServletContext().getContextPath(), prefix);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
if (isRaw) {
// headers with for raw websocket requests
return;
}
String path = request.getRequestURI().getPath();
Matcher matcher = SESSION_PATTERN.matcher(path);
if (matcher.matches()) {
String sessionId = matcher.group(1);
saveHeaders(sessionId, request.getHeaders());
}
}
};
}
use of org.projectodd.sockjs.SockJsServer in project AngularBeans by bessemHmidi.
the class AngularBeansServletContextListener method initJSR356.
public void initJSR356() throws ServletException {
sockJsServer = new SockJsServer();
sockJsServer.init();
if (sockJsServer.options.websocket) {
// Make sure we listen on all possible mappings of the servlet
// for (String mapping :
// getServletContext().getServletRegistration(context.getServletName()).getMappings())
// {
final String commonPrefix = extractPrefixFromMapping("/rt-service/*");
//
String websocketPath = commonPrefix + "/{server}/{session}/websocket";
ServerEndpointConfig sockJsConfig = ServerEndpointConfig.Builder.create(SockJsEndpoint.class, websocketPath).configurator(configuratorFor(commonPrefix, false)).build();
// rt-service/websocket
String rawWebsocketPath = commonPrefix + "/websocket";
ServerEndpointConfig rawWsConfig = ServerEndpointConfig.Builder.create(RawWebsocketEndpoint.class, rawWebsocketPath).configurator(configuratorFor(commonPrefix, true)).build();
ServerContainer serverContainer = (ServerContainer) context.getAttribute("javax.websocket.server.ServerContainer");
try {
serverContainer.addEndpoint(sockJsConfig);
serverContainer.addEndpoint(rawWsConfig);
Logger.getLogger(this.getClass().getSimpleName()).info("deployement of programmatic Web socket EndPoint :" + rawWebsocketPath);
} catch (DeploymentException ex) {
throw new ServletException("Error deploying websocket endpoint:", ex);
}
}
}
use of org.projectodd.sockjs.SockJsServer in project AngularBeans by bessemHmidi.
the class AngularBeansServletContextListener method configuratorFor.
private ServerEndpointConfig.Configurator configuratorFor(final String prefix, final boolean isRaw) {
return new ServerEndpointConfig.Configurator() {
@Override
public <T> T getEndpointInstance(Class<T> endpointClass) throws InstantiationException {
try {
return endpointClass.getConstructor(SockJsServer.class, String.class, String.class).newInstance(sockJsServer, context.getContextPath(), prefix);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
if (isRaw) {
// headers with for raw websocket requests
return;
}
String path = request.getRequestURI().getPath();
Matcher matcher = SESSION_PATTERN.matcher(path);
if (matcher.matches()) {
String sessionId = matcher.group(1);
saveHeaders(sessionId, request.getHeaders());
}
}
};
}
use of org.projectodd.sockjs.SockJsServer in project AngularBeans by bessemHmidi.
the class RealTimeEndPoint method init.
// @OnClose
// public void onclose(Session session) {
// sessionCloseEvent.fire(new WSocketEvent(session, null));
// Logger.getLogger("AngularBeans").info("ws-channel closed");
// }
//
// @OnError
// public void onError(Session session, Throwable error) {
// // errorEvent.fire(new WSocketEvent(session,
// // Util.parse(Util.getJson(error))));
// error.printStackTrace();
// }
//
// @PostConstruct
// public void start() {
// try {
// super.inito();
// } catch (ServletException e) {
// e.printStackTrace();
// }
//
// }
@Override
public void init() throws ServletException {
SockJsServer server = AngularBeansServletContextListener.sockJsServer;
// Various options can be set on the server, such as:
// echoServer.options.responseLimit = 4 * 1024;
// server.options.
// onConnection is the main entry point for handling SockJS connections
server.onConnection(new SockJsServer.OnConnectionHandler() {
@Override
public void handle(final SockJsConnection connection) {
// logger.info("session opened");
// onData gets called when a client sends data to the server
connection.onData(new SockJsConnection.OnDataHandler() {
@Override
public void handle(String message) {
JsonObject jObj = CommonUtils.parse(message).getAsJsonObject();
String UID = null;
if (jObj.get("session") == null) {
UID = SessionMapper.getHTTPSessionID(connection.id);
} else {
UID = jObj.get("session").getAsString();
SessionMapper.getSessionsMap().put(UID, new HashSet<String>());
}
SessionMapper.getSessionsMap().get(UID).add(connection.id);
RealTimeDataReceivedEvent ev = new RealTimeDataReceivedEvent(connection, jObj);
ev.setConnection(connection);
ev.setSessionId(UID);
NGSessionScopeContext.setCurrentContext(UID);
String service = jObj.get("service").getAsString();
if (service.equals("ping")) {
sessionOpenEvent.fire(ev);
logger.info("AngularBeans-client: " + UID);
} else {
receiveEvents.fire(ev);
}
// connection.write(message);
}
});
// onClose gets called when a client disconnects
connection.onClose(new SockJsConnection.OnCloseHandler() {
@Override
public void handle() {
getServletContext().log("Realtime client disconnected..");
}
});
}
});
server.options.websocket = true;
setServer(server);
// Don't forget to call super.init() to wire everything up
super.init();
}
Aggregations