Last active
August 29, 2015 14:09
-
-
Save zapu/00947ef360b4ca1fc02f to your computer and use it in GitHub Desktop.
This file contains 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
module SocketPolicyServer | |
type HttpListener = System.Net.HttpListener | |
type HttpListenerRequest = System.Net.HttpListenerRequest | |
let StartPolicyServer (port:int) = | |
if not HttpListener.IsSupported then | |
failwith "HttpListener not supported" | |
else | |
let listener = new HttpListener() | |
let prfx = "http://localhost:" + (string port) + "/" | |
listener.Prefixes.Add(prfx) | |
listener.Start() | |
async { | |
while listener.IsListening do | |
let contextTask = listener.GetContextAsync() | |
let! context = Async.AwaitTask contextTask | |
async { | |
let request = context.Request | |
let response = context.Response | |
let buffer = System.Text.Encoding.UTF8.GetBytes("Hello world!\n") | |
response.ContentLength64 = buffer.LongLength |> ignore | |
response.OutputStream.Write(buffer, 0, buffer.Length) | |
response.Close() | |
} |> Async.Start | |
} |> Async.RunSynchronously | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment