diff --git a/README.md b/README.md index 072a33c..c0f592c 100644 --- a/README.md +++ b/README.md @@ -2,11 +2,15 @@ An Android EventSource (SSE - Server Sent Events) Library +## Installation jCenter Gradle import +```groovy +implementation 'com.tylerjroach:eventsource:1.2.11' +``` - compile 'com.tylerjroach:eventsource:1.2.11' - -## What's new (1.2.11) +## What's new + +1.2.11 * Removed need to create event source in background thread, now done automatically * First official jcenter release * Executor fix @@ -24,62 +28,72 @@ jCenter Gradle import * Ability to choose thread for callbacks to return -##Example implementation: - - private SSEHandler sseHandler = new SSEHandler(); - - private void startEventSource() { - eventSource = new EventSource.Builder(eventUrl) - .eventHandler(sseHandler) - .headers(extraHeaderParameters) - .build(); - eventSource.connect(); +## Example + +```java +private SSEHandler sseHandler = new SSEHandler(); +private EventSource eventSource; + +private void startEventSource() { + String eventUrl = YOUR_URL; + + Map headerParameters = new HashMap<>(); + headerParameters.put("Content-Type", "text/event-stream"); + headerParameters.put("Authorization", "Bearer YOUR_TOKEN"); + + eventSource = new EventSource.Builder(eventUrl) + .eventHandler(sseHandler) + .headers(headerParameters) + .build(); + eventSource.connect(); +} + +private void stopEventSource() { + if (eventSource != null) + eventSource.close(); + sseHandler = null; +} + +/** +* All callbacks are currently returned on executor thread. +* If you want to update the ui from a callback, make sure to post to main thread +*/ + +private class SSEHandler implements EventSourceHandler { + + public SSEHandler() {} + + @Override + public void onConnect() { + Log.v("SSE Connected", "True"); } - - private void stopEventSource() { - if (eventsource != null) - eventSource.close(); - sseHandler = null; + + @Override + public void onMessage(String event, MessageEvent message) { + Log.v("SSE Message", event); + Log.v("SSE Message: ", message.lastEventId); + Log.v("SSE Message: ", message.data); + } + + @Override + public void onComment(String comment) { + //comments only received if exposeComments turned on + Log.v("SSE Comment", comment); + } + + @Override + public void onError(Throwable t) { + //ignore ssl NPE on eventSource.close() + } + + @Override + public void onClosed(boolean willReconnect) { + Log.v("SSE Closed", "reconnect? " + willReconnect); } - - /** - * All callbacks are currently returned on executor thread. - * If you want to update the ui from a callback, make sure to post to main thread - */ - - private class SSEHandler implements EventSourceHandler { - - public SSEHandler() {} - - @Override - public void onConnect() { - Log.v("SSE Connected", "True"); - } - - @Override - public void onMessage(String event, MessageEvent message) { - Log.v("SSE Message", event); - Log.v("SSE Message: ", message.lastEventId); - Log.v("SSE Message: ", message.data); - } - - @Override - public void onComment(String comment) { - //comments only received if exposeComments turned on - Log.v("SSE Comment", comment); - } - - @Override - public void onError(Throwable t) { - //ignore ssl NPE on eventSource.close() - } - - @Override - public void onClosed(boolean willReconnect) { - Log.v("SSE Closed", "reconnect? " + willReconnect); - } - -To stop event source, make sure to run eventSource.close() +} +``` + +To stop event source, make sure to run `eventSource.close()` If you have a pull request, please follow square-android style guides found here: https://github.com/square/java-code-styles