org.springframework.web.reactive.socket.adapter

Class AbstractListenerWebSocketSession<T>

  • Type Parameters:
    T - the native delegate type
    All Implemented Interfaces:
    org.reactivestreams.Subscriber<java.lang.Void>, WebSocketSession
    Direct Known Subclasses:
    JettyWebSocketSession, StandardWebSocketSession, UndertowWebSocketSession


    public abstract class AbstractListenerWebSocketSession<T>
    extends AbstractWebSocketSession<T>
    implements org.reactivestreams.Subscriber<java.lang.Void>
    Base class for WebSocketSession implementations that bridge between event-listener WebSocket APIs (e.g. Java WebSocket API JSR-356, Jetty, Undertow) and Reactive Streams.

    Also an implementation of Subscriber&lt;Void&gt; so it can be used as the completion subscriber for session handling

    Since:
    5.0
    Author:
    Violeta Georgieva, Rossen Stoyanchev
    • Constructor Detail

      • AbstractListenerWebSocketSession

        public AbstractListenerWebSocketSession(T delegate,
                                                java.lang.String id,
                                                HandshakeInfo handshakeInfo,
                                                DataBufferFactory bufferFactory)
        Base constructor.
        Parameters:
        delegate - the native WebSocket session, channel, or connection
        id - the session id
        handshakeInfo - the handshake info
        bufferFactory - the DataBuffer factor for the current connection
      • AbstractListenerWebSocketSession

        public AbstractListenerWebSocketSession(T delegate,
                                                java.lang.String id,
                                                HandshakeInfo info,
                                                DataBufferFactory bufferFactory,
                                                @Nullable
                                                reactor.core.publisher.MonoProcessor<java.lang.Void> completionMono)
        Alternative constructor with completion Mono&lt;Void&gt; to propagate the session completion (success or error) (for client-side use).
    • Method Detail

      • receive

        public reactor.core.publisher.Flux<WebSocketMessage> receive()
        Description copied from interface: WebSocketSession
        Provides access to the stream of inbound messages.

        This stream receives a completion or error signal when the connection is closed. In a typical WebSocketHandler implementation this stream is composed into the overall processing flow, so that when the connection is closed, handling will end.

        See the class-level doc of WebSocketHandler and the reference for more details and examples of how to handle the session.

        Specified by:
        receive in interface WebSocketSession
        Specified by:
        receive in class AbstractWebSocketSession<T>
      • send

        public reactor.core.publisher.Mono<java.lang.Void> send(org.reactivestreams.Publisher<WebSocketMessage> messages)
        Description copied from interface: WebSocketSession
        Give a source of outgoing messages, write the messages and return a Mono<Void> that completes when the source completes and writing is done.

        See the class-level doc of WebSocketHandler and the reference for more details and examples of how to handle the session.

        Specified by:
        send in interface WebSocketSession
        Specified by:
        send in class AbstractWebSocketSession<T>
      • canSuspendReceiving

        protected abstract boolean canSuspendReceiving()
        Whether the underlying WebSocket API has flow control and can suspend and resume the receiving of messages.

        Note: Sub-classes are encouraged to start out in suspended mode, if possible, and wait until demand is received.

      • suspendReceiving

        protected abstract void suspendReceiving()
        Suspend receiving until received message(s) are processed and more demand is generated by the downstream Subscriber.

        Note: if the underlying WebSocket API does not provide flow control for receiving messages, this method should be a no-op and canSuspendReceiving() should return false.

      • resumeReceiving

        protected abstract void resumeReceiving()
        Resume receiving new message(s) after demand is generated by the downstream Subscriber.

        Note: if the underlying WebSocket API does not provide flow control for receiving messages, this method should be a no-op and canSuspendReceiving() should return false.

      • sendMessage

        protected abstract boolean sendMessage(WebSocketMessage message)
                                        throws java.io.IOException
        Send the given WebSocket message.

        Note: Sub-classes are responsible for releasing the payload data buffer, once fully written, if pooled buffers apply to the underlying container.

        Throws:
        java.io.IOException
      • onSubscribe

        public void onSubscribe(org.reactivestreams.Subscription subscription)
        Specified by:
        onSubscribe in interface org.reactivestreams.Subscriber<java.lang.Void>
      • onNext

        public void onNext(java.lang.Void aVoid)
        Specified by:
        onNext in interface org.reactivestreams.Subscriber<java.lang.Void>
      • onError

        public void onError(java.lang.Throwable ex)
        Specified by:
        onError in interface org.reactivestreams.Subscriber<java.lang.Void>
      • onComplete

        public void onComplete()
        Specified by:
        onComplete in interface org.reactivestreams.Subscriber<java.lang.Void>