Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve README / Documentation #17

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 71 additions & 57 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<String, String> 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

Expand Down