Can't connect to Poloniex

2019-07-13 00:30发布

问题:

I'm trying to connect to Poloniex. To connect, I'm using WynthShop and this code:

public class Program
{
    public static void Main(string[] args)
    {
        var channelFactory = new DefaultWampChannelFactory();
        var channel = channelFactory.CreateJsonChannel("wss://api.poloniex.com", "realm1");
        channel.Open().GetAwaiter().GetResult();
        var tickerSubject = channel.RealmProxy.Services.GetSubject("ticker");

        var cancellationTokenSource = new CancellationTokenSource();

        using (var subscr = tickerSubject.Subscribe(evt =>
        {
            var currencyPair = evt.Arguments[0].Deserialize<string>();
            var last = evt.Arguments[1].Deserialize<decimal>();
            Console.WriteLine($"Currencypair: {currencyPair}, Last: {last}");
        }))
        {
            Console.WriteLine("Press a key to exit");
            Console.ReadKey();
            cancellationTokenSource.Cancel();
        }
    }
}

But unfortunately I get the error "Unable to connect to the remote server". Maybe someone knows how to solve the problem with the connection. Also I tried to use the following code, but also fails to connect:

public class Program
{
    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();
    }
}

回答1:

I create the websocket myself specifying the SSL/TLS versions i can support

 public void Connect()
 {
        _channel = new DefaultWampChannelFactory().ConnectToRealm(_realm)
            .WebSocketTransport(prtcl => CreateWebSocket(prtcl, _url))
            .JsonSerialization()
            .Build();
        //.CreateJsonChannel(_url, _realm);
        //_channel.RealmProxy.

        _channel.RealmProxy.Monitor.ConnectionBroken += (sender, args) =>
        {
            _logger.WriteError(string.Format("ConnectionError -> From connection error event WssUrl : {0} , Realm : {1}", _url, _realm), new ArgumentException(args.Details != null ? args.Details.Message : "NO Details" ));
        };

         _channel.RealmProxy.Monitor.ConnectionError += (sender, args) =>
         {
             _logger.WriteError(string.Format("ConnectionError -> From connection error event WssUrl : {0} , Realm : {1}",_url,_realm), args.Exception);
         };

         _channel.RealmProxy.Monitor.ConnectionEstablished += (sender, args) =>
         {
             string info = string.Format("from ConnectionEstablished event -> WssUrl : {0} , Realm : {1}", _url, _realm);
             OnConnected(info);
         };

        _reconnector = new WampChannelReconnector(_channel, ConnectInternal);
        _reconnector.Start();
    }

    private static WebSocket CreateWebSocket(string subprotocolName, string serverAddress)
    {         
        WebSocket result = new WebSocket(serverAddress,
            subprotocolName, null, null, string.Empty, string.Empty, WebSocketVersion.None,null,SslProtocols.Ssl3 | SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12, 4096);

        result.AutoSendPingInterval = 10;

        return result;
 }


回答2:

When you read the API doc, you can read:

Push API

The best way to get public data updates on markets is via the push API, which pushes live ticker, order book, trade, and Trollbox updates over WebSockets using the WAMP protocol. In order to use the push API, connect to wss://api.poloniex.com and subscribe to the desired feed.

WAMP = The Web Application Messaging Protocol

So you will have to chose a C# lib compatible with WAMP protocol, you may take a look here to find an comptable implementation.

In your case you may use WampSharp which is compatible with C#



标签: c# poloniex