Class PersistentSocket

  • All Implemented Interfaces:

    
    public class PersistentSocket<T extends Object>
    extends WebSocketListener
                        

    PersistentSocket architecture

    • Healthmonitor that sends a ping every 30 seconds

    • Automatically reconnects if it encounters a temp failure

    • Raises the error if there is a permanent failure

    • Flow to avoid concurrency related bugs

    • Ability to wait till the socket is connected (important to prevent race conditions)

    • Constructor Detail

      • PersistentSocket

        PersistentSocket(String url, OkHttpClient httpClient, NetworkStateProvider networkStateProvider, CoroutineScope scope)
    • Method Detail

      • getEvents

         final MutableSharedFlow<VideoEvent> getEvents()

        flow with all the events, listen to this

      • getErrors

         final MutableSharedFlow<Throwable> getErrors()

        flow with temporary and permanent errors

      • getConnected

         final CancellableContinuation<T> getConnected()

        Continuation if the socket successfully connected and we've authenticated

      • setConnected

         final Unit setConnected(CancellableContinuation<T> connected)

        Continuation if the socket successfully connected and we've authenticated

      • connect

         final T connect(Function1<CancellableContinuation<T>, Unit> invocation)

        Connect the socket, authenticate, start the healthmonitor and see if the network is online

      • reconnect

         final Unit reconnect(Long timeout)

        Increment the reconnection attempts, disconnect and reconnect

      • onOpen

         Unit onOpen(WebSocket webSocket, Response response)
      • onMessage

         Unit onMessage(WebSocket webSocket, String text)

        Invoked when a text (type 0x1) message has been received.

      • onMessage

         Unit onMessage(WebSocket webSocket, ByteString bytes)

        Invoked when a binary (type 0x2) message has been received.

      • onClosing

         Unit onClosing(WebSocket webSocket, Integer code, String reason)

        Invoked when the remote peer has indicated that no more incoming messages will be transmitted.

      • onClosed

         Unit onClosed(WebSocket webSocket, Integer code, String reason)

        Invoked when both peers have indicated that no more messages will be transmitted and the connection has been successfully released. No further calls to this listener will be made.

      • onFailure

         Unit onFailure(WebSocket webSocket, Throwable t, Response response)

        Invoked when a web socket has been closed due to an error reading from or writing to the network. Both outgoing and incoming messages may have been lost. No further calls to this listener will be made.