示例#1
0
        private BinanceApiResult <BinanceStream> CreateSocket(string url)
        {
            try
            {
                var socket = SocketFactory.CreateWebsocket(url);

                var socketObject = new BinanceStream()
                {
                    Socket = socket, StreamId = NextStreamId()
                };
                socket.SetEnabledSslProtocols(protocols);
                socket.OnClose += (obj, args) => Socket_OnClose(socketObject, args);
                socket.OnError += (obj, args) => Socket_OnError(socketObject, args);
                socket.OnOpen  += (obj, args) => Socket_OnOpen(socketObject, args);
                socket.Connect();
                lock (sockets)
                    sockets.Add(socketObject);

                return(new BinanceApiResult <BinanceStream>()
                {
                    Data = socketObject, Success = true
                });
            }
            catch (Exception e)
            {
                var errorMessage = $"Couldn't open socket stream: {e.Message}";
                log.Write(LogVerbosity.Error, errorMessage);
                return(ThrowErrorMessage <BinanceStream>(BinanceErrors.GetError(BinanceErrorKey.CantConnectToServer), errorMessage));
            }
        }
        private async Task <CallResult <BinanceStream> > CreateSocket(string url)
        {
            try
            {
                var socket       = SocketFactory.CreateWebsocket(log, url);
                var socketObject = new BinanceStream()
                {
                    Socket = socket, StreamResult = new BinanceStreamSubscription()
                    {
                        StreamId = NextStreamId()
                    }
                };
                socket.SetEnabledSslProtocols(protocols);

                socket.OnClose += () => Socket_OnClose(socketObject);

                socket.OnError += Socket_OnError;
                socket.OnError += socketObject.StreamResult.InvokeError;

                socket.OnOpen += Socket_OnOpen;
                if (apiProxy != null)
                {
                    socket.SetProxy(apiProxy.Host, apiProxy.Port);
                }

                var connected = await socket.Connect().ConfigureAwait(false);

                if (!connected)
                {
                    log.Write(LogVerbosity.Error, "Couldn't open socket stream");
                    return(new CallResult <BinanceStream>(null, new CantConnectError()));
                }

                log.Write(LogVerbosity.Debug, "Socket connection established");

                lock (sockets)
                    sockets.Add(socketObject);
                return(new CallResult <BinanceStream>(socketObject, null));
            }
            catch (Exception e)
            {
                var errorMessage = $"Couldn't open socket stream: {e.Message}";
                log.Write(LogVerbosity.Error, errorMessage);
                return(new CallResult <BinanceStream>(null, new CantConnectError()));
            }
        }
示例#3
0
        private async Task <CallResult <BinanceStream> > ConnectSocket(string url)
        {
            try
            {
                var socket       = CreateSocket(url);
                var socketObject = new BinanceStream()
                {
                    Socket = socket, StreamResult = new BinanceStreamSubscription()
                    {
                        StreamId = NextStreamId()
                    }
                };
                socket.OnClose += () => Socket_OnClose(socketObject);
                socket.OnError += socketObject.StreamResult.InvokeError;

                var connected = await socket.Connect().ConfigureAwait(false);

                if (!connected)
                {
                    log.Write(LogVerbosity.Error, $"{socketObject.StreamResult.StreamId} Couldn't open socket stream");
                    return(new CallResult <BinanceStream>(null, new CantConnectError()));
                }

                socketObject.TryReconnect = true;
                log.Write(LogVerbosity.Debug, $"{socketObject.StreamResult.StreamId} Socket connection established");

                lock (sockets)
                    sockets.Add(socketObject);
                return(new CallResult <BinanceStream>(socketObject, null));
            }
            catch (Exception e)
            {
                var errorMessage = $"Couldn't open socket stream: {e.Message}";
                log.Write(LogVerbosity.Error, errorMessage);
                return(new CallResult <BinanceStream>(null, new CantConnectError()));
            }
        }