无法连接到Poloniex

时间:2017-11-06 10:37:25

标签: c# poloniex

我试图连接到Poloniex。要连接,我使用WynthShop和此代码:

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

但不幸的是我收到错误&#34;无法连接到远程服务器&#34;。 也许有人知道如何解决连接问题。此外,我尝试使用以下代码,但也无法连接:

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

2 个答案:

答案 0 :(得分:0)

我自己创建了websocket,指定了我可以支持的SSL / TLS版本

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

答案 1 :(得分:-1)

当您阅读API doc时,您可以阅读:

  

推送API

     

在市场上获取公共数据更新的最佳方式是通过推送API,它使用WAMP协议通过WebSockets 推送实时自动收报机,订单簿,交易和Trollbox更新。要使用推送API,请连接到wss://api.poloniex.com并订阅所需的订阅源。

WAMP = W eb A 应用 M essaging P rotocol

因此,您必须选择与WAMP协议兼容的C#lib,您可以使用look here来查找可编译的实现。

在您的情况下,您可以使用与C#兼容的WampSharp