Skip to content

Instantly share code, notes, and snippets.

@micahasmith
Created May 7, 2012 21:40
Show Gist options
  • Select an option

  • Save micahasmith/2630646 to your computer and use it in GitHub Desktop.

Select an option

Save micahasmith/2630646 to your computer and use it in GitHub Desktop.
Quick Rx Based Web Server
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