Search in sources :

Example 1 with SockJsServer

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());
            }
        }
    };
}
Also used : HandshakeResponse(javax.websocket.HandshakeResponse) ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) Matcher(java.util.regex.Matcher) SockJsServer(org.projectodd.sockjs.SockJsServer) SockJsException(org.projectodd.sockjs.SockJsException) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) HandshakeRequest(javax.websocket.server.HandshakeRequest)

Example 2 with SockJsServer

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);
        }
    }
}
Also used : ServletException(javax.servlet.ServletException) ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) DeploymentException(javax.websocket.DeploymentException) SockJsServer(org.projectodd.sockjs.SockJsServer) ServerContainer(javax.websocket.server.ServerContainer)

Example 3 with SockJsServer

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());
            }
        }
    };
}
Also used : HandshakeResponse(javax.websocket.HandshakeResponse) ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) Matcher(java.util.regex.Matcher) SockJsServer(org.projectodd.sockjs.SockJsServer) ServletException(javax.servlet.ServletException) DeploymentException(javax.websocket.DeploymentException) HandshakeRequest(javax.websocket.server.HandshakeRequest)

Example 4 with SockJsServer

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();
}
Also used : SockJsConnection(org.projectodd.sockjs.SockJsConnection) JsonObject(com.google.gson.JsonObject) SockJsServer(org.projectodd.sockjs.SockJsServer)

Aggregations

SockJsServer (org.projectodd.sockjs.SockJsServer)4 ServletException (javax.servlet.ServletException)3 ServerEndpointConfig (javax.websocket.server.ServerEndpointConfig)3 Matcher (java.util.regex.Matcher)2 DeploymentException (javax.websocket.DeploymentException)2 HandshakeResponse (javax.websocket.HandshakeResponse)2 HandshakeRequest (javax.websocket.server.HandshakeRequest)2 JsonObject (com.google.gson.JsonObject)1 IOException (java.io.IOException)1 ServerContainer (javax.websocket.server.ServerContainer)1 SockJsConnection (org.projectodd.sockjs.SockJsConnection)1 SockJsException (org.projectodd.sockjs.SockJsException)1