org.springframework.web.socket.sockjs.transport.session

Class AbstractSockJsSession

  • java.lang.Object
    • org.springframework.web.socket.sockjs.transport.session.AbstractSockJsSession
    • Field Detail

      • DISCONNECTED_CLIENT_LOG_CATEGORY

        public static final java.lang.String DISCONNECTED_CLIENT_LOG_CATEGORY
        Log category to use on network IO exceptions after a client has gone away.

        The Servlet API does not provide notifications when a client disconnects; see SERVLET_SPEC-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.

        See Also:
        disconnectedClientLogger, Constant Field Values
      • disconnectedClientLogger

        protected static final Log disconnectedClientLogger
        Separate logger to use on network IO failure after a client has gone away.
        See Also:
        DISCONNECTED_CLIENT_LOG_CATEGORY
      • logger

        protected final Log logger
      • responseLock

        protected final java.lang.Object responseLock
    • Constructor Detail

      • AbstractSockJsSession

        public AbstractSockJsSession(java.lang.String id,
                                     SockJsServiceConfig config,
                                     WebSocketHandler handler,
                                     @Nullable
                                     java.util.Map<java.lang.String,java.lang.Object> attributes)
        Create a new instance.
        Parameters:
        id - the session ID
        config - the SockJS service configuration options
        handler - the recipient of SockJS messages
        attributes - attributes from the HTTP handshake to associate with the WebSocket session; the provided attributes are copied, the original map is not used.
    • Method Detail

      • getAttributes

        public java.util.Map<java.lang.String,java.lang.Object> getAttributes()
        Description copied from interface: WebSocketSession
        Return 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 via WebSocketClient handshake methods.

        Specified by:
        getAttributes in interface WebSocketSession
        Returns:
        a Map with the session attributes (never null)
      • sendMessageInternal

        protected abstract void sendMessageInternal(java.lang.String message)
                                             throws java.io.IOException
        Throws:
        java.io.IOException
      • isNew

        public boolean isNew()
      • isClosed

        public boolean isClosed()
      • close

        public final void close()
                         throws java.io.IOException
        Performs cleanup and notify the WebSocketHandler.
        Specified by:
        close in interface java.io.Closeable
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface WebSocketSession
        Throws:
        java.io.IOException
      • getTimeSinceLastActive

        public long getTimeSinceLastActive()
        Description copied from interface: SockJsSession
        Return 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:
        getTimeSinceLastActive in interface SockJsSession
      • updateLastActiveTime

        protected void updateLastActiveTime()
        Should be invoked whenever the session becomes inactive.
      • disableHeartbeat

        public void disableHeartbeat()
        Description copied from interface: SockJsSession
        Disable 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:
        disableHeartbeat in interface SockJsSession
      • 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

        protected abstract void disconnect(CloseStatus status)
                                    throws java.io.IOException
        Actually close the underlying WebSocket session or in the case of HTTP transports complete the underlying request.
        Throws:
        java.io.IOException
      • writeFrameInternal

        protected abstract void writeFrameInternal(SockJsFrame frame)
                                            throws java.io.IOException
        Throws:
        java.io.IOException
      • delegateConnectionEstablished

        public void delegateConnectionEstablished()
                                           throws java.lang.Exception
        Throws:
        java.lang.Exception
      • delegateConnectionClosed

        public final void delegateConnectionClosed(CloseStatus status)
                                            throws java.lang.Exception
        Invoked when the underlying connection is closed.
        Throws:
        java.lang.Exception
      • tryCloseWithSockJsTransportError

        public void tryCloseWithSockJsTransportError(java.lang.Throwable error,
                                                     CloseStatus closeStatus)
        Close due to error arising from SockJS transport handling.
      • delegateError

        public void delegateError(java.lang.Throwable ex)
                           throws java.lang.Exception
        Throws:
        java.lang.Exception
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object