Search in sources :

Example 1 with ServerSentEvent

use of com.here.oksse.ServerSentEvent in project fitpay-android-sdk by fitpay.

the class UserEventStream method getListener.

private ServerSentEvent.Listener getListener() {
    return new ServerSentEvent.Listener() {

        private int counter = 0;

        @Override
        public void onOpen(ServerSentEvent sse, Response response) {
            FPLog.d(TAG, "connected to event stream for user " + user.getId());
            connected = true;
        }

        @Override
        public void onMessage(ServerSentEvent sse, String id, String event, String message) {
            lastEventTs = System.currentTimeMillis();
            String payload = StringUtils.getDecryptedString(KeysManager.KEY_API, message);
            Gson gson = Constants.getGson();
            UserStreamEvent fitpayEvent = gson.fromJson(payload, UserStreamEvent.class);
            FPLog.d(TAG, "sse onMessage " + user.getId() + " received: " + fitpayEvent);
            RxBus.getInstance().post(fitpayEvent);
        }

        @Override
        public void onComment(ServerSentEvent sse, String comment) {
            FPLog.d(TAG, "sse onComment: " + comment);
        }

        @Override
        public boolean onRetryTime(ServerSentEvent sse, long milliseconds) {
            FPLog.d(TAG, "sse onRetryTime: " + milliseconds);
            return true;
        }

        @Override
        public boolean onRetryError(ServerSentEvent sse, Throwable throwable, Response response) {
            FPLog.e(TAG, "sse onRetryTime: " + response);
            FPLog.e(TAG, throwable);
            if (++counter <= 5) {
                FPLog.d(TAG, "still within retry parameters: " + counter + ", retrying sse connection");
                return true;
            } else {
                FPLog.d(TAG, "outside the retry parameters: " + counter + ", retrying sse connection");
                return false;
            }
        }

        @Override
        public void onClosed(ServerSentEvent sse) {
            FPLog.d(TAG, "event stream for user " + user.getId() + " closed");
        }

        @Override
        public Request onPreRetry(ServerSentEvent sse, Request originalRequest) {
            return null;
        }
    };
}
Also used : Response(okhttp3.Response) ServerSentEvent(com.here.oksse.ServerSentEvent) Request(okhttp3.Request) Gson(com.google.gson.Gson) UserStreamEvent(com.fitpay.android.api.models.UserStreamEvent)

Aggregations

UserStreamEvent (com.fitpay.android.api.models.UserStreamEvent)1 Gson (com.google.gson.Gson)1 ServerSentEvent (com.here.oksse.ServerSentEvent)1 Request (okhttp3.Request)1 Response (okhttp3.Response)1