2

我正在尝试连接到 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();
        }
    }
}

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

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

2 回答 2

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;
 }
于 2018-01-23T13:12:50.710 回答
-1

当您阅读API 文档时,您可以阅读:

推送 API

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

WAMP = Web应用程序消息传递协议_

所以你必须选择一个与 WAMP 协议兼容的 C# lib,你可以看看这里找到一个可兼容的实现。

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

于 2017-11-25T18:56:10.177 回答