【问题标题】:How to use Jawampa (Java WAMP implementation) to subcribe to an event如何使用 Jawampa(Java WAMP 实现)订阅事件
【发布时间】:2017-01-12 19:20:51
【问题描述】:

我想使用 poloniex API。 https://poloniex.com/support/api/

到目前为止,我让 Jawampa (https://github.com/Matthias247/jawampa) 与 IntelliJ 一起运行。

我的第一个问题是,如何成功登录? (Jawampa 的文档没有帮助)

我得到了一个 API 密钥和一个秘密。我必须在 Jawampa 的构建器中使用哪些功能:

withRealm withRoles withConnectorProvider withConnectionConfiguration 带有序列化 withStrictUriValidation withAuthId withAuth方法 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 创建一个新客户端?

非常感谢您。

【问题讨论】:

    标签: java wamp wamp-protocol


    【解决方案1】:
    1. 我的第一个问题是,如何成功登录?

    您无需进行身份验证即可通过 WAMP 协议访问 Poloniex Push API。推送 API 方法是公开的,因此您不必提供 API 密钥和秘密。只需连接到 wss://api.poloniex.com 并订阅所需的订阅源(Ticker、Order Book and Trades、Trollbox)。

    顺便说一句,您只需要为交易 API 方法提供 API 密钥。 Secret 用于对 POST 数据进行签名。

    1. 我必须在 Jawampa 的构建器中使用哪些功能:

    这是连接到 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;
        }
    

    连接您的客户端后,您可以像这样订阅一个供稿:

        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. wss://api.poloniex.com 是否正确或我应该使用 该客户端的 wss://api.poloniex.com/returnTicker?

    wss://api.poloniex.com 是正确的。此外,returnTicker 属于 Public API,通过 HTTP GET 请求访问。

    1. 我是否必须始终为每个 URI 创建一个新客户端?

    关于 Push API,一旦您将客户端连接到 wss://api.poloniex.com,您就可以使用该客户端订阅多个订阅源。例如:

    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()); });
                }
            }
        });
    

    然而,根据 Jawampa Docs:

    WampClient 关闭后无法再次重新打开。如果需要,应该创建一个新的 WampClient 实例,而不是这个。

    【讨论】:

      猜你喜欢
      • 2019-12-01
      • 2018-12-09
      • 1970-01-01
      • 2021-01-23
      • 2019-05-22
      • 1970-01-01
      • 2020-01-24
      • 2019-10-05
      • 2013-11-03
      相关资源
      最近更新 更多