Exemple #1
0
        public ChangesResult LongPoll(ChangesQuery query = null)
        {
            query      = query ?? new ChangesQuery();
            query.Feed = ChangesQuery.FeedType.longpoll;

            return(Get(query));
        }
Exemple #2
0
        public ChangesResult Poll(ChangesQuery query = null)
        {
            query      = query ?? new ChangesQuery();
            query.Feed = ChangesQuery.FeedType.normal;

            return(Get(query));
        }
Exemple #3
0
        private ChangesResult Get(ChangesQuery query)
        {
            string path = "_changes";

            var responseContent = session.Get(path, query.ToDictionary());

            return(responseContent.DeserializeObject <ChangesResult>());
        }
Exemple #4
0
        public void Stream(Action <string> onLine, ChangesQuery query = null, Action <KeyValuePair <string, string> > onHeader = null)
        {
            if (onLine == null)
            {
                throw new ArgumentNullException("onLine");
            }

            query      = query ?? new ChangesQuery();
            query.Feed = ChangesQuery.FeedType.continuous;

            var sb = new StringBuilder();

            sb.AppendFormat("GET /{0}/_changes{1} HTTP/1.1\r\n", session.DatabaseName, session.ToQueryString(query.ToDictionary()));
            sb.AppendFormat("Host: {0}\r\n", session.baseUri.Host);
            sb.AppendFormat("Connection: keep-alive\r\n");
            sb.AppendFormat("\r\n");

            using (var client = new TcpClient())
            {
                client.ReceiveTimeout = query.Timeout ?? 60000;
                client.NoDelay        = true;
                client.Connect(session.baseUri.Host, session.baseUri.Port);

                using (var stream = client.GetStream())
                {
                    using (var writer = new StreamWriter(stream))
                    {
                        writer.Write(sb.ToString());
                        writer.Flush();

                        using (var reader = new StreamReader(stream))
                        {
                            bool headersRead    = false;
                            bool dataLengthRead = false;

                            while (!reader.EndOfStream)
                            {
                                var line = reader.ReadLine();


                                if (line.StartsWith("HTTP/") && !headersRead)
                                {
                                    continue;
                                }

                                if (line == "" && !headersRead)
                                {
                                    headersRead = true;
                                    continue;
                                }

                                if (String.IsNullOrWhiteSpace(line))
                                {
                                    continue;
                                }

                                if (!headersRead)
                                {
                                    var split = line.Split(':');

                                    if (split.Length > 1)
                                    {
                                        if (onHeader != null)
                                        {
                                            onHeader(new KeyValuePair <string, string>(split[0], split[1]));
                                        }
                                        continue;
                                    }
                                }

                                if (!dataLengthRead)
                                {
                                    dataLengthRead = true;
                                    continue;
                                }

                                onLine(line);

                                dataLengthRead = false;
                            }
                        }
                    }
                }
            }
        }