-
- All Implemented Interfaces:
-
java.io.Closeable,java.lang.AutoCloseable
public final class EventSource implements Closeable
Server-Sent Events stream client.
EventSource is built on Sunday requests and allows customizing the requests for scenarios like authentication and custom headers.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public final classEventSource.EventSSE Event.
public classEventSource.Companionpublic enumEventSource.ReadyStatePossible states of the EventSource.
-
Field Summary
Fields Modifier and Type Field Description private final EventSource.ReadyStatereadyStateprivate final DurationretryTimeprivate Function0<Unit>onOpenprivate Function1<Throwable, Unit>onErrorprivate Function1<EventSource.Event, Unit>onMessageprivate final Map<String, Function1<EventSource.Event, Unit>>eventListeners
-
Constructor Summary
Constructors Constructor Description EventSource(SuspendFunction1<Iterable<Pair<String, String>>, Request> requestSupplier, Duration retryTime, Duration eventTimeout, Duration eventTimeoutCheckInterval, Logger logger)
-
Method Summary
Modifier and Type Method Description final EventSource.ReadyStategetReadyState()final DurationgetRetryTime()final Function0<Unit>getOnOpen()final UnitsetOnOpen(Function0<Unit> onOpen)final Function1<Throwable, Unit>getOnError()final UnitsetOnError(Function1<Throwable, Unit> onError)final Function1<EventSource.Event, Unit>getOnMessage()final UnitsetOnMessage(Function1<EventSource.Event, Unit> onMessage)final Map<String, Function1<EventSource.Event, Unit>>getEventListeners()final UnitaddEventListener(String event, Function1<EventSource.Event, Unit> handler)Add an event listener for a specific event type. final UnitremoveEventListener(String event, Function1<EventSource.Event, Unit> handler)Removed a previously added event listener. final Unitconnect()Connect the EventSource. Unitclose()Close and disconnect the EventSource. -
-
Method Detail
-
getReadyState
final EventSource.ReadyState getReadyState()
-
getRetryTime
final Duration getRetryTime()
-
getOnError
final Function1<Throwable, Unit> getOnError()
-
setOnError
final Unit setOnError(Function1<Throwable, Unit> onError)
-
getOnMessage
final Function1<EventSource.Event, Unit> getOnMessage()
-
setOnMessage
final Unit setOnMessage(Function1<EventSource.Event, Unit> onMessage)
-
getEventListeners
final Map<String, Function1<EventSource.Event, Unit>> getEventListeners()
-
addEventListener
final Unit addEventListener(String event, Function1<EventSource.Event, Unit> handler)
Add an event listener for a specific event type.
-
removeEventListener
final Unit removeEventListener(String event, Function1<EventSource.Event, Unit> handler)
Removed a previously added event listener.
-
connect
final Unit connect()
Connect the EventSource.
connect is idempotent and redundant calls will be ignored. Additionally, the EventSource will remain connected, including reconnecting when necessary, until close is called.
-
close
Unit close()
Close and disconnect the EventSource.
-
-
-
-