3

我想从 Poloniex 实时检索报价。他们为此使用 wamp。我通过 nugget WampSharp 安装并找到了这段代码:

  static async void MainAsync(string[] args)
    {

        var channelFactory = new DefaultWampChannelFactory();
        var channel = channelFactory.CreateMsgpackChannel("wss://api.poloniex.com", "realm1");
        await channel.Open();

        var realmProxy = channel.RealmProxy;

        Console.WriteLine("Connection established");

        int received = 0;
        IDisposable subscription = null;

        subscription =
            realmProxy.Services.GetSubject("ticker")
                      .Subscribe(x =>
                      {
                          Console.WriteLine("Got Event: " + x);

                          received++;

                          if (received > 5)
                          {
                              Console.WriteLine("Closing ..");
                              subscription.Dispose();
                          }
                      });

        Console.ReadLine();
    }

但无论在 await channel.open() 我有以下错误:HHTP 502 bad gateway

你知道问题出在哪里吗

先感谢您

4

2 回答 2

2

Poloniex 服务似乎无法处理这么多的连接。这就是您收到 HTTP 502 bad gateway 错误的原因。您可以尝试使用重新连接器机制来尝试定期连接。

static void Main(string[] args)
{
    var channelFactory = new DefaultWampChannelFactory();
    var channel = channelFactory.CreateJsonChannel("wss://api.poloniex.com", "realm1");

    Func<Task> connect = async () =>
    {
        await Task.Delay(30000);

        await channel.Open();

        var tickerSubject = channel.RealmProxy.Services.GetSubject("ticker");

        var subscription = tickerSubject.Subscribe(evt =>
        {
            var currencyPair = evt.Arguments[0].Deserialize<string>();
            var last = evt.Arguments[1].Deserialize<decimal>();
            Console.WriteLine($"Currencypair: {currencyPair}, Last: {last}");
        },
        ex => {
            Console.WriteLine($"Oh no! {ex}");
        });
    };

    WampChannelReconnector reconnector =
        new WampChannelReconnector(channel, connect);

    reconnector.Start();

    Console.WriteLine("Press a key to exit");
    Console.ReadKey();
}

这是基于代码示例。

于 2017-09-13T02:26:38.840 回答
0

摆脱Console.WriteLine它正在干扰您的代码。

于 2017-09-09T13:34:26.497 回答