How to use Jawampa (Java WAMP implementation) to subcribe to an event

て烟熏妆下的殇ゞ 提交于 2019-12-09 13:44:24

问题


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.



来源:https://stackoverflow.com/questions/39330283/how-to-use-jawampa-java-wamp-implementation-to-subcribe-to-an-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!