Created
May 7, 2012 21:40
-
-
Save micahasmith/2630646 to your computer and use it in GitHub Desktop.
Quick Rx Based Web Server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text; | |
| using System.Net.Sockets; | |
| using System.Data; | |
| using System.Net; | |
| using System.Threading; | |
| using System.Threading.Tasks; | |
| using System.Reactive.Linq; | |
| using System.Reactive.Subjects; | |
| using System.Reactive; | |
| namespace RxWeb | |
| { | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| new WebActor("http://localhost:8081/"); | |
| Console.ReadKey(); | |
| } | |
| } | |
| public class WebActor | |
| { | |
| public WebActor(string uri) | |
| { | |
| HttpListener listener = new HttpListener(); | |
| listener.Prefixes.Add("http://localhost:8081/"); | |
| listener.Start(); | |
| Init(listener); | |
| } | |
| private static void Init(HttpListener listener) | |
| { | |
| var ofactory = Observable.FromAsyncPattern<HttpListenerContext>(listener.BeginGetContext, listener.EndGetContext); | |
| var observable = ofactory(); | |
| object o = new object(); | |
| var disposable=observable.Subscribe(i => | |
| { | |
| Console.WriteLine("responding"); | |
| i.Response.Close(Encoding.ASCII.GetBytes("thanks!"), false); | |
| Console.WriteLine("stopping"); | |
| Init(listener); | |
| }); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment