<commit_before>﻿module FsWamp.Server
open System
open System.Net
open System.Threading.Tasks
open System.Threading
open FsWamp.Messages
open FsWamp.Common

let private processContext (context : HttpListenerContext) ct =
    async {
        let! wsContext = context.AcceptWebSocketAsync(null) |> Async.AwaitTask
        let sendMessage = sendMessage wsContext.WebSocket ct
        let welcome = welcomeMessage (Guid.NewGuid().ToString("n")) "FsWamp/0.0.1"

        do! welcome |> sendMessage

        while not ct.IsCancellationRequested do
            let! (data, mt) = recv wsContext.WebSocket ct

            if mt = WebSockets.WebSocketMessageType.Text then
                let s = System.Text.UTF8Encoding.UTF8.GetString(data);
                match s with
                    | PREFIX (prefix, uri) ->
                        printfn "Got prefixmessage with prefix: %s for uri: %s" prefix uri
                    | CALL (callId, procUri, args) ->
                        match procUri with
                            | "add" ->
                                let res = args |> Seq.map int |> Seq.sum |> string
                                let callResult = callResultMessage callId res
                                do! callResult |> sendMessage
                            | _ ->
                                let callError = callErrorMessage callId "error#unknown_function" "Unknown function: " procUri
                                do! callError |> sendMessage
                    | SUBSCRIBE (topicId) ->
                        match topicId with
                            | "seTopic" ->
                                let event = eventMessage topicId "hello through event"
                                do! event |> sendMessage
                                printfn "send back an event"
                            | _ -> ()
                    | _ -> printfn "Got unknown message"
    }

let server host port ct =
    let listener = new HttpListener();
    listener.Prefixes.Add(sprintf "http://%s:%i/" host port);
    listener.Start();
    let rec listen (ct : CancellationToken) =
        async {
            try
                printfn "Listening"
                let! context = listener.GetContextAsync() |> Async.AwaitTask
                processContext context ct |> Async.Start
                if not ct.IsCancellationRequested then
                    return! listen ct
                  else
                    printfn "Cancellation requested!"
                    (listener :> IDisposable).Dispose()
            with | :? OperationCanceledException -> printfn "Cancellation requested"; return()
        }

    listen ct
<commit_msg>Close response if request is not websocket<commit_after>﻿module FsWamp.Server
open System
open System.Net
open System.Threading.Tasks
open System.Threading
open FsWamp.Messages
open FsWamp.Common

let private processContext (context : HttpListenerContext) ct =
    async {
        let! wsContext = context.AcceptWebSocketAsync(null) |> Async.AwaitTask
        let sendMessage = sendMessage wsContext.WebSocket ct
        let welcome = welcomeMessage (Guid.NewGuid().ToString("n")) "FsWamp/0.0.1"

        do! welcome |> sendMessage

        while not ct.IsCancellationRequested do
            let! (data, mt) = recv wsContext.WebSocket ct

            if mt = WebSockets.WebSocketMessageType.Text then
                let s = System.Text.UTF8Encoding.UTF8.GetString(data);
                match s with
                    | PREFIX (prefix, uri) ->
                        printfn "Got prefixmessage with prefix: %s for uri: %s" prefix uri
                    | CALL (callId, procUri, args) ->
                        match procUri with
                            | "add" ->
                                let res = args |> Seq.map int |> Seq.sum |> string
                                let callResult = callResultMessage callId res
                                do! callResult |> sendMessage
                            | _ ->
                                let callError = callErrorMessage callId "error#unknown_function" "Unknown function: " procUri
                                do! callError |> sendMessage
                    | SUBSCRIBE (topicId) ->
                        match topicId with
                            | "seTopic" ->
                                let event = eventMessage topicId "hello through event"
                                do! event |> sendMessage
                                printfn "send back an event"
                            | _ -> ()
                    | _ -> printfn "Got unknown message"
    }

let server host port ct =
    let listener = new HttpListener();
    listener.Prefixes.Add(sprintf "http://%s:%i/" host port);
    listener.Start();
    let rec listen (ct : CancellationToken) =
        async {
            try
                printfn "Listening"
                let! context = listener.GetContextAsync() |> Async.AwaitTask
                if context.Request.IsWebSocketRequest then
                    processContext context ct |> Async.Start
                else context.Response.Close()

                if not ct.IsCancellationRequested then
                    return! listen ct
                  else
                    printfn "Cancellation requested!"
                    (listener :> IDisposable).Dispose()
            with | :? OperationCanceledException -> printfn "Cancellation requested"; return()
        }

    listen ct

{'fraction_non_alphanumeric': 0.07623563742476747, 'fraction_numerical': 0.001823819077147547, 'mean_word_length': 1.4, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3353294', 'n_tokens_mistral': 1382, 'n_tokens_neox': 1298, 'n_words': 377}