Search in sources :

Example 1 with SockJsConnection

use of org.projectodd.sockjs.SockJsConnection in project AngularBeans by bessemHmidi.

the class RealTimeClient method publish.

private void publish(Map<String, Object> paramsToSend) {
    for (SockJsConnection session : new HashSet<SockJsConnection>(sessions)) {
        if (!session.getReadyState().equals(OPEN)) {
            sessions.remove(session);
        } else {
            session.write(util.getJson(paramsToSend), async);
            async = false;
        }
    }
}
Also used : SockJsConnection(org.projectodd.sockjs.SockJsConnection) HashSet(java.util.HashSet)

Example 2 with SockJsConnection

use of org.projectodd.sockjs.SockJsConnection 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)

Example 3 with SockJsConnection

use of org.projectodd.sockjs.SockJsConnection in project AngularBeans by bessemHmidi.

the class GlobalConnectionHolder method removeConnection.

public void removeConnection(String id) {
    synchronized (lock) {
        for (SockJsConnection connection : allConnections) {
            String httpSessionId = SessionMapper.getHTTPSessionID(connection.id);
            if (httpSessionId != null && httpSessionId.equals(id)) {
                SessionMapper.getSessionsMap().remove(id);
                try {
                    connection.destroy();
                } catch (Exception ex) {
                    Logger.getLogger(GlobalConnectionHolder.class.getName()).log(Level.WARNING, "error closing connection", ex);
                }
                allConnections.remove(connection);
            }
        }
    }
}
Also used : SockJsConnection(org.projectodd.sockjs.SockJsConnection)

Example 4 with SockJsConnection

use of org.projectodd.sockjs.SockJsConnection in project AngularBeans by bessemHmidi.

the class RealTimeClient method broadcast.

private void broadcast(String channel, boolean withoutMe, Map<String, Object> paramsToSend) {
    for (SockJsConnection connection : connectionHolder.getAllConnections()) {
        if (!broadcastManager.isSubscribed(connection.id, channel)) {
            continue;
        }
        if (withoutMe) {
            if (sessions.contains(connection)) {
                continue;
            }
        }
        if (connection.getReadyState().equals(OPEN)) {
            String objectMessage = util.getJson(paramsToSend);
            connection.write(objectMessage, async);
            async = false;
        }
    }
}
Also used : SockJsConnection(org.projectodd.sockjs.SockJsConnection)

Aggregations

SockJsConnection (org.projectodd.sockjs.SockJsConnection)4 JsonObject (com.google.gson.JsonObject)1 HashSet (java.util.HashSet)1 SockJsServer (org.projectodd.sockjs.SockJsServer)1