Skip to content

Instantly share code, notes, and snippets.

@zapu
Last active August 29, 2015 14:09
Show Gist options
  • Save zapu/00947ef360b4ca1fc02f to your computer and use it in GitHub Desktop.
Save zapu/00947ef360b4ca1fc02f to your computer and use it in GitHub Desktop.
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