Class AbstractSockJsSession
- All Implemented Interfaces:
Closeable,AutoCloseable,SockJsSession,WebSocketSession
- Direct Known Subclasses:
AbstractHttpSockJsSession,WebSocketServerSockJsSession
SockJsSession.- Since:
- 4.0
- Author:
- Rossen Stoyanchev, Sam Brannen
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionAbstractSockJsSession(String id, SockJsServiceConfig config, WebSocketHandler handler, Map<String, Object> attributes) Create a new instance. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidfinal voidclose()Performs cleanup and notify theWebSocketHandler.final voidclose(CloseStatus status) Performs cleanup and notify theWebSocketHandler.final voiddelegateConnectionClosed(CloseStatus status) Invoked when the underlying connection is closed.voidvoidvoiddelegateMessages(String... messages) voidDisable the SockJS heartbeat, presumably because a higher-level protocol has heartbeats enabled for the session already.protected abstract voiddisconnect(CloseStatus status) Actually close the underlying WebSocket session or in the case of HTTP transports complete the underlying request.Return the map with attributes associated with the WebSocket session.getId()Return a unique session identifier.protected SockJsMessageCodeclongReturn the time (in ms) since the session was last active, or otherwise if the session is new, then the time since the session was created.abstract booleanisActive()Polling and Streaming sessions periodically close the current HTTP request and wait for the next request to come through.booleanisClosed()booleanisNew()booleanisOpen()Whether the underlying connection is open.protected voidprotected voidfinal voidsendMessage(WebSocketMessage<?> message) Send a WebSocket message: eitherTextMessageorBinaryMessage.protected abstract voidsendMessageInternal(String message) toString()voidtryCloseWithSockJsTransportError(Throwable error, CloseStatus closeStatus) Close due to error arising from SockJS transport handling.protected voidShould be invoked whenever the session becomes inactive.protected voidwriteFrame(SockJsFrame frame) For internal use within a TransportHandler and the (TransportHandler-specific) session class.protected abstract voidwriteFrameInternal(SockJsFrame frame) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.springframework.web.socket.WebSocketSession
getAcceptedProtocol, getBinaryMessageSizeLimit, getExtensions, getHandshakeHeaders, getLocalAddress, getPrincipal, getRemoteAddress, getTextMessageSizeLimit, getUri, setBinaryMessageSizeLimit, setTextMessageSizeLimit
-
Field Details
-
DISCONNECTED_CLIENT_LOG_CATEGORY
Log category to use on network IO exceptions after a client has gone away.Servlet containers don't expose a client disconnected callback; see eclipse-ee4j/servlet-api#44. Therefore network IO failures may occur simply because a client has gone away, and that can fill the logs with unnecessary stack traces.
We make a best effort to identify such network failures, on a per-server basis, and log them under a separate log category. A simple one-line message is logged at DEBUG level, while a full stack trace is shown at TRACE level.
-
disconnectedClientLogger
Separate logger to use on network IO failure after a client has gone away.- See Also:
-
logger
-
responseLock
-
-
Constructor Details
-
AbstractSockJsSession
public AbstractSockJsSession(String id, SockJsServiceConfig config, WebSocketHandler handler, @Nullable Map<String, Object> attributes) Create a new instance.- Parameters:
id- the session IDconfig- the SockJS service configuration optionshandler- the recipient of SockJS messagesattributes- the attributes from the HTTP handshake to associate with the WebSocket session; the provided attributes are copied, the original map is not used.
-
-
Method Details
-
getId
Description copied from interface:WebSocketSessionReturn a unique session identifier.- Specified by:
getIdin interfaceWebSocketSession
-
getMessageCodec
-
getSockJsServiceConfig
-
getAttributes
Description copied from interface:WebSocketSessionReturn the map with attributes associated with the WebSocket session.On the server side the map can be populated initially through a
HandshakeInterceptor. On the client side the map can be populated viaWebSocketClienthandshake methods.- Specified by:
getAttributesin interfaceWebSocketSession- Returns:
- a Map with the session attributes (never
null)
-
sendMessage
Description copied from interface:WebSocketSessionSend a WebSocket message: eitherTextMessageorBinaryMessage.Note: The underlying standard WebSocket session (JSR-356) does not allow concurrent sending. Therefore sending must be synchronized. To ensure that, one option is to wrap the
WebSocketSessionwith theConcurrentWebSocketSessionDecorator.- Specified by:
sendMessagein interfaceWebSocketSession- Throws:
IOException- See Also:
-
sendMessageInternal
- Throws:
IOException
-
isNew
public boolean isNew() -
isOpen
public boolean isOpen()Description copied from interface:WebSocketSessionWhether the underlying connection is open.- Specified by:
isOpenin interfaceWebSocketSession
-
isClosed
public boolean isClosed() -
close
Performs cleanup and notify theWebSocketHandler.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceWebSocketSession- Throws:
IOException
-
close
Performs cleanup and notify theWebSocketHandler.- Specified by:
closein interfaceWebSocketSession- Throws:
IOException
-
getTimeSinceLastActive
public long getTimeSinceLastActive()Description copied from interface:SockJsSessionReturn the time (in ms) since the session was last active, or otherwise if the session is new, then the time since the session was created.- Specified by:
getTimeSinceLastActivein interfaceSockJsSession
-
updateLastActiveTime
protected void updateLastActiveTime()Should be invoked whenever the session becomes inactive. -
disableHeartbeat
public void disableHeartbeat()Description copied from interface:SockJsSessionDisable the SockJS heartbeat, presumably because a higher-level protocol has heartbeats enabled for the session already. It is not recommended to disable this otherwise, as it helps proxies to know the connection is not hanging.- Specified by:
disableHeartbeatin interfaceSockJsSession
-
sendHeartbeat
- Throws:
SockJsTransportFailureException
-
scheduleHeartbeat
protected void scheduleHeartbeat() -
cancelHeartbeat
protected void cancelHeartbeat() -
isActive
public abstract boolean isActive()Polling and Streaming sessions periodically close the current HTTP request and wait for the next request to come through. During this "downtime" the session is still open but inactive and unable to send messages and therefore has to buffer them temporarily. A WebSocket session by contrast is stateful and remain active until closed. -
disconnect
Actually close the underlying WebSocket session or in the case of HTTP transports complete the underlying request.- Throws:
IOException
-
writeFrame
For internal use within a TransportHandler and the (TransportHandler-specific) session class.- Throws:
SockJsTransportFailureException
-
writeFrameInternal
- Throws:
IOException
-
delegateConnectionEstablished
- Throws:
Exception
-
delegateMessages
- Throws:
SockJsMessageDeliveryException
-
delegateConnectionClosed
Invoked when the underlying connection is closed.- Throws:
Exception
-
tryCloseWithSockJsTransportError
Close due to error arising from SockJS transport handling. -
delegateError
- Throws:
Exception
-
toString
-