mirror of
https://github.com/AsamK/signal-cli
synced 2025-09-06 06:00:38 +00:00
Add http endpoint events with SSE
This commit is contained in:
parent
1d98e5307a
commit
a780be70dd
2 changed files with 176 additions and 0 deletions
|
@ -0,0 +1,55 @@
|
|||
package org.asamk.signal.http;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* This class send Server-sent events payload to an OutputStream.
|
||||
* See <a href="https://html.spec.whatwg.org/multipage/server-sent-events.html">spec</a>
|
||||
*/
|
||||
public class ServerSentEventSender {
|
||||
|
||||
private final BufferedWriter writer;
|
||||
|
||||
public ServerSentEventSender(final OutputStream outputStream) {
|
||||
this.writer = new BufferedWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param id Event id
|
||||
* @param event Event type
|
||||
* @param data Event data, each entry must not contain newline chars.
|
||||
*/
|
||||
public synchronized void sendEvent(String id, String event, List<String> data) throws IOException {
|
||||
if (id != null) {
|
||||
writer.write("id:");
|
||||
writer.write(id);
|
||||
writer.write("\n");
|
||||
}
|
||||
if (event != null) {
|
||||
writer.write("event:");
|
||||
writer.write(event);
|
||||
writer.write("\n");
|
||||
}
|
||||
if (data.size() == 0) {
|
||||
writer.write("data\n");
|
||||
} else {
|
||||
for (final var d : data) {
|
||||
writer.write("data:");
|
||||
writer.write(d);
|
||||
writer.write("\n");
|
||||
}
|
||||
}
|
||||
writer.write("\n");
|
||||
writer.flush();
|
||||
}
|
||||
|
||||
public synchronized void sendKeepAlive() throws IOException {
|
||||
writer.write(":\n");
|
||||
writer.flush();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue