Esempio n. 1
0
        /// <summary>
        /// Send a JSON object followed by a newline without closing the connection.
        /// Used by the continuous mode of _changes and _active_tasks.
        /// </summary>
        /// <returns><c>true</c>, if data was written, <c>false</c> otherwise.</returns>
        /// <param name="changesDict">The metadata dictionary to write</param>
        /// <param name="mode">The mode of the response (event source vs continuous)</param>
        public bool SendContinuousLine(IDictionary <string, object> changesDict, ChangesFeedMode mode)
        {
            if (!Chunked)
            {
                Log.To.Router.W(TAG, "Attempt to send streaming data when not in chunked mode");
                return(false);
            }

            var json = Manager.GetObjectMapper().WriteValueAsBytes(changesDict).ToList();

            if (mode == ChangesFeedMode.EventSource)
            {
                // https://developer.mozilla.org/en-US/docs/Server-sent_events/Using_server-sent_events#Event_stream_format
                json.InsertRange(0, Encoding.UTF8.GetBytes("data: "));
                json.AddRange(Encoding.UTF8.GetBytes("\n\n"));
            }
            else
            {
                json.AddRange(Encoding.UTF8.GetBytes("\n"));
            }

            Log.To.Router.V(TAG, "Sending continous change chunk: {0}", new SecureLogJsonString(changesDict,
                                                                                                LogMessageSensitivity.PotentiallyInsecure));
            bool written = WriteData(json, false);

            return(written);
        }
        /// <summary>
        /// Send a JSON object followed by a newline without closing the connection.
        /// Used by the continuous mode of _changes and _active_tasks.
        /// </summary>
        /// <returns><c>true</c>, if data was written, <c>false</c> otherwise.</returns>
        /// <param name="changesDict">The metadata dictionary to write</param>
        /// <param name="mode">The mode of the response (event source vs continuous)</param>
        public bool SendContinuousLine(IDictionary<string, object> changesDict, ChangesFeedMode mode)
        {
            if (!Chunked) {
                Log.W(TAG, "Attempt to send streaming data when not in chunked mode");
                return false;
            }

            var json = Manager.GetObjectMapper().WriteValueAsBytes(changesDict).ToList();
            if (mode == ChangesFeedMode.EventSource) {
                // https://developer.mozilla.org/en-US/docs/Server-sent_events/Using_server-sent_events#Event_stream_format
                json.InsertRange(0, Encoding.UTF8.GetBytes("data: "));
                json.AddRange(Encoding.UTF8.GetBytes("\n\n"));
            } else {
                json.AddRange(Encoding.UTF8.GetBytes("\n"));
            }

            bool written = WriteData(json, false);
            return written;
        }