コード例 #1
0
ファイル: Program.cs プロジェクト: ousttrue/ReactiveWeb
        public void Request(Uri uri)
        {
            Console.WriteLine("############################################################");

            // request
            var request = new HttpRequest(uri);
            request.EnableEncoding();
            request.SetKeepAlive(true);
            Console.Write(request.ToString());

#if USE_BYTES_BODY
            using (var subject = new ChunkedHttpResponseObserver())
            {
                var body = new Subject<IList<Byte>>();
                body.Subscribe(x =>
                {
                    Console.WriteLine("body: " + x.Count + " bytes");
                }
                , ex =>
                {
                    Console.WriteLine(ex);
                }
                , () =>
                {
                    Console.WriteLine("body end");
                });
                subject.BodyObservable.Subscribe(body);

                bool keepAlive = true;
                subject.KeepAliveObservalbe
                    .Subscribe(x =>
                    {
                        // disconnect
                        keepAlive = x;
                    });
#else
            using (var subject = new HttpRawByteSubject())
            {
                var body = new Subject<Byte>();
                var buffer = new List<Byte>();
                body.Subscribe(x =>
                {
                    buffer.Add(x);
                }
                , ex =>
                {
                    Console.WriteLine(ex);
                }
                , () =>
                {
                    Console.WriteLine("body: " + buffer.Count + " bytes");
                    Console.WriteLine("body end");
                });
                subject.BodyObservable.Subscribe(body);
#endif

                // statusline
                subject.StatusObservable.Subscribe(x =>
                {
                    Console.WriteLine("status: " + x);
                });

                // response headers
                subject.HeaderObservable.Subscribe(x =>
                {
                    Console.WriteLine(x.Key + ": " + x.Value);
                }
                , () =>
                {
                    Console.WriteLine();
                }
                );

                ////////////////////////////
                // connect
                ////////////////////////////
                GetConnection(uri)
                    .Subscribe(x =>
                    {
                        // ready to read
                        subject.OnNext(x);

                        // send request
                        x.SendRequestObservable(request)
                        .Subscribe(_ =>
                        {

                        });
                    }
                    , subject.OnError
                    )
                    ;

                // wait...
                body.Wait();

                if (!keepAlive)
                {
                    Close();
                }
            }
        }
    }