Example usage for javax.websocket Session isOpen

List of usage examples for javax.websocket Session isOpen

Introduction

In this page you can find the example usage for javax.websocket Session isOpen.

Prototype

boolean isOpen();

Source Link

Usage

From source file:org.b3log.symphony.processor.channel.ArticleListChannel.java

/**
 * Notifies the specified article heat message to browsers.
 *
 * @param message the specified message, for example      <pre>
 * {/*ww  w  .j  a va 2 s . c  o  m*/
 *     "articleId": "",
 *     "operation": "" // "+"/"-"
 * }
 * </pre>
 */
public static void notifyHeat(final JSONObject message) {
    final String articleId = message.optString(Article.ARTICLE_T_ID);
    final String msgStr = message.toString();

    synchronized (SESSIONS) {
        for (final Map.Entry<Session, String> entry : SESSIONS.entrySet()) {
            final Session session = entry.getKey();
            final String articleIds = entry.getValue();

            if (!StringUtils.contains(articleIds, articleId)) {
                continue;
            }

            if (session.isOpen()) {
                session.getAsyncRemote().sendText(msgStr);
            }
        }
    }
}

From source file:org.b3log.symphony.processor.channel.ChatRoomChannel.java

/**
 * Called when the socket connection with the browser is established.
 *
 * @param session session// w w  w  .  ja v  a 2 s . c o m
 */
@OnOpen
public void onConnect(final Session session) {
    SESSIONS.add(session);

    synchronized (SESSIONS) {
        final Iterator<Session> i = SESSIONS.iterator();
        while (i.hasNext()) {
            final Session s = i.next();

            if (s.isOpen()) {
                final String msgStr = new JSONObject().put(Common.ONLINE_CHAT_CNT, SESSIONS.size())
                        .put(Common.TYPE, "online").toString();
                s.getAsyncRemote().sendText(msgStr);
            }
        }
    }
}

From source file:org.b3log.symphony.processor.channel.ChatRoomChannel.java

/**
 * Notifies the specified chat message to browsers.
 *
 * @param message the specified message, for example      <pre>
 * {/* w  ww  . j  a  v a2 s.  c om*/
 *     "userName": "",
 *     "content": ""
 * }
 * </pre>
 */
public static void notifyChat(final JSONObject message) {
    message.put(Common.TYPE, "msg");
    final String msgStr = message.toString();

    synchronized (SESSIONS) {
        final Iterator<Session> i = SESSIONS.iterator();
        while (i.hasNext()) {
            final Session session = i.next();

            if (session.isOpen()) {
                session.getAsyncRemote().sendText(msgStr);
            }
        }
    }
}

From source file:org.b3log.symphony.processor.channel.ChatRoomChannel.java

/**
 * Removes the specified session.//from   ww  w  .  ja v a2 s  . com
 *
 * @param session the specified session
 */
private void removeSession(final Session session) {
    SESSIONS.remove(session);

    synchronized (SESSIONS) {
        final Iterator<Session> i = SESSIONS.iterator();
        while (i.hasNext()) {
            final Session s = i.next();

            if (s.isOpen()) {
                final String msgStr = new JSONObject().put(Common.ONLINE_CHAT_CNT, SESSIONS.size())
                        .put(Common.TYPE, "online").toString();
                s.getAsyncRemote().sendText(msgStr);
            }
        }
    }
}

From source file:org.b3log.symphony.processor.channel.TimelineChannel.java

/**
 * Notifies the specified comment message to browsers.
 *
 * @param message the specified message, for example      <pre>
 * {/*from  ww w  .j a  v  a2s . c o m*/
 *     "commentContent": ""
 * }
 * </pre>
 */
public static void notifyTimeline(final JSONObject message) {
    final String msgStr = message.toString();

    synchronized (SESSIONS) {
        for (final Session session : SESSIONS) {
            if (session.isOpen()) {
                session.getAsyncRemote().sendText(msgStr);
            }
        }
    }
}

From source file:org.wso2.carbon.device.mgt.extensions.remote.session.endpoint.SubscriptionEndpoint.java

/**
 * Web socket onError use to handle  socket connection error
 *
 * @param session    - Registered  session.
 * @param throwable  - Web socket exception
 * @param deviceType - DeviceType/*from w  ww .  java  2 s . c  om*/
 * @param deviceId   - Device Identifier
 */
public void onError(Session session, Throwable throwable, @PathParam("deviceType") String deviceType,
        @PathParam("deviceId") String deviceId) {

    if (throwable instanceof IOException) {
        if (log.isDebugEnabled()) {
            log.error("Error occurred in session ID: " + session.getId() + " device type: " + deviceType
                    + "device id: " + deviceId + ", for request URI - " + session.getRequestURI() + ", "
                    + throwable.getMessage(), throwable);
        }
    } else {
        log.error("Error occurred in session ID: " + session.getId() + " device type: " + deviceType
                + " device " + "id: " + deviceId + ", for request URI - " + session.getRequestURI() + ", "
                + throwable.getMessage(), throwable);
    }
    try {
        ServiceHolder.getInstance().getRemoteSessionManagementService().endSession(session,
                "Remote session closed");
        if (session.isOpen()) {
            session.close(new CloseReason(CloseReason.CloseCodes.PROTOCOL_ERROR, "Unexpected Error Occurred"));
        }
    } catch (IOException ex) {
        if (log.isDebugEnabled()) {
            log.error("Failed to disconnect the client.", ex);
        }
    }
}

From source file:org.wso2.carbon.device.mgt.extensions.remote.session.RemoteSessionManagementServiceImpl.java

/**
 * Closing the session and cleanup the resources
 *
 * @param session Web socket Remote Session
 *//*from www. j a  v  a  2 s.c  o  m*/
@Override
public void endSession(Session session, String closeReason) {

    RemoteSession remoteSession = RemoteSessionManagementDataHolder.getInstance().getSessionMap()
            .remove(session.getId());
    if (remoteSession != null) {
        //String operationId = remoteSession.getOperationId();
        String deviceKey = remoteSession.getTenantDomain() + "/" + remoteSession.getDeviceType() + "/"
                + remoteSession.getDeviceId();
        RemoteSession lastSession = RemoteSessionManagementDataHolder.getInstance()
                .getActiveDeviceClientSessionMap().get(deviceKey);
        if (lastSession != null && lastSession.getMySession().getId().equals(session.getId())) {
            RemoteSessionManagementDataHolder.getInstance().getActiveDeviceClientSessionMap().remove(deviceKey);
        }
        if (remoteSession.getPeerSession() != null) {
            Session peerSession = remoteSession.getPeerSession().getMySession();
            if (peerSession != null) {
                RemoteSessionManagementDataHolder.getInstance().getSessionMap().remove(peerSession.getId());
                if (lastSession != null && lastSession.getMySession().getId().equals(peerSession.getId())) {
                    RemoteSessionManagementDataHolder.getInstance().getActiveDeviceClientSessionMap()
                            .remove(deviceKey);
                }
                if (peerSession.isOpen()) {
                    try {
                        peerSession.close(new CloseReason(CloseReason.CloseCodes.GOING_AWAY, closeReason));
                    } catch (IOException ex) {
                        if (log.isDebugEnabled()) {
                            log.error("Failed to disconnect the client.", ex);
                        }
                    }
                }
            }
        }
    }
}