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

牧云@^-^@ 提交于 2019-12-03 21:34:55
  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.

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