如何使用Jawampa(Java WAMP实现)来订阅事件
我想使用poloniex API。 https://poloniex.com/support/api/
到目前为止,我使用IntelliJ运行Jawampa( https://github.com/Matthias247/jawampa )。
我的第一个问题是,如何成功登录? (Jawampa的文件没有帮助)
我有一个API密钥和一个秘密。 我必须在Jawampa的建造者中使用哪些function:
withRealm withRoles withConnectorProvider withConnectionConfiguration withSerializations withStrictUriValidation withAuthId withAuthMethod withObjectMapper
我到目前为止这个代码
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; }
wss://api.poloniex.com是正确的还是我应该使用wss://api.poloniex.com/returnTicker为该客户端?
我是否必须为每个URI创建一个新的客户端?
非常感谢你提前。
- 我的第一个问题是,如何成功登录?
您无需通过WAMP协议进行身份validation即可访问Poloniex Push API。 Push API方法是公共的,因此您不必提供API密钥和密钥。 只需连接到wss://api.poloniex.com并订阅所需的订阅源(Ticker,Order Book和Trades,Trollbox)。
顺便说一句,您只需要使用Trading API方法提供API密钥。 秘密用于签署POST数据。
- 我必须在Jawampa的建造者中使用哪些function:
这是您连接到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; }
连接客户端后,您订阅了以下Feed:
client.statusChanged().subscribe(new Action1() { @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();
- wss://api.poloniex.com是正确的还是我应该使用wss://api.poloniex.com/returnTicker为该客户端?
wss://api.poloniex.com是正确的。 此外,returnTicker属于Public API,可通过HTTP GET请求进行访问。
- 我是否必须为每个URI创建一个新的客户端?
对于Push API,将客户端连接到wss://api.poloniex.com后,您可以使用此客户端订阅多个订阅源。 例如:
client.statusChanged().subscribe(new Action1() { @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()); }); } } });
但是,根据Jawampa Docs的说法:
WampClient关闭后,无法再次重新打开。 如果需要,应该创建一个新的WampClient实例。