I was trying to connect SSE from android app but it have a problem. It is not showing data 100%. Like in first attempt someone trigger notify command then it won't work but after that second attempt everybody get notify. Here is my code: //calling this code from activity on create
EventHandler eventHandler = new SimpleEventsListener();
String url = String.format(ApiInterface.ENDPOINT + "notify/");
EventSource.Builder builder = new EventSource.Builder(eventHandler, URI.create(url)).connectTimeoutMs(29 * 1000);
try (EventSource eventSource = builder.build()) {
for (; ; ) {
eventSource.start();
TimeUnit.SECONDS.sleep(30);
}
}
here is EventSource implementation.
public class SimpleEventsListener implements EventHandler {
@Override
public void onOpen() throws Exception {
System.out.println("SimpleEventsListener onOpen ");
}
@Override
public void onClosed() throws Exception {
System.out.println("SimpleEventsListener onClosed");
}
@Override
public void onMessage(String event, MessageEvent messageEvent) throws Exception {
System.out.println("SimpleEventsListener Event received " + messageEvent.getData());
// here implemented data recieved code which coming from messageEvent.getData()
}
@Override
public void onComment(String comment) throws Exception {
System.out.println("SimpleEventsListener onComment");
}
@Override
public void onError(Throwable t) {
System.out.println("SimpleEventsListener onError: " + t);
}
}
What is the of best way to receive data on the first attempt too?