java类javax.websocket.CloseReason的实例源码

WsRemoteEndpointImplBase.java 文件源码 项目:lazycat 阅读 25 收藏 0 点赞 0 评论 0
private void handleSendFailureWithEncode(Throwable t) throws IOException, EncodeException {
    // First, unwrap any execution exception
    if (t instanceof ExecutionException) {
        t = t.getCause();
    }

    // Close the session
    wsSession.doClose(new CloseReason(CloseCodes.GOING_AWAY, t.getMessage()),
            new CloseReason(CloseCodes.CLOSED_ABNORMALLY, t.getMessage()));

    // Rethrow the exception
    if (t instanceof EncodeException) {
        throw (EncodeException) t;
    }
    if (t instanceof IOException) {
        throw (IOException) t;
    }
    throw new IOException(t);
}
WsSession.java 文件源码 项目:tomcat7 阅读 32 收藏 0 点赞 0 评论 0
/**
 * Called when a close message is received. Should only ever happen once.
 * Also called after a protocol error when the ProtocolHandler needs to
 * force the closing of the connection.
 */
public void onClose(CloseReason closeReason) {

    synchronized (stateLock) {
        if (state != State.CLOSED) {
            try {
                wsRemoteEndpoint.setBatchingAllowed(false);
            } catch (IOException e) {
                log.warn(sm.getString("wsSession.flushFailOnClose"), e);
                fireEndpointOnError(e);
            }
            if (state == State.OPEN) {
                state = State.OUTPUT_CLOSED;
                sendCloseMessage(closeReason);
                fireEndpointOnClose(closeReason);
            }
            state = State.CLOSED;

            // Close the socket
            wsRemoteEndpoint.close();
        }
    }
}
WsFrameBase.java 文件源码 项目:tomcat7 阅读 22 收藏 0 点赞 0 评论 0
@SuppressWarnings("unchecked")
private void sendMessageBinary(ByteBuffer msg, boolean last)
        throws WsIOException {
    if (binaryMsgHandler instanceof WrappedMessageHandler) {
        long maxMessageSize =
                ((WrappedMessageHandler) binaryMsgHandler).getMaxMessageSize();
        if (maxMessageSize > -1 && msg.remaining() > maxMessageSize) {
            throw new WsIOException(new CloseReason(CloseCodes.TOO_BIG,
                    sm.getString("wsFrame.messageTooBig",
                            Long.valueOf(msg.remaining()),
                            Long.valueOf(maxMessageSize))));
        }
    }
    try {
        if (binaryMsgHandler instanceof MessageHandler.Partial<?>) {
            ((MessageHandler.Partial<ByteBuffer>) binaryMsgHandler).onMessage(msg, last);
        } else {
            // Caller ensures last == true if this branch is used
            ((MessageHandler.Whole<ByteBuffer>) binaryMsgHandler).onMessage(msg);
        }
    } catch(Throwable t) {
        handleThrowableOnSend(t);
    }
}
PojoEndpointBase.java 文件源码 项目:tomcat7 阅读 44 收藏 0 点赞 0 评论 0
@Override
public final void onClose(Session session, CloseReason closeReason) {

    if (methodMapping.getOnClose() != null) {
        try {
            methodMapping.getOnClose().invoke(pojo,
                    methodMapping.getOnCloseArgs(pathParameters, session, closeReason));
        } catch (Throwable t) {
            ExceptionUtils.handleThrowable(t);
            log.error(sm.getString("pojoEndpointBase.onCloseFail",
                    pojo.getClass().getName()), t);
        }
    }

    // Trigger the destroy method for any associated decoders
    Set<MessageHandler> messageHandlers = session.getMessageHandlers();
    for (MessageHandler messageHandler : messageHandlers) {
        if (messageHandler instanceof PojoMessageHandlerWholeBase<?>) {
            ((PojoMessageHandlerWholeBase<?>) messageHandler).onClose();
        }
    }
}
PojoEndpointBase.java 文件源码 项目:lazycat 阅读 26 收藏 0 点赞 0 评论 0
@Override
public final void onClose(Session session, CloseReason closeReason) {

    if (methodMapping.getOnClose() != null) {
        try {
            methodMapping.getOnClose().invoke(pojo,
                    methodMapping.getOnCloseArgs(pathParameters, session, closeReason));
        } catch (Throwable t) {
            ExceptionUtils.handleThrowable(t);
            log.error(sm.getString("pojoEndpointBase.onCloseFail", pojo.getClass().getName()), t);
        }
    }

    // Trigger the destroy method for any associated decoders
    Set<MessageHandler> messageHandlers = session.getMessageHandlers();
    for (MessageHandler messageHandler : messageHandlers) {
        if (messageHandler instanceof PojoMessageHandlerWholeBase<?>) {
            ((PojoMessageHandlerWholeBase<?>) messageHandler).onClose();
        }
    }
}
WsFrameBase.java 文件源码 项目:lazycat 阅读 21 收藏 0 点赞 0 评论 0
@SuppressWarnings("unchecked")
private void sendMessageText(boolean last) throws WsIOException {
    if (textMsgHandler instanceof WrappedMessageHandler) {
        long maxMessageSize = ((WrappedMessageHandler) textMsgHandler).getMaxMessageSize();
        if (maxMessageSize > -1 && messageBufferText.remaining() > maxMessageSize) {
            throw new WsIOException(new CloseReason(CloseCodes.TOO_BIG, sm.getString("wsFrame.messageTooBig",
                    Long.valueOf(messageBufferText.remaining()), Long.valueOf(maxMessageSize))));
        }
    }

    try {
        if (textMsgHandler instanceof MessageHandler.Partial<?>) {
            ((MessageHandler.Partial<String>) textMsgHandler).onMessage(messageBufferText.toString(), last);
        } else {
            // Caller ensures last == true if this branch is used
            ((MessageHandler.Whole<String>) textMsgHandler).onMessage(messageBufferText.toString());
        }
    } catch (Throwable t) {
        handleThrowableOnSend(t);
    } finally {
        messageBufferText.clear();
    }
}
WsWebSocketContainer.java 文件源码 项目:tomcat7 阅读 28 收藏 0 点赞 0 评论 0
/**
 * Cleans up the resources still in use by WebSocket sessions created from
 * this container. This includes closing sessions and cancelling
 * {@link Future}s associated with blocking read/writes.
 */
public void destroy() {
    CloseReason cr = new CloseReason(
            CloseCodes.GOING_AWAY, sm.getString("wsWebSocketContainer.shutdown"));

    for (WsSession session : sessions.keySet()) {
        try {
            session.close(cr);
        } catch (IOException ioe) {
            log.debug(sm.getString(
                    "wsWebSocketContainer.sessionCloseFail", session.getId()), ioe);
        }
    }

    // Only unregister with AsyncChannelGroupUtil if this instance
    // registered with it
    if (asynchronousChannelGroup != null) {
        synchronized (asynchronousChannelGroupLock) {
            if (asynchronousChannelGroup != null) {
                AsyncChannelGroupUtil.unregister();
                asynchronousChannelGroup = null;
            }
        }
    }
}
WebSocketClient.java 文件源码 项目:osc-core 阅读 31 收藏 0 点赞 0 评论 0
/**
 * @return
 *         Returns an object of Reconnect Handler
 *         Responsible for reconnecting to the server when connection loses
 *         Handle any networking issues as well
 */
private ReconnectHandler getReconnecHandler() {
    ClientManager.ReconnectHandler reconnectHandler = new ClientManager.ReconnectHandler() {
        @Override
        public boolean onDisconnect(CloseReason closeReason) {
            // If we close the session no need to reconnect
            if (WebSocketClient.this.isInterrupted || closeReason.getCloseCode() == CloseReason.CloseCodes.NORMAL_CLOSURE) {
                return false;
            }
            reconnect("Disconnected.... Trying to reconnect .... " + closeReason);
            return true;
        }

        @Override
        public boolean onConnectFailure(Exception exception) {
            if (!WebSocketClient.this.isInterrupted) {
                reconnect("Connection Failure... Attempting to reconnect ..." + exception.toString());
                return true;
            }
            return false;
        }
    };
    return reconnectHandler;
}
LearningWebsocketServer.java 文件源码 项目:lams 阅读 26 收藏 0 点赞 0 评论 0
/**
    * When user leaves the activity.
    */
   @OnClose
   public void unregisterUser(Session websocket, CloseReason reason) {
Long toolContentID = Long
    .valueOf(websocket.getRequestParameterMap().get(AttributeNames.PARAM_TOOL_CONTENT_ID).get(0));
websockets.get(toolContentID).remove(websocket);

if (log.isDebugEnabled()) {
    // If there was something wrong with the connection, put it into logs.
    log.debug("User " + websocket.getUserPrincipal().getName() + " left Dokumaran with Tool Content ID: "
        + toolContentID
        + (!(reason.getCloseCode().equals(CloseCodes.GOING_AWAY)
            || reason.getCloseCode().equals(CloseCodes.NORMAL_CLOSURE))
                ? ". Abnormal close. Code: " + reason.getCloseCode() + ". Reason: "
                    + reason.getReasonPhrase()
                : ""));
}
   }
LearningWebsocketServer.java 文件源码 项目:lams 阅读 33 收藏 0 点赞 0 评论 0
/**
    * When user leaves the activity.
    */
   @OnClose
   public void unregisterUser(Session websocket, CloseReason reason) {
Long toolSessionId = Long
    .valueOf(websocket.getRequestParameterMap().get(AttributeNames.PARAM_TOOL_SESSION_ID).get(0));
websockets.get(toolSessionId).remove(websocket);

if (log.isDebugEnabled()) {
    // If there was something wrong with the connection, put it into logs.
    log.debug("User " + websocket.getUserPrincipal().getName() + " left Leader Selection with Tool Session ID: "
        + toolSessionId
        + (!(reason.getCloseCode().equals(CloseCodes.GOING_AWAY)
            || reason.getCloseCode().equals(CloseCodes.NORMAL_CLOSURE))
                ? ". Abnormal close. Code: " + reason.getCloseCode() + ". Reason: "
                    + reason.getReasonPhrase()
                : ""));
}
   }
PresenceWebsocketServer.java 文件源码 项目:lams 阅读 23 收藏 0 点赞 0 评论 0
/**
    * If there was something wrong with the connection, put it into logs.
    */
   @OnClose
   public void unregisterUser(Session session, CloseReason reason) {
Long lessonId = Long.valueOf(session.getRequestParameterMap().get(AttributeNames.PARAM_LESSON_ID).get(0));
Set<Websocket> lessonWebsockets = PresenceWebsocketServer.websockets.get(lessonId);
Iterator<Websocket> websocketIterator = lessonWebsockets.iterator();
while (websocketIterator.hasNext()) {
    Websocket websocket = websocketIterator.next();
    if (websocket.session.equals(session)) {
    websocketIterator.remove();
    break;
    }
}

if (PresenceWebsocketServer.log.isDebugEnabled()) {
    PresenceWebsocketServer.log.debug(
        "User " + session.getUserPrincipal().getName() + " left Presence Chat with lessonId: " + lessonId
            + (!(reason.getCloseCode().equals(CloseCodes.GOING_AWAY)
                || reason.getCloseCode().equals(CloseCodes.NORMAL_CLOSURE))
                    ? ". Abnormal close. Code: " + reason.getCloseCode() + ". Reason: "
                        + reason.getReasonPhrase()
                    : ""));
}
   }
KumaliveWebsocketServer.java 文件源码 项目:lams 阅读 25 收藏 0 点赞 0 评论 0
@OnClose
   public void unregisterUser(Session websocket, CloseReason reason) throws JSONException, IOException {
String login = websocket.getUserPrincipal().getName();
if (login == null) {
    return;
}

Integer organisationId = Integer
    .valueOf(websocket.getRequestParameterMap().get(AttributeNames.PARAM_ORGANISATION_ID).get(0));
KumaliveDTO kumalive = kumalives.get(organisationId);
if (kumalive == null) {
    return;
}
KumaliveUser user = kumalive.learners.remove(login);
if (user != null) {
    Integer userId = user.userDTO.getUserID();
    if (kumalive.raisedHand != null) {
    kumalive.raisedHand.remove(userId);
    }
    if (userId.equals(kumalive.speaker)) {
    kumalive.speaker = null;
    }
}

sendRefresh(kumalive);
   }
CommandWebsocketServer.java 文件源码 项目:lams 阅读 21 收藏 0 点赞 0 评论 0
/**
    * Removes Learner websocket from the collection.
    */
   @OnClose
   public void unregisterUser(Session session, CloseReason reason) {
String login = session.getUserPrincipal().getName();
if (login == null) {
    return;
}

Long lessonId = Long.valueOf(session.getRequestParameterMap().get(AttributeNames.PARAM_LESSON_ID).get(0));
Map<String, Session> lessonWebsockets = CommandWebsocketServer.websockets.get(lessonId);
if (lessonWebsockets == null) {
    return;
}

lessonWebsockets.remove(login);
   }
LearningWebsocketServer.java 文件源码 项目:lams 阅读 27 收藏 0 点赞 0 评论 0
/**
    * When user leaves the activity.
    */
   @OnClose
   public void unregisterUser(Session session, CloseReason reason) {
Long toolSessionId = Long
    .valueOf(session.getRequestParameterMap().get(AttributeNames.PARAM_TOOL_SESSION_ID).get(0));
Set<Websocket> sessionWebsockets = LearningWebsocketServer.websockets.get(toolSessionId);
Iterator<Websocket> websocketIterator = sessionWebsockets.iterator();
while (websocketIterator.hasNext()) {
    Websocket websocket = websocketIterator.next();
    if (websocket.session.equals(session)) {
    websocketIterator.remove();
    break;
    }
}

if (LearningWebsocketServer.log.isDebugEnabled()) {
    LearningWebsocketServer.log.debug(
        "User " + session.getUserPrincipal().getName() + " left Chat with toolSessionId: " + toolSessionId
            + (!(reason.getCloseCode().equals(CloseCodes.GOING_AWAY)
                || reason.getCloseCode().equals(CloseCodes.NORMAL_CLOSURE))
                    ? ". Abnormal close. Code: " + reason.getCloseCode() + ". Reason: "
                        + reason.getReasonPhrase()
                    : ""));
}
   }
LearningWebsocketServer.java 文件源码 项目:lams 阅读 27 收藏 0 点赞 0 评论 0
/**
    * When user leaves the activity.
    */
   @OnClose
   public void unregisterUser(Session websocket, CloseReason reason) {
Long toolSessionId = Long
    .valueOf(websocket.getRequestParameterMap().get(AttributeNames.PARAM_TOOL_SESSION_ID).get(0));
LearningWebsocketServer.websockets.get(toolSessionId).remove(websocket);

if (LearningWebsocketServer.log.isDebugEnabled()) {
    // If there was something wrong with the connection, put it into logs.
    LearningWebsocketServer.log.debug("User " + websocket.getUserPrincipal().getName()
        + " left Scratchie with Tool Session ID: " + toolSessionId
        + (!(reason.getCloseCode().equals(CloseCodes.GOING_AWAY)
            || reason.getCloseCode().equals(CloseCodes.NORMAL_CLOSURE))
                ? ". Abnormal close. Code: " + reason.getCloseCode() + ". Reason: "
                    + reason.getReasonPhrase()
                : ""));
}
   }
LearningWebsocketServer.java 文件源码 项目:lams 阅读 33 收藏 0 点赞 0 评论 0
/**
    * When user leaves the activity.
    */
   @OnClose
   public void unregisterUser(Session websocket, CloseReason reason) {
Long toolSessionId = Long
    .valueOf(websocket.getRequestParameterMap().get(AttributeNames.PARAM_TOOL_SESSION_ID).get(0));
LearningWebsocketServer.websockets.get(toolSessionId).remove(websocket);

if (LearningWebsocketServer.log.isDebugEnabled()) {
    // If there was something wrong with the connection, put it into logs.
    LearningWebsocketServer.log.debug("User " + websocket.getUserPrincipal().getName()
        + " left Scribe with Tool Session ID: " + toolSessionId
        + (!(reason.getCloseCode().equals(CloseCodes.GOING_AWAY)
            || reason.getCloseCode().equals(CloseCodes.NORMAL_CLOSURE))
                ? ". Abnormal close. Code: " + reason.getCloseCode() + ". Reason: "
                    + reason.getReasonPhrase()
                : ""));
}
   }
JobScpAndProcessUtility.java 文件源码 项目:Hydrograph 阅读 31 收藏 0 点赞 0 评论 0
/**
 * Close Websocket connection Connection
 * @param session
 */
private void closeWebSocketConnection(final Session session ){
    try {
        Thread.sleep(3000);
    } catch (InterruptedException e1) {
    }

    if (session != null && session.isOpen()) {
        try {
            CloseReason closeReason = new CloseReason(
                    CloseCodes.NORMAL_CLOSURE, "Session Closed");
            session.close(closeReason);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
TrackingDisplayUtils.java 文件源码 项目:Hydrograph 阅读 27 收藏 0 点赞 0 评论 0
/**
 * 
 * Close websocket client connection.
 * @param session
 */
public void closeWebSocketConnection(Session session){
    try {
        Thread.sleep(DELAY);
    } catch (InterruptedException e1) {
    }
    if (session != null  && session.isOpen()) {
        try {
            CloseReason closeReason = new CloseReason(CloseCodes.NORMAL_CLOSURE,"Closed");
            session.close(closeReason);
            logger.info("Session closed");
        } catch (IOException e) {
            logger.error("Fail to close connection ",e);
        }

    }

}
DrawboardEndpoint.java 文件源码 项目:apache-tomcat-7.0.73-with-comment 阅读 30 收藏 0 点赞 0 评论 0
@Override
public void onClose(Session session, CloseReason closeReason) {
    Room room = getRoom(false);
    if (room != null) {
        room.invokeAndWait(new Runnable() {
            @Override
            public void run() {
                try {
                    // Player can be null if it couldn't enter the room
                    if (player != null) {
                        // Remove this player from the room.
                        player.removeFromRoom();

                        // Set player to null to prevent NPEs when onMessage events
                        // are processed (from other threads) after onClose has been
                        // called from different thread which closed the Websocket session.
                        player = null;
                    }
                } catch (RuntimeException ex) {
                    log.error("Unexpected exception: " + ex.toString(), ex);
                }
            }
        });
    }
}
WsFrameBase.java 文件源码 项目:apache-tomcat-7.0.73-with-comment 阅读 22 收藏 0 点赞 0 评论 0
@SuppressWarnings("unchecked")
private void sendMessageBinary(ByteBuffer msg, boolean last)
        throws WsIOException {
    if (binaryMsgHandler instanceof WrappedMessageHandler) {
        long maxMessageSize =
                ((WrappedMessageHandler) binaryMsgHandler).getMaxMessageSize();
        if (maxMessageSize > -1 && msg.remaining() > maxMessageSize) {
            throw new WsIOException(new CloseReason(CloseCodes.TOO_BIG,
                    sm.getString("wsFrame.messageTooBig",
                            Long.valueOf(msg.remaining()),
                            Long.valueOf(maxMessageSize))));
        }
    }
    try {
        if (binaryMsgHandler instanceof MessageHandler.Partial<?>) {
            ((MessageHandler.Partial<ByteBuffer>) binaryMsgHandler).onMessage(msg, last);
        } else {
            // Caller ensures last == true if this branch is used
            ((MessageHandler.Whole<ByteBuffer>) binaryMsgHandler).onMessage(msg);
        }
    } catch(Throwable t) {
        handleThrowableOnSend(t);
    }
}
PojoEndpointBase.java 文件源码 项目:apache-tomcat-7.0.73-with-comment 阅读 26 收藏 0 点赞 0 评论 0
@Override
public final void onClose(Session session, CloseReason closeReason) {

    if (methodMapping.getOnClose() != null) {
        try {
            methodMapping.getOnClose().invoke(pojo,
                    methodMapping.getOnCloseArgs(pathParameters, session, closeReason));
        } catch (Throwable t) {
            ExceptionUtils.handleThrowable(t);
            log.error(sm.getString("pojoEndpointBase.onCloseFail",
                    pojo.getClass().getName()), t);
        }
    }

    // Trigger the destroy method for any associated decoders
    Set<MessageHandler> messageHandlers = session.getMessageHandlers();
    for (MessageHandler messageHandler : messageHandlers) {
        if (messageHandler instanceof PojoMessageHandlerWholeBase<?>) {
            ((PojoMessageHandlerWholeBase<?>) messageHandler).onClose();
        }
    }
}
WsRemoteEndpointImplBase.java 文件源码 项目:apache-tomcat-7.0.73-with-comment 阅读 32 收藏 0 点赞 0 评论 0
private void handleSendFailureWithEncode(Throwable t) throws IOException, EncodeException {
    // First, unwrap any execution exception
    if (t instanceof ExecutionException) {
        t = t.getCause();
    }

    // Close the session
    wsSession.doClose(new CloseReason(CloseCodes.GOING_AWAY, t.getMessage()),
            new CloseReason(CloseCodes.CLOSED_ABNORMALLY, t.getMessage()));

    // Rethrow the exception
    if (t instanceof EncodeException) {
        throw (EncodeException) t;
    }
    if (t instanceof IOException) {
        throw (IOException) t;
    }
    throw new IOException(t);
}
WsWebSocketContainer.java 文件源码 项目:apache-tomcat-7.0.73-with-comment 阅读 21 收藏 0 点赞 0 评论 0
/**
 * Cleans up the resources still in use by WebSocket sessions created from
 * this container. This includes closing sessions and cancelling
 * {@link Future}s associated with blocking read/writes.
 */
public void destroy() {
    CloseReason cr = new CloseReason(
            CloseCodes.GOING_AWAY, sm.getString("wsWebSocketContainer.shutdown"));

    for (WsSession session : sessions.keySet()) {
        try {
            session.close(cr);
        } catch (IOException ioe) {
            log.debug(sm.getString(
                    "wsWebSocketContainer.sessionCloseFail", session.getId()), ioe);
        }
    }

    // Only unregister with AsyncChannelGroupUtil if this instance
    // registered with it
    if (asynchronousChannelGroup != null) {
        synchronized (asynchronousChannelGroupLock) {
            if (asynchronousChannelGroup != null) {
                AsyncChannelGroupUtil.unregister();
                asynchronousChannelGroup = null;
            }
        }
    }
}
DrawboardEndpoint.java 文件源码 项目:apache-tomcat-7.0.73-with-comment 阅读 24 收藏 0 点赞 0 评论 0
@Override
public void onClose(Session session, CloseReason closeReason) {
    Room room = getRoom(false);
    if (room != null) {
        room.invokeAndWait(new Runnable() {
            @Override
            public void run() {
                try {
                    // Player can be null if it couldn't enter the room
                    if (player != null) {
                        // Remove this player from the room.
                        player.removeFromRoom();

                        // Set player to null to prevent NPEs when onMessage events
                        // are processed (from other threads) after onClose has been
                        // called from different thread which closed the Websocket session.
                        player = null;
                    }
                } catch (RuntimeException ex) {
                    log.error("Unexpected exception: " + ex.toString(), ex);
                }
            }
        });
    }
}
WsSession.java 文件源码 项目:lazycat 阅读 26 收藏 0 点赞 0 评论 0
/**
 * Called when a close message is received. Should only ever happen once.
 * Also called after a protocol error when the ProtocolHandler needs to
 * force the closing of the connection.
 */
public void onClose(CloseReason closeReason) {

    synchronized (stateLock) {
        if (state != State.CLOSED) {
            try {
                wsRemoteEndpoint.setBatchingAllowed(false);
            } catch (IOException e) {
                log.warn(sm.getString("wsSession.flushFailOnClose"), e);
                fireEndpointOnError(e);
            }
            if (state == State.OPEN) {
                state = State.OUTPUT_CLOSED;
                sendCloseMessage(closeReason);
                fireEndpointOnClose(closeReason);
            }
            state = State.CLOSED;

            // Close the socket
            wsRemoteEndpoint.close();
        }
    }
}
EchoEndpoint.java 文件源码 项目:guice-websocket 阅读 28 收藏 0 点赞 0 评论 0
@PreDestroy
public void shutdown() {
    scheduledExecutorService.shutdown();
    clientsSessions.forEach(session -> {
        try {
            if (!session.isOpen()) {
                return;
            }

            session.close(new CloseReason(CloseCodes.SERVICE_RESTART, "Shutting down"));
        } catch (Exception e) {
            LOGGER.error("Error while closing session", e);
        }
    });
}
WsHttpUpgradeHandler.java 文件源码 项目:lazycat 阅读 29 收藏 0 点赞 0 评论 0
private void close(CloseReason cr) {
    /*
     * Any call to this method is a result of a problem reading from the
     * client. At this point that state of the connection is unknown.
     * Attempt to send a close frame to the client and then close the socket
     * immediately. There is no point in waiting for a close frame from the
     * client because there is no guarantee that we can recover from
     * whatever messed up state the client put the connection into.
     */
    wsSession.onClose(cr);
}
WS.java 文件源码 项目:OftenPorter 阅读 28 收藏 0 点赞 0 评论 0
public void close(CloseReason closeReason) throws IOException
{
    HttpSession httpSession = (HttpSession) session.getUserProperties().get(HttpSession.class.getName());
    httpSession.removeAttribute(WObject.class.getName());
    httpSession.removeAttribute(PorterOfFun.class.getName());
    httpSession.removeAttribute(WebSocket.class.getName());
    if (closeReason != null)
    {
        session.close(closeReason);
    } else
    {
        session.close();
    }
}
WsSession.java 文件源码 项目:tomcat7 阅读 33 收藏 0 点赞 0 评论 0
/**
 * WebSocket 1.0. Section 2.1.5.
 * Need internal close method as spec requires that the local endpoint
 * receives a 1006 on timeout.
 */
public void doClose(CloseReason closeReasonMessage,
        CloseReason closeReasonLocal) {
    // Double-checked locking. OK because state is volatile
    if (state != State.OPEN) {
        return;
    }

    synchronized (stateLock) {
        if (state != State.OPEN) {
            return;
        }

        if (log.isDebugEnabled()) {
            log.debug(sm.getString("wsSession.doClose", id));
        }
        try {
            wsRemoteEndpoint.setBatchingAllowed(false);
        } catch (IOException e) {
            log.warn(sm.getString("wsSession.flushFailOnClose"), e);
            fireEndpointOnError(e);
        }

        state = State.OUTPUT_CLOSED;

        sendCloseMessage(closeReasonMessage);
        fireEndpointOnClose(closeReasonLocal);
    }

    IOException ioe = new IOException(sm.getString("wsSession.messageFailed"));
    SendResult sr = new SendResult(ioe);
    for (FutureToSendHandler f2sh : futures.keySet()) {
        f2sh.onResult(sr);
    }
}
WsSession.java 文件源码 项目:tomcat7 阅读 34 收藏 0 点赞 0 评论 0
private void fireEndpointOnClose(CloseReason closeReason) {

        // Fire the onClose event
        Throwable throwable = null;
        InstanceManager instanceManager = webSocketContainer.getInstanceManager();
        Thread t = Thread.currentThread();
        ClassLoader cl = t.getContextClassLoader();
        t.setContextClassLoader(applicationClassLoader);
        try {
            localEndpoint.onClose(this, closeReason);
        } catch (Throwable t1) {
            ExceptionUtils.handleThrowable(t1);
            throwable = t1;
        } finally {
            if (instanceManager != null) {
                try {
                    instanceManager.destroyInstance(localEndpoint);
                } catch (Throwable t2) {
                    ExceptionUtils.handleThrowable(t2);
                    if (throwable == null) {
                        throwable = t2;
                    }
                }
            }
            t.setContextClassLoader(cl);
        }

        if (throwable != null) {
            fireEndpointOnError(throwable);
        }
    }


问题


面经


文章

微信
公众号

扫码关注公众号