/** * Create a new {@link CloseStatus} from this one with the specified reason. * @param reason the reason * @return a new {@link CloseStatus} instance */ public CloseStatus withReason(String reason) { Assert.hasText(reason, "Reason must not be empty"); return new CloseStatus(this.code, reason); }
@Override public String toString() { return "SessionDisconnectEvent[sessionId=" + this.sessionId + ", " + this.status.toString() + "]"; }
private IIdType bindSimple(WebSocketSession theSession, String theBindString) { IdType id = new IdType(theBindString); if (!id.hasIdPart() || !id.isIdPartValid()) { try { String message = "Invalid bind request - No ID included"; ourLog.warn(message); theSession.close(new CloseStatus(CloseStatus.PROTOCOL_ERROR.getCode(), message)); } catch (IOException e) { handleFailure(e); } return null; } if (id.hasResourceType() == false) { id = id.withResourceType("Subscription"); } try { ActiveSubscription activeSubscription = mySubscriptionRegistry.get(id.getIdPart()); myState = new BoundStaticSubscipriptionState( theSession, activeSubscription); } catch (ResourceNotFoundException e) { try { String message = "Invalid bind request - Unknown subscription: " + id.getValue(); ourLog.warn(message); theSession.close(new CloseStatus(CloseStatus.PROTOCOL_ERROR.getCode(), message)); } catch (IOException e1) { handleFailure(e); } return null; } return id; }
@Override protected void handleTextMessage(WebSocketSession session, TextMessage message) { try { session.close(CloseStatus.NOT_ACCEPTABLE.withReason("Text messages not supported")); } catch (IOException ex) { // ignore } }
@Override public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception { if (session.isOpen()) { session.close(); } sessions.remove(session); logger.info(String.format("Session %s closed because of %s", session.getId(), status.getReason())); }
@Override public void close(CloseStatus status) throws IOException { this.closeLock.lock(); try { if (this.closeInProgress) { return; } if (!CloseStatus.SESSION_NOT_RELIABLE.equals(status)) { try { checkSessionLimits(); } catch (SessionLimitExceededException ex) { // Ignore } if (this.limitExceeded) { if (logger.isDebugEnabled()) { logger.debug("Changing close status " + status + " to SESSION_NOT_RELIABLE."); } status = CloseStatus.SESSION_NOT_RELIABLE; } } this.closeInProgress = true; super.close(status); } finally { this.closeLock.unlock(); } }
/** * Invoked after the WebSocket connection has been closed by either side, or after a * transport error has occurred. Although the session may technically still be open, * depending on the underlying implementation, sending messages at this point is * discouraged and most likely will not succeed. */ @Override public void afterConnectionClosed(final WebSocketSession session, CloseStatus status) throws Exception { if (!status.equalsCode(CloseStatus.NORMAL)) { log.warn("[Handler::afterConnectionClosed] status: {}, sessionId: {}", status, session.getId()); } stop(session); }
@Override public void closeNativeSession(String reason) { try { wsSession.close(new CloseStatus(CloseStatus.NORMAL.getCode(), reason)); } catch (IOException e) { log.warn("Exception closing webSocket session", e); } }
@Test public void closeWhenNotOpen() throws Exception { assertNew(); this.session.close(); assertNull("Close not ignored for a new session", this.session.getCloseStatus()); this.session.delegateConnectionEstablished(); assertOpen(); this.session.close(); assertClosed(); assertEquals(3000, this.session.getCloseStatus().getCode()); this.session.close(CloseStatus.SERVER_ERROR); assertEquals("Close should be ignored if already closed", 3000, this.session.getCloseStatus().getCode()); }
@Override protected void handleBinaryMessage(WebSocketSession session, BinaryMessage message) { try { session.close(CloseStatus.NOT_ACCEPTABLE.withReason("Binary messages not supported")); } catch (IOException ex) { // ignore } }
@Override public void close(CloseStatus status) throws IOException { this.closeLock.lock(); try { if (this.closeInProgress) { return; } if (!CloseStatus.SESSION_NOT_RELIABLE.equals(status)) { try { checkSessionLimits(); } catch (SessionLimitExceededException ex) { // Ignore } if (this.limitExceeded) { if (logger.isDebugEnabled()) { logger.debug("Changing close status " + status + " to SESSION_NOT_RELIABLE."); } status = CloseStatus.SESSION_NOT_RELIABLE; } } this.closeInProgress = true; super.close(status); } finally { this.closeLock.unlock(); } }
@Override public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception { log.debug("Connection Closed[{}], CloseStatus={}.", this.getName(), status); Heartbeat heartbeat = webSocketManager.getHeartbeat(); if (heartbeat != null) { heartbeat.stop(); } this.session = null; // normal close if (CloseStatus.NORMAL.equalsCode(status)) { log.info("Connection[{}] is normally closed.", this.getName()); return; } // abnormal close log.error("Connection[{}] is abnormally closed, CloseStatus={}.", this.getName(), status); abnormalConnectionClosedListener.accept(status); }
/** * Performs cleanup and notify the {@link WebSocketHandler}. */ @Override public final void close() throws IOException { close(new CloseStatus(3000, "Go away!")); }
@Override public void closeNativeSession(String reason) { try { wsSession.close(new CloseStatus(CloseStatus.NORMAL.getCode(), reason)); } catch (IOException e) { log.warn("Exception closing webSocket session", e); } }