Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP 502 bad gateway c# with wamp on poloniex

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

like image 757
ronki Avatar asked Jan 29 '26 06:01

ronki


1 Answers

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.

like image 144
darkl Avatar answered Jan 30 '26 21:01

darkl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!