Exemple #1
0
        private void btnAuthorizeApplication_Click(object sender, EventArgs e)
        {
            var msgFactory = new OpenApiMessagesFactory();
            var msg        = msgFactory.CreateAppAuthorizationRequest(_clientId, _clientSecret);

            Transmit(msg);
        }
        public void OpenConnection()
        {
            isShutdown = false;

            MessageHandler?.Invoke("Opening API connection.");

            //Start the listener thread to listen for Proto messages from the server
            _tcpClient = new TcpClient(Config.ApiHost, Config.ApiPort);;
            _apiSocket = new SslStream(_tcpClient.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null);
            _apiSocket.AuthenticateAsClient(Config.ApiHost);

            StartListenThread();

            //use another thread to transmit a queue of Proto messages because there is a limit of 30 messages per second
            Thread transmitThread = new Thread(() =>
            {
                Thread.CurrentThread.IsBackground = true;
                try
                {
                    //A continuos loop to transmit messages from a queue
                    Transmit();
                }
                catch (Exception e)
                {
                    ErrorHandler?.Invoke("Transmitter throws exception: " + e);
                    isShutdown = true;
                    //try to reconnect
                    ConnectionLostHandler?.Invoke();
                }
            });


            //Connection Flow:
            //Authorise the App
            //For each user account if accountID not loaded request from API
            //For each user account authorise the account
            //For each user account if symbols not loaded request from API
            //For each user account subscribe to all symbols
            //Start the heartbeat time

            //Authorise the application - when message recieved go to beginConnection
            var msgFactory = new OpenApiMessagesFactory();
            var msg        = msgFactory.CreateAppAuthorizationRequest(Config.ClientId, Config.ClientSecret);

            _trasmitQueue.Enqueue(msg);

            transmitThread.Start();
        }