Class PersistentSocket
-
- All Implemented Interfaces:
public class PersistentSocket<T extends Object> extends WebSocketListenerPersistentSocket 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)
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public classPersistentSocket.DisconnectReason
-
Field Summary
Fields Modifier and Type Field Description private LongreconnectTimeoutprivate final MutableSharedFlow<VideoEvent>eventsprivate final MutableSharedFlow<Throwable>errorsprivate final StateFlow<SocketState>connectionStateprivate final StateFlow<String>connectionIdprivate CancellableContinuation<T>connected
-
Constructor Summary
Constructors Constructor Description PersistentSocket(String url, OkHttpClient httpClient, NetworkStateProvider networkStateProvider, CoroutineScope scope, SuspendFunction0<Unit> onFastReconnected)
-
Method Summary
Modifier and Type Method Description final LonggetReconnectTimeout()final UnitsetReconnectTimeout(Long reconnectTimeout)final MutableSharedFlow<VideoEvent>getEvents()flow with all the events, listen to this final MutableSharedFlow<Throwable>getErrors()flow with temporary and permanent errors final StateFlow<SocketState>getConnectionState()the current connection state of the socket final StateFlow<String>getConnectionId()final CancellableContinuation<T>getConnected()Continuation if the socket successfully connected and we've authenticated final UnitsetConnected(CancellableContinuation<T> connected)Continuation if the socket successfully connected and we've authenticated Tconnect(Function1<CancellableContinuation<T>, Unit> invocation)Connect the socket, authenticate, start the healthmonitor and see if the network is online final Unitcleanup()final Unitdisconnect(PersistentSocket.DisconnectReason disconnectReason)Disconnect the socket final Unitreconnect(Long timeout)Increment the reconnection attempts, disconnect and reconnect Unitauthenticate()final UnitonInternetConnected()final UnitonInternetDisconnected()UnitonOpen(WebSocket webSocket, Response response)UnitonClosing(WebSocket webSocket, Integer code, String reason)Invoked when the remote peer has indicated that no more incoming messages will be transmitted. UnitonClosed(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. UnitonFailure(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. -
-
Constructor Detail
-
PersistentSocket
PersistentSocket(String url, OkHttpClient httpClient, NetworkStateProvider networkStateProvider, CoroutineScope scope, SuspendFunction0<Unit> onFastReconnected)
-
-
Method Detail
-
getReconnectTimeout
final Long getReconnectTimeout()
-
setReconnectTimeout
final Unit setReconnectTimeout(Long reconnectTimeout)
-
getEvents
final MutableSharedFlow<VideoEvent> getEvents()
flow with all the events, listen to this
-
getConnectionState
final StateFlow<SocketState> getConnectionState()
the current connection state of the socket
-
getConnectionId
final StateFlow<String> getConnectionId()
-
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
T connect(Function1<CancellableContinuation<T>, Unit> invocation)
Connect the socket, authenticate, start the healthmonitor and see if the network is online
-
disconnect
final Unit disconnect(PersistentSocket.DisconnectReason disconnectReason)
Disconnect the socket
-
reconnect
final Unit reconnect(Long timeout)
Increment the reconnection attempts, disconnect and reconnect
-
authenticate
Unit authenticate()
-
onInternetConnected
final Unit onInternetConnected()
-
onInternetDisconnected
final Unit onInternetDisconnected()
-
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.
-
-
-
-