Ejemplo n.º 1
0
        public string macaroon; // used to call /v1/invoices/subscribe

        public async void Start()
        {
            using (var client = new HttpClient())
            {
                // The HTTP stream should stay open until closed by either server or client
                client.Timeout = TimeSpan.FromMilliseconds(Timeout.Infinite);

                var request = new HttpRequestMessage(HttpMethod.Get, url);
                request.Headers.Add("Grpc-Metadata-macaroon", macaroon);    // macaroon should be provided from lnd

//#if DEBUG
                // This disables SSL certificate validation
                ServicePointManager.ServerCertificateValidationCallback +=
                    (sender, cert, chain, sslPolicyErrors) => true;
//#endif
                using (var response = await client.SendAsync(
                           request,
                           HttpCompletionOption.ResponseHeadersRead))
                {
                    using (var body = await response.Content.ReadAsStreamAsync())
                        using (var reader = new StreamReader(body))
                        {
                            IsLive = true;
                            //need to read and chop message
                            try
                            {
                                while (!reader.EndOfStream)
                                {
                                    string       line = reader.ReadLine();
                                    InvoiceEvent e    = JsonSerializer.Deserialize <InvoiceEvent>(line);//SimpleJson.DeserializeObject<InvoiceEvent>(line);

                                    //Notify listeners of new invoice
                                    InvoicePaid?.Invoke(e.result);
                                }
                            }
                            catch (Exception e)
                            {
                                // TODO: check that the exception type is actually from a closed stream.
                                Debug.WriteLine(e.Message);
                                IsLive = false;
                                StreamLost?.Invoke(this);
                            }
                        }
                }
            }
        }