コード例 #1
0
        private void OnMessageReceived(BestHTTP.ServerSentEvents.Message message)
        {
            if (this.State >= States.Closing)
            {
                return;
            }

            // 1.) Set the last event ID string of the event source to value of the last event ID buffer.
            // The buffer does not get reset, so the last event ID string of the event source remains set to this value until the next time it is set by the server.
            // We check here only for null, because it can be a non-null but empty string.
            if (message.Id != null)
            {
                this.LastEventId = message.Id;
            }

            if (message.Retry.TotalMilliseconds > 0)
            {
                this.ReconnectionTime = message.Retry;
            }

            // 2.) If the data buffer is an empty string, set the data buffer and the event type buffer to the empty string and abort these steps.
            if (string.IsNullOrEmpty(message.Data))
            {
                return;
            }

            // 3.) If the data buffer's last character is a U+000A LINE FEED (LF) character, then remove the last character from the data buffer.
            // This step can be ignored. We constructed the string to be able to skip this step.

            if (OnMessage != null)
            {
                try
                {
                    OnMessage(this, message);
                }
                catch (Exception ex)
                {
                    HTTPManager.Logger.Exception("EventSource", "OnMessageReceived - OnMessage", ex);
                }
            }

            if (EventTable != null && !string.IsNullOrEmpty(message.Event))
            {
                OnEventDelegate action;
                if (EventTable.TryGetValue(message.Event, out action))
                {
                    if (action != null)
                    {
                        try
                        {
                            action(this, message);
                        }
                        catch (Exception ex)
                        {
                            HTTPManager.Logger.Exception("EventSource", "OnMessageReceived - action", ex);
                        }
                    }
                }
            }
        }
コード例 #2
0
        static void OnMessageCallback(uint id, string eventStr, string data, string eventId, int retry)
        {
            EventSource es;

            if (EventSources.TryGetValue(id, out es))
            {
                var msg = new BestHTTP.ServerSentEvents.Message();
                msg.Id    = eventId;
                msg.Data  = data;
                msg.Event = eventStr;
                msg.Retry = TimeSpan.FromSeconds(retry);

                es.OnMessageReceived(msg);
            }
        }
コード例 #3
0
        void ParseLine(byte[] buffer, int count)
        {
            // If the line is empty (a blank line) => Dispatch the event
            if (count == 0)
            {
                if (CurrentMessage != null)
                {
                    lock (FrameLock)
                        CompletedMessages.Add(CurrentMessage);
                    CurrentMessage = null;
                }

                return;
            }

            // If the line starts with a U+003A COLON character (:) => Ignore the line.
            if (buffer[0] == 0x3A)
            {
                return;
            }

            //If the line contains a U+003A COLON character (:)
            int colonIdx = -1;

            for (int i = 0; i < count && colonIdx == -1; ++i)
            {
                if (buffer[i] == 0x3A)
                {
                    colonIdx = i;
                }
            }

            string field;
            string value;

            if (colonIdx != -1)
            {
                // Collect the characters on the line before the first U+003A COLON character (:), and let field be that string.
                field = Encoding.UTF8.GetString(buffer, 0, colonIdx);

                //Collect the characters on the line after the first U+003A COLON character (:), and let value be that string. If value starts with a U+0020 SPACE character, remove it from value.
                if (colonIdx + 1 < count && buffer[colonIdx + 1] == 0x20)
                {
                    colonIdx++;
                }

                colonIdx++;

                // discarded because it is not followed by a blank line
                if (colonIdx >= count)
                {
                    return;
                }

                value = Encoding.UTF8.GetString(buffer, colonIdx, count - colonIdx);
            }
            else
            {
                // Otherwise, the string is not empty but does not contain a U+003A COLON character (:) =>
                //      Process the field using the whole line as the field name, and the empty string as the field value.
                field = Encoding.UTF8.GetString(buffer, 0, count);
                value = string.Empty;
            }

            if (CurrentMessage == null)
            {
                CurrentMessage = new BestHTTP.ServerSentEvents.Message();
            }

            switch (field)
            {
            // If the field name is "id" => Set the last event ID buffer to the field value.
            case "id":
                CurrentMessage.Id = value;
                break;

            // If the field name is "event" => Set the event type buffer to field value.
            case "event":
                CurrentMessage.Event = value;
                break;

            // If the field name is "data" => Append the field value to the data buffer, then append a single U+000A LINE FEED (LF) character to the data buffer.
            case "data":
                // Append a new line if we already have some data. This way we can skip step 3.) in the EventSource's OnMessageReceived.
                // We do only null check, becouse empty string can be valid payload
                if (CurrentMessage.Data != null)
                {
                    CurrentMessage.Data += Environment.NewLine;
                }

                CurrentMessage.Data += value;
                break;

            // If the field name is "retry" => If the field value consists of only ASCII digits, then interpret the field value as an integer in base ten,
            //  and set the event stream's reconnection time to that integer. Otherwise, ignore the field.
            case "retry":
                int result;
                if (int.TryParse(value, out result))
                {
                    CurrentMessage.Retry = TimeSpan.FromMilliseconds(result);
                }
                break;

            // Otherwise: The field is ignored.
            default:
                break;
            }
        }
コード例 #4
0
        void ParseLine(byte[] buffer, int count)
        {
            // If the line is empty (a blank line) => Dispatch the event
            if (count == 0)
            {
                if (CurrentMessage != null)
                {
                    lock (FrameLock)
                        CompletedMessages.Add(CurrentMessage);
                    CurrentMessage = null;
                }

                return;
            }

            // If the line starts with a U+003A COLON character (:) => Ignore the line.
            if (buffer[0] == 0x3A)
                return;

            //If the line contains a U+003A COLON character (:)
            int colonIdx = -1;
            for (int i = 0; i < count && colonIdx == -1; ++i)
                if (buffer[i] == 0x3A)
                    colonIdx = i;

            string field;
            string value;

            if (colonIdx != -1)
            {
                // Collect the characters on the line before the first U+003A COLON character (:), and let field be that string.
                field = Encoding.UTF8.GetString(buffer, 0, colonIdx);

                //Collect the characters on the line after the first U+003A COLON character (:), and let value be that string. If value starts with a U+0020 SPACE character, remove it from value.
                if (colonIdx + 1 < count && buffer[colonIdx + 1] == 0x20)
                    colonIdx++;

                colonIdx++;

                // discarded because it is not followed by a blank line
                if (colonIdx >= count)
                    return;

                value = Encoding.UTF8.GetString(buffer, colonIdx, count - colonIdx);
            }
            else
            {
                // Otherwise, the string is not empty but does not contain a U+003A COLON character (:) =>
                //      Process the field using the whole line as the field name, and the empty string as the field value.
                field = Encoding.UTF8.GetString(buffer, 0, count);
                value = string.Empty;
            }

            if (CurrentMessage == null)
                CurrentMessage = new BestHTTP.ServerSentEvents.Message();

            switch(field)
            {
                // If the field name is "id" => Set the last event ID buffer to the field value.
                case "id":
                    CurrentMessage.Id = value;
                    break;

                // If the field name is "event" => Set the event type buffer to field value.
                case "event":
                    CurrentMessage.Event = value;
                    break;

                // If the field name is "data" => Append the field value to the data buffer, then append a single U+000A LINE FEED (LF) character to the data buffer.
                case "data":
                    // Append a new line if we already have some data. This way we can skip step 3.) in the EventSource's OnMessageReceived.
                    // We do only null check, becouse empty string can be valid payload
                    if (CurrentMessage.Data != null)
                        CurrentMessage.Data += Environment.NewLine;

                    CurrentMessage.Data += value;
                    break;

                // If the field name is "retry" => If the field value consists of only ASCII digits, then interpret the field value as an integer in base ten,
                //  and set the event stream's reconnection time to that integer. Otherwise, ignore the field.
                case "retry":
                    int result;
                    if (int.TryParse(value, out result))
                        CurrentMessage.Retry = TimeSpan.FromMilliseconds(result);
                    break;

                // Otherwise: The field is ignored.
                default:
                    break;
            }
        }
コード例 #5
0
        static void OnMessageCallback(uint id, string eventStr, string data, string eventId, int retry)
        {
            EventSource es;
            if (EventSources.TryGetValue(id, out es))
            {
                var msg = new BestHTTP.ServerSentEvents.Message();
                msg.Id = eventId;
                msg.Data = data;
                msg.Event = eventStr;
                msg.Retry = TimeSpan.FromSeconds(retry);

                es.OnMessageReceived(msg);
            }
        }