HTTP 502 bad gateway c# with wamp on poloniex

777 views Asked by At

I want to retrieve ticks from Poloniex in real time. They use wamp for that. I installed via nugget WampSharp and found this code :

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

but no matter at the await channel.open() I have the following error : HHTP 502 bad gateway

Do you have an idea where is the problem

thank you in advance

2

There are 2 answers

2
darkl On BEST ANSWER

The Poloniex service seems not to be able to handle so many connections. That's why you get the HTTP 502 bad gateway error. You can try to use the reconnector mechanism in order to try connecting periodically.

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

This is based on this code sample.

1
KyloRen On

Get rid of the Console.WriteLine it is interfering with your code.