7 people like it.
Like the snippet!
Rx Web Server Demo
Barebone Reactive Extensions web server demo.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
|
open System
open System.Net
open System.Text
open System.Reactive.Concurrency
open System.Reactive.Subjects
open System.Reactive.Linq
open FSharp.Control.Reactive
let makeObservableListener url =
let listener = new HttpListener()
listener.Prefixes.Add url
listener.Start()
let makeObservable =
Observable.FromAsync(listener.GetContextAsync)
|> Observable.repeat
|> Observable.retry
|> Observable.publish
|> Observable.refCount
(listener, makeObservable)
let handleRequest obs =
obs
|> Observable.filter (fun (ctx : HttpListenerContext) -> ctx.Request.Url.AbsolutePath.EndsWith("/hello"))
|> Observable.subscribe (fun ctx ->
let b = Encoding.UTF8.GetBytes("world")
ctx.Response.OutputStream.Write(b, 0, b.Length)
ctx.Response.Close())
[<EntryPoint>]
let main argv =
let (lst, obs) = makeObservableListener "http://localhost:55555/"
handleRequest obs |> ignore
Console.ReadKey() |> ignore
lst.Close()
0
|
namespace System
namespace System.Net
namespace System.Text
namespace System.Reactive
namespace System.Reactive.Concurrency
namespace System.Reactive.Subjects
namespace System.Reactive.Linq
Multiple items
namespace FSharp
--------------------
namespace Microsoft.FSharp
Multiple items
namespace FSharp.Control
--------------------
namespace Microsoft.FSharp.Control
namespace FSharp.Control.Reactive
val makeObservableListener : url:string -> HttpListener * IObservable<'a>
Full name: Script.makeObservableListener
val url : string
val listener : HttpListener
Multiple items
type HttpListener =
new : unit -> HttpListener
member Abort : unit -> unit
member AuthenticationSchemeSelectorDelegate : AuthenticationSchemeSelector with get, set
member AuthenticationSchemes : AuthenticationSchemes with get, set
member BeginGetContext : callback:AsyncCallback * state:obj -> IAsyncResult
member Close : unit -> unit
member DefaultServiceNames : ServiceNameCollection
member EndGetContext : asyncResult:IAsyncResult -> HttpListenerContext
member ExtendedProtectionPolicy : ExtendedProtectionPolicy with get, set
member ExtendedProtectionSelectorDelegate : ExtendedProtectionSelector with get, set
...
nested type ExtendedProtectionSelector
Full name: System.Net.HttpListener
--------------------
HttpListener() : unit
property HttpListener.Prefixes: HttpListenerPrefixCollection
HttpListenerPrefixCollection.Add(uriPrefix: string) : unit
HttpListener.Start() : unit
val makeObservable : IObservable<'a>
type Observable =
static member Aggregate<'TSource> : source:IObservable<'TSource> * accumulator:Func<'TSource, 'TSource, 'TSource> -> IObservable<'TSource> + 2 overloads
static member All<'TSource> : source:IObservable<'TSource> * predicate:Func<'TSource, bool> -> IObservable<bool>
static member Amb<'TSource> : [<ParamArray>] sources:IObservable<'TSource>[] -> IObservable<'TSource> + 2 overloads
static member And<'TLeft, 'TRight> : left:IObservable<'TLeft> * right:IObservable<'TRight> -> Pattern<'TLeft, 'TRight>
static member Any<'TSource> : source:IObservable<'TSource> -> IObservable<bool> + 1 overload
static member AsObservable<'TSource> : source:IObservable<'TSource> -> IObservable<'TSource>
static member Average : source:IObservable<float> -> IObservable<float> + 19 overloads
static member Buffer<'TSource, 'TBufferClosing> : source:IObservable<'TSource> * bufferClosingSelector:Func<IObservable<'TBufferClosing>> -> IObservable<IList<'TSource>> + 10 overloads
static member Case<'TValue, 'TResult> : selector:Func<'TValue> * sources:IDictionary<'TValue, IObservable<'TResult>> -> IObservable<'TResult> + 2 overloads
static member Cast<'TResult> : source:IObservable<obj> -> IObservable<'TResult>
...
Full name: System.Reactive.Linq.Observable
Observable.FromAsync(actionAsync: Func<Threading.CancellationToken,Threading.Tasks.Task>) : IObservable<Reactive.Unit>
Observable.FromAsync(actionAsync: Func<Threading.Tasks.Task>) : IObservable<Reactive.Unit>
Observable.FromAsync<'TResult>(functionAsync: Func<Threading.CancellationToken,Threading.Tasks.Task<'TResult>>) : IObservable<'TResult>
Observable.FromAsync<'TResult>(functionAsync: Func<Threading.Tasks.Task<'TResult>>) : IObservable<'TResult>
val repeat : source:IObservable<'Source> -> IObservable<'Source>
Full name: FSharp.Control.Reactive.Observable.repeat
val retry : source:IObservable<'Source> -> IObservable<'Source>
Full name: FSharp.Control.Reactive.Observable.retry
val publish : source:IObservable<'a> -> IConnectableObservable<'a>
Full name: FSharp.Control.Reactive.Observable.publish
val refCount : source:IConnectableObservable<'a> -> IObservable<'a>
Full name: FSharp.Control.Reactive.Observable.refCount
val handleRequest : obs:IObservable<HttpListenerContext> -> IDisposable
Full name: Script.handleRequest
val obs : IObservable<HttpListenerContext>
Multiple items
val filter : predicate:('T -> bool) -> source:IObservable<'T> -> IObservable<'T>
Full name: FSharp.Control.Reactive.Observable.filter
--------------------
val filter : predicate:('T -> bool) -> source:IObservable<'T> -> IObservable<'T>
Full name: Microsoft.FSharp.Control.Observable.filter
val ctx : HttpListenerContext
type HttpListenerContext =
member Request : HttpListenerRequest
member Response : HttpListenerResponse
member User : IPrincipal
Full name: System.Net.HttpListenerContext
property HttpListenerContext.Request: HttpListenerRequest
property HttpListenerRequest.Url: Uri
property Uri.AbsolutePath: string
String.EndsWith(value: string) : bool
String.EndsWith(value: string, comparisonType: StringComparison) : bool
String.EndsWith(value: string, ignoreCase: bool, culture: Globalization.CultureInfo) : bool
Multiple items
val subscribe : onNext:('T -> unit) -> observable:IObservable<'T> -> IDisposable
Full name: FSharp.Control.Reactive.Observable.subscribe
--------------------
val subscribe : callback:('T -> unit) -> source:IObservable<'T> -> IDisposable
Full name: Microsoft.FSharp.Control.Observable.subscribe
val b : byte []
type Encoding =
member BodyName : string
member Clone : unit -> obj
member CodePage : int
member DecoderFallback : DecoderFallback with get, set
member EncoderFallback : EncoderFallback with get, set
member EncodingName : string
member Equals : value:obj -> bool
member GetByteCount : chars:char[] -> int + 3 overloads
member GetBytes : chars:char[] -> byte[] + 5 overloads
member GetCharCount : bytes:byte[] -> int + 2 overloads
...
Full name: System.Text.Encoding
property Encoding.UTF8: Encoding
Encoding.GetBytes(s: string) : byte []
Encoding.GetBytes(chars: char []) : byte []
Encoding.GetBytes(chars: char [], index: int, count: int) : byte []
Encoding.GetBytes(chars: nativeptr<char>, charCount: int, bytes: nativeptr<byte>, byteCount: int) : int
Encoding.GetBytes(s: string, charIndex: int, charCount: int, bytes: byte [], byteIndex: int) : int
Encoding.GetBytes(chars: char [], charIndex: int, charCount: int, bytes: byte [], byteIndex: int) : int
property HttpListenerContext.Response: HttpListenerResponse
property HttpListenerResponse.OutputStream: IO.Stream
IO.Stream.Write(buffer: byte [], offset: int, count: int) : unit
property Array.Length: int
HttpListenerResponse.Close() : unit
HttpListenerResponse.Close(responseEntity: byte [], willBlock: bool) : unit
Multiple items
type EntryPointAttribute =
inherit Attribute
new : unit -> EntryPointAttribute
Full name: Microsoft.FSharp.Core.EntryPointAttribute
--------------------
new : unit -> EntryPointAttribute
val main : argv:string [] -> int
Full name: Script.main
val argv : string []
val lst : HttpListener
val ignore : value:'T -> unit
Full name: Microsoft.FSharp.Core.Operators.ignore
type Console =
static member BackgroundColor : ConsoleColor with get, set
static member Beep : unit -> unit + 1 overload
static member BufferHeight : int with get, set
static member BufferWidth : int with get, set
static member CapsLock : bool
static member Clear : unit -> unit
static member CursorLeft : int with get, set
static member CursorSize : int with get, set
static member CursorTop : int with get, set
static member CursorVisible : bool with get, set
...
Full name: System.Console
Console.ReadKey() : ConsoleKeyInfo
Console.ReadKey(intercept: bool) : ConsoleKeyInfo
HttpListener.Close() : unit
More information