using System; using System.IO; using System.Net; using System.Text; using System.Collections.Generic; public class SSEvent { public string Name { get; set; } public string Data { get; set; } } public class Application { public static List Queue = new List(1024); public static void Main() { Console.Write("Hello World\n"); Console.Write("Attempting to open stream\n"); var response = Application.OpenSSEStream("https://api.spark.io/v1/events?access_token="); Console.Write("Success! \n"); } public static Stream OpenSSEStream(string url) { /* Optionally ignore certificate errors ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications); */ var request = WebRequest.Create( new Uri(url)); ((HttpWebRequest)request).AllowReadStreamBuffering = false; var response = request.GetResponse(); var stream = response.GetResponseStream(); Application.ReadStreamForever(stream); return stream; } public static void ReadStreamForever(Stream stream) { var encoder = new UTF8Encoding(); var buffer = new byte[2048]; while(true) { //TODO: Better evented handling of the response stream if (stream.CanRead) { int len = stream.Read(buffer, 0, 2048); if (len > 0) { var text = encoder.GetString(buffer, 0, len); Application.Push(text); } } //System.Threading.Thread.Sleep(250); } } public static void Push(string text) { if (String.IsNullOrWhiteSpace(text)) { return; } var lines = text.Trim().Split('\n'); Application.Queue.AddRange(lines); if (text.Contains("data:")) { Application.ProcessLines(); } } public static void ProcessLines() { var lines = Application.Queue; SSEvent lastEvent = null; int index = 0; int lastEventIdx = -1; for(int i=0;i= 0) { lines.RemoveRange(0, lastEventIdx); } } /* Optionally ignore certificate errors */ public bool AcceptALlCertifications(object sender, System.Security.Cryptography.X509Certificates.X509Certificate cert, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors errors) { return true; } }