public static SseMessage ParseMessage(string input, out string remainder) { SseMessage message = new SseMessage(); var line = ParseLine(input, out remainder); //while (!(line == null || line == "")) //while (line != null && line != "") while (!string.IsNullOrEmpty(line)) { if (line.StartsWith("data:")) { if (message.Data != null) { message.Data += "\r\n"; } const string removestring = "data:"; message.Data += (line.Substring(0 + removestring.Length)).Trim(); } else if (line.StartsWith("event:")) { const string removestring = "event:"; message.Event = (line.Substring(0 + removestring.Length)).Trim(); } else if (line.StartsWith("id:")) { const string removestring = "id:"; message.Id = line.Substring(0 + removestring.Length).Trim(); } line = ParseLine(remainder, out remainder); } return(message); }
static void Main(string[] args) { var path = @".\data\test.txt"; try { var remainder = ""; SseMessage message = new SseMessage(); message = Parser.ParseFile(path, out remainder); Console.WriteLine("***Parsing file located at {0} ***", path); // Could or should this be rewritten with either null conditionals or different form -> ? : if (message.Id != null) //Is there a benefit to this? -> !string.IsNullOrEmpty(message.Id)) { Console.WriteLine("Message ID = {0}", message.Id); } if (message.Event != null) { Console.WriteLine("Message Event = {0}", message.Event); } if (message.Data != null) { Console.WriteLine("Message Data = {0}", message.Data); } Console.WriteLine("Remainder = {0}", remainder); } catch (FileNotFoundException e) { Console.WriteLine("Could not find: {0}", e.FileName); } }
public static SseMessage ParseFile(string path, out string remainder) { var filemessage = File.ReadAllText(path); SseMessage message = new SseMessage(); message = Parser.ParseMessage(filemessage, out remainder); return(message); }