Last active
September 27, 2017 14:57
-
-
Save diefferson/584adfb46cefcb8b95d9ed6fc1c6ea3e to your computer and use it in GitHub Desktop.
Websocket client with android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.disapps.sockettest.websocket; | |
import android.arch.lifecycle.LiveData; | |
import android.arch.lifecycle.MutableLiveData; | |
import android.util.Log; | |
import com.google.gson.Gson; | |
import com.google.gson.JsonElement; | |
import com.google.gson.JsonParser; | |
import com.jakewharton.rxrelay2.PublishRelay; | |
import br.com.disapps.sockettest.Message; | |
import io.reactivex.Observable; | |
import okhttp3.OkHttpClient; | |
import okhttp3.Request; | |
import okhttp3.Response; | |
import okhttp3.WebSocket; | |
import okhttp3.WebSocketListener; | |
import okio.ByteString; | |
public class WebSocketClient { | |
public static final int CLOSE_SOCKET_SUCCESS = 1001; | |
private OkHttpClient okHttpClient; | |
private Request request; | |
private Gson gson; | |
private WebSocket webSocket; | |
private PublishRelay<String> messageRelay = PublishRelay.create(); | |
private MutableLiveData<Message> eventLiveData = new MutableLiveData<>(); | |
private boolean connected; | |
private WebSocketClient(Builder builder){ | |
this.okHttpClient = builder.okHttpClient; | |
this.gson = builder.gson; | |
this.request = builder.request; | |
this.webSocket = okHttpClient.newWebSocket(request, webSocketListener); | |
} | |
public <T> Observable<Object> observeMessages(String filter, boolean isArray, Class<T> typeKey) { | |
return messageRelay | |
.filter(message ->{ | |
JsonElement element = new JsonParser().parse(message); | |
if(isArray){ | |
return element.getAsJsonObject() | |
.getAsJsonArray(filter) != null; | |
}else{ | |
return element.getAsJsonObject() | |
.getAsJsonObject(filter) != null; | |
} | |
}) | |
.map(message -> { | |
T response = gson.fromJson(message, typeKey); | |
return response; | |
}); | |
} | |
public void disconnect() { | |
webSocket.close(CLOSE_SOCKET_SUCCESS, "SUCCESS"); | |
} | |
public LiveData<Message> websocketEvents() { | |
return eventLiveData; | |
} | |
private WebSocketListener webSocketListener = new WebSocketListener() { | |
@Override | |
public void onOpen(WebSocket webSocket, Response response) { | |
Log.d("LOL WebSocket", "onOpen"); | |
} | |
@Override | |
public void onMessage(WebSocket webSocket, String text) { | |
Log.d("LOL WebSocket", "receive: " + text); | |
messageRelay.accept(text); | |
} | |
@Override | |
public void onMessage(WebSocket webSocket, ByteString bytes) { | |
} | |
@Override | |
public void onClosing(WebSocket webSocket, int code, String reason) { | |
Log.d("LOL WebSocket", "onClosing[code=" + code + ", reason=" + reason + "]"); | |
connected = false; | |
WebSocketClient.this.webSocket = null; | |
} | |
@Override | |
public void onClosed(WebSocket webSocket, int code, String reason) { | |
Log.d("LOL WebSocket", "onClosed[code=" + code + ", reason=" + reason + "]"); | |
connected = false; | |
WebSocketClient.this.webSocket = null; | |
} | |
@Override | |
public void onFailure(WebSocket webSocket, Throwable t, Response response) { | |
Log.d("LOL WebSocket", "onFailure"+t.getMessage()); | |
connected = false; | |
WebSocketClient.this.webSocket = null; | |
} | |
}; | |
public boolean isConnected() { | |
return connected; | |
} | |
public void setConnected() { | |
this.connected = true; | |
} | |
public static class Builder { | |
private OkHttpClient okHttpClient; | |
private Request request; | |
private Gson gson; | |
public Builder okHttpClient(OkHttpClient okHttpClient) { | |
this.okHttpClient = okHttpClient; | |
return this; | |
} | |
public Builder gson(Gson gson) { | |
this.gson = gson; | |
return this; | |
} | |
public Builder request(Request request) { | |
this.request = request; | |
return this; | |
} | |
public WebSocketClient build() { | |
if (okHttpClient == null) throw new IllegalStateException("okHttpClient == null"); | |
if (request == null) throw new IllegalStateException("request == null"); | |
if (gson == null) throw new IllegalStateException("gson == null"); | |
return new WebSocketClient(this); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Dependences