Skip to content

Commit

Permalink
add 'Last-Event-Id' header
Browse files Browse the repository at this point in the history
'Last-Event-Id' is part of SSE protool, used for handling retry correctly by restring last event sent to client
  • Loading branch information
yosriz committed Mar 28, 2018
1 parent 2315770 commit 358f8b2
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/main/java/com/here/oksse/RealServerSentEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class RealServerSentEvent implements ServerSentEvent {

private long reconnectTime = TimeUnit.SECONDS.toMillis(3);
private long readTimeoutMillis = 0;
private String lastEventId;

RealServerSentEvent(Request request, Listener listener) {
if (!"GET".equals(request.method())) {
Expand All @@ -53,13 +54,16 @@ private void prepareCall(Request request) {
if (client == null) {
throw new AssertionError("Client is null");
}
Request newRequest = request.newBuilder()
Request.Builder requestBuilder = request.newBuilder()
.header("Accept-Encoding", "")
.header("Accept", "text/event-stream")
.header("Cache-Control", "no-cache")
.build();
.header("Cache-Control", "no-cache");

call = client.newCall(newRequest);
if (lastEventId != null) {
requestBuilder.header("Last-Event-Id", lastEventId);
}

call = client.newCall(requestBuilder.build());
}

private void enqueue() {
Expand Down Expand Up @@ -168,7 +172,6 @@ private class Reader {
// Intentionally done to reuse StringBuilder for memory optimization
@SuppressWarnings("PMD.AvoidStringBufferField")
private StringBuilder data = new StringBuilder();
private String lastEventId;
private String eventName = DEFAULT_EVENT;

Reader(BufferedSource source) {
Expand Down Expand Up @@ -216,7 +219,7 @@ void setTimeout(long timeout, TimeUnit unit) {

private void processLine(String line) {
//log("Sse read line: " + line);
if (line.isEmpty()) { // If the line is empty (a blank line). Dispatch the event.
if (line == null || line.isEmpty()) { // If the line is empty (a blank line). Dispatch the event.
dispatchEvent();
return;
}
Expand Down

0 comments on commit 358f8b2

Please sign in to comment.