Esempio n. 1
0
        /// <summary>
        /// Parse the event from the text received over HTTP
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static SseEvent Parse(string text)
        {
            string[] lines = text.Split(
                new[] { "\r\n", "\r", "\n" },
                StringSplitOptions.RemoveEmptyEntries
                );

            var message = new SseEvent {
                data   = null,
                @event = null,
                id     = null,
                retry  = null
            };

            StringBuilder data = new StringBuilder();

            foreach (var line in lines)
            {
                if (line.StartsWith("id: "))
                {
                    message.id = int.Parse(line.Substring(4));
                }
                else if (line.StartsWith("event: "))
                {
                    message.@event = line.Substring(7);
                }
                else if (line.StartsWith("retry: "))
                {
                    message.retry = int.Parse(line.Substring(7));
                }
                else if (line.StartsWith("data: "))
                {
                    data.Append(line.Substring(6));
                }
            }

            message.data = data.ToString();

            if (!string.IsNullOrWhiteSpace(message.data))
            {
                message.jsonData = JsonReader.Parse(message.data).AsJsonObject;
            }

            return(message);
        }
Esempio n. 2
0
        private void HandleEvent(string raw)
        {
            var parsed = SseEvent.Parse(raw);

            eventHandler.Invoke(parsed);
        }