if ((!this.orderFeed.isEmpty() || this.subscribeAccountInfo) && (this.accessKey == null || this.secretKey == null)) {
throw new IllegalArgumentException("Access key and secret key are required to subscribe order feed and account info.");
}
final Socket socket;
try {
socket = IO.socket(uri, opt);
} catch (URISyntaxException e) {
throw new IllegalArgumentException(e);
}
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
subscribeMarketData();
subscribeGrouporder();
subscribePrivateData();
}
private void subscribeMarketData() {
subscribe("marketdata", marketData);
}
private void subscribeGrouporder() {
subscribe("grouporder", grouporder);
}
private void subscribe(String name, Iterable<CurrencyPair> currencyPairs) {
for (CurrencyPair currencyPair : currencyPairs) {
String market = toMarket(currencyPair);
String subscribeData = String.format("%s_%s", name, market);
log.debug("subscribing {}", subscribeData);
socket.emit("subscribe", subscribeData);
}
}
private void subscribePrivateData() {
final List<String> params = buildPrivateDataParams();
if (!params.isEmpty()) {
BTCChinaPayload payload = getPayload(params.toArray(new String[0]));
final List<String> arg = new ArrayList<String>(2);
arg.add(toPostData(payload));
arg.add(getSign(payload));
// Use 'private' method to subscribe the order feed
socket.emit("private", arg);
} else {
log.debug("No private data specified to subscribe.");
}
}