Skip to content

Instantly share code, notes, and snippets.

@coinscious-io
Last active August 2, 2019 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coinscious-io/77395e40dfa44dfee86393a3c3f4c06c to your computer and use it in GitHub Desktop.
Save coinscious-io/77395e40dfa44dfee86393a3c3f4c06c to your computer and use it in GitHub Desktop.
Subscribing to live stream trade history data
import okhttp3.Dispatcher;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.WebSocket;
import okhttp3.WebSocketListener;
import java.util.concurrent.TimeUnit;
public class Example {
public static void main(String[] args) {
Dispatcher dispatcher = new Dispatcher();
OkHttpClient httpClient = new OkHttpClient.Builder()
.pingInterval(60, TimeUnit.SECONDS)
.dispatcher(dispatcher)
.build();
Request request = new Request.Builder()
.url("wss://livestream.coinscious.org/live")
.addHeader("Authorization", "Bearer <token>")
.build();
WebSocket webSocket = httpClient.newWebSocket(request, new WebSocketListener() {
@Override
public void onMessage(WebSocket webSocket, String text) {
// TODO your code here
}
@Override
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
// TODO your code here
}
@Override
public void onClosing(WebSocket webSocket, int code, String reason) {
// TODO your code here
}
});
webSocket.send("{\"subscribe\":\"trade\",\"msg\":{\"exchange\":\"binance\",\"pair\":\"btc_usdt\"}}");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment