Skip to content

Commit

Permalink
Merge pull request #15 from kinecosystem/add_last_event_id_header
Browse files Browse the repository at this point in the history
add 'Last-Event-Id' header
  • Loading branch information
marcelpinto authored Apr 24, 2018
2 parents 0c60781 + 358f8b2 commit c92d055
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 @@ -162,7 +166,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 @@ -199,7 +202,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 c92d055

Please sign in to comment.