1
1
package io .avaje .jex .http .sse ;
2
2
3
+ import java .io .Closeable ;
4
+ import java .util .function .Consumer ;
5
+
3
6
import io .avaje .jex .http .Context ;
4
7
import io .avaje .jex .http .ExchangeHandler ;
5
8
import io .avaje .jex .spi .JsonService ;
6
- import java .io .Closeable ;
7
- import java .util .function .Consumer ;
8
9
9
10
/**
10
- * A client for Server-Sent Events (SSE). This class handles the setup of the SSE connection, sending events and
11
- * comments to the client, and managing the lifecycle of the connection. It ensures proper headers are set and provides
12
- * methods for sending various types of data.
11
+ * A client for Server-Sent Events (SSE). This class handles the setup of the SSE connection,
12
+ * sending events and comments to the client, and managing the lifecycle of the connection. It
13
+ * ensures proper headers are set and provides methods for sending various types of data.
13
14
*
14
- * <p>This class implements {@link Closeable} to allow for proper resource management. The connection is automatically
15
- * closed if the client disconnects or if an error occurs during event emission.
15
+ * <p>This class implements {@link Closeable} to allow for proper resource management. The
16
+ * connection is automatically closed if the client disconnects or if an error occurs during event
17
+ * emission.
16
18
*/
17
19
public interface SseClient extends Closeable {
18
20
@@ -33,14 +35,23 @@ static ExchangeHandler handler(Consumer<SseClient> consumer) {
33
35
Context ctx ();
34
36
35
37
/**
36
- * By blocking the SSE connection, you can share this client outside the handler to notify it from other sources.
37
- * Keep in mind that this function will block the handler until the SSE client is released by another thread.
38
+ * By blocking the SSE connection, you can share this client outside the handler to notify it from
39
+ * other sources. Keep in mind that this function will block the handler until the SSE client is
40
+ * released by another thread.
38
41
*/
39
42
void keepAlive ();
40
43
41
44
/**
42
- * Attempt to send a comment. If the {@link Emitter} fails to emit (remote client has disconnected), the
43
- * {@link #close()} function will be called instead.
45
+ * Add a callback that will be called either when connection is closed through {@link #close()},
46
+ * or when the {@link Emitter} is detected as closed.
47
+ *
48
+ * @param task task to run
49
+ */
50
+ void onClose (Runnable task );
51
+
52
+ /**
53
+ * Attempt to send a comment. If the {@link Emitter} fails to emit (remote client has
54
+ * disconnected), the {@link #close()} function will be called instead.
44
55
*/
45
56
void sendComment (String comment );
46
57
@@ -51,19 +62,19 @@ static ExchangeHandler handler(Consumer<SseClient> consumer) {
51
62
void sendEvent (String event , Object data );
52
63
53
64
/**
54
- * Attempt to send an event. If the {@link Emitter} fails to emit (remote client has disconnected), the
55
- * {@link #close()} function will be called instead.
65
+ * Attempt to send an event. If the {@link Emitter} fails to emit (remote client has
66
+ * disconnected), the {@link #close()} function will be called instead.
56
67
*
57
68
* @param event The name of the event.
58
- * @param data The data to send in the event. This can be a String, an InputStream, or any object that can be
59
- * serialized to JSON using the configured {@link JsonService}.
69
+ * @param data The data to send in the event. This can be a String, an InputStream, or any object
70
+ * that can be serialized to JSON using the configured {@link JsonService}.
60
71
* @param id The ID of the event.
61
72
*/
62
73
void sendEvent (String event , Object data , String id );
63
74
64
75
/**
65
- * Returns true if {@link #close()} has been called. This can either be by the user, or by Jex upon detecting that
66
- * the {@link Emitter} is closed.
76
+ * Returns true if {@link #close()} has been called. This can either be by the user, or by Jex
77
+ * upon detecting that the {@link Emitter} is closed.
67
78
*/
68
79
boolean terminated ();
69
80
}
0 commit comments