Skip to content

Instantly share code, notes, and snippets.

@diefferson
Last active September 27, 2017 14:57
Show Gist options
  • Save diefferson/584adfb46cefcb8b95d9ed6fc1c6ea3e to your computer and use it in GitHub Desktop.
Save diefferson/584adfb46cefcb8b95d9ed6fc1c6ea3e to your computer and use it in GitHub Desktop.
Websocket client with android
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);
}
}
}
@diefferson
Copy link
Author

diefferson commented Sep 26, 2017

Dependences

 //Gson
 compile 'com.google.code.gson:gson:2.3.0'

// Retrofit
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'com.squareup.retrofit2:converter-scalars:2.2.0'

// OkHttp
compile 'com.squareup.okhttp3:okhttp:3.8.1'
compile 'com.squareup.okhttp3:logging-interceptor:3.8.1'

// RxJava
compile 'io.reactivex.rxjava2:rxjava:2.1.3'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'com.jakewharton.rxrelay2:rxrelay:2.0.0'
///compile "com.github.akarnokd:rxjava2-interop:0.9.7"


compile "android.arch.lifecycle:runtime:1.0.0"
compile "android.arch.lifecycle:extensions:1.0.0-alpha9-1"
annotationProcessor "android.arch.lifecycle:compiler:1.0.0-alpha9-1"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment