How to use Jawampa (Java WAMP implementation) to s

2019-04-01 19:22发布

问题:

i want to use the poloniex API. https://poloniex.com/support/api/

So far i made Jawampa ( https://github.com/Matthias247/jawampa ) running with IntelliJ.

My first Question is, how to login successfuly? (The Docu of Jawampa doesnt help)

I got a API Key and a Secret. Which functions i have to use in the builder of Jawampa:

withRealm withRoles withConnectorProvider withConnectionConfiguration withSerializations withStrictUriValidation withAuthId withAuthMethod withObjectMapper

I have so far this code

     try {
        WampClientBuilder builder = new WampClientBuilder();
        builder.withConnectorProvider(connectorProvider)
                .withUri("wss://api.poloniex.com")
                .withAuthId("APIKEY")
                .withRealm("realm2")
                .withInfiniteReconnects()
                .withReconnectInterval(1, TimeUnit.SECONDS);
        client1 = builder.build();
    } catch (Exception e) {
        e.printStackTrace();
        return;
    }

Is wss://api.poloniex.com correct or should i use wss://api.poloniex.com/returnTicker for that client?

Do I have to make always a new client for every URI?

Thank you so much in advance.

回答1:

  1. My first Question is, how to login successfuly?

You don't have to authenticate to access Poloniex Push API via WAMP protocol. Push API methods are public, so you don't have to supply the API key and secret. Just connect to wss://api.poloniex.com and subscribe to a desired feed (Ticker, Order Book and Trades, Trollbox).

Btw, you need to supply the API Key only with Trading API methods. And the Secret is used to sign a POST data.

  1. Which functions i have to use in the builder of Jawampa:

This is how you connect to the Push API:

    WampClient client;
    try {
        WampClientBuilder builder = new WampClientBuilder();
        IWampConnectorProvider connectorProvider = new NettyWampClientConnectorProvider();
        builder.withConnectorProvider(connectorProvider)
                .withUri("wss://api.poloniex.com")
                .withRealm("realm1")
                .withInfiniteReconnects()
                .withReconnectInterval(5, TimeUnit.SECONDS);
        client = builder.build();

    } catch (Exception e) {
        return;
    }

Once your client is connected, you subscribe to a feed like this:

    client.statusChanged().subscribe(new Action1<WampClient.State>() {
        @Override
        public void call(WampClient.State t1) {
            if (t1 instanceof WampClient.ConnectedState) {
                subscription = client.makeSubscription("trollbox")
                        .subscribe((s) -> { System.out.println(s.arguments()); }
            }
        }
    });
    client.open();
  1. Is wss://api.poloniex.com correct or should i use wss://api.poloniex.com/returnTicker for that client?

wss://api.poloniex.com is correct. Besides, returnTicker belongs to the Public API and is accessed via HTTP GET requests.

  1. Do I have to make always a new client for every URI?

In respect to the Push API, once you connected a client to wss://api.poloniex.com, you can use this client to make subscriptions to multiple feeds. For example:

client.statusChanged().subscribe(new Action1<WampClient.State>() {
        @Override
        public void call(WampClient.State t1) {
            if (t1 instanceof WampClient.ConnectedState) {
                client.makeSubscription("trollbox")
                        .subscribe((s) -> { System.out.println(s.arguments()); });
                client.makeSubscription("ticker")
                        .subscribe((s) -> { System.out.println(s.arguments()); });
            }
        }
    });

However, according to Jawampa Docs:

After a WampClient was closed it can not be reopened again. Instead of this a new instance of the WampClient should be created if necessary.