Skip to content

Commit 513cbfa

Browse files
pre63TheAngryByrd
authored andcommitted
Using Example Update
I updated the using example to use the latest API version and removed the async so that it could easily be run in a console program without modification.
1 parent 7222a60 commit 513cbfa

1 file changed

Lines changed: 60 additions & 50 deletions

File tree

README.md

Lines changed: 60 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -40,68 +40,78 @@ FSharp.Control.Websockets.TPL | [![NuGet Badge](https://buildstats.info/nuget/FS
4040

4141
```fsharp
4242
open System
43-
open System.Net
4443
open System.Net.WebSockets
4544
open System.Threading.Tasks
4645
open Microsoft.AspNetCore.Builder
4746
open Microsoft.AspNetCore.Hosting
4847
open Microsoft.AspNetCore.Http
4948
open FSharp.Control.Websockets
49+
open Microsoft.Extensions.Configuration
5050
51-
let echoWebSocket (httpContext : HttpContext) (next : unit -> Async<unit>) = async {
52-
51+
let echoWebSocket (httpContext: HttpContext) (next: unit -> Async<unit>) =
52+
async {
5353
if httpContext.WebSockets.IsWebSocketRequest then
54-
let! websocket = httpContext.WebSockets.AcceptWebSocketAsync() |> Async.AwaitTask
55-
// Create a thread-safe WebSocket from an existing websocket
56-
let threadSafeWebSocket = ThreadSafeWebSocket.createFromWebSocket websocket
57-
while threadSafeWebSocket.State = WebSocketState.Open do
58-
try
59-
let! result =
60-
threadSafeWebSocket
61-
|> ThreadSafeWebSocket.receiveMessageAsUTF8
62-
match result with
63-
| Ok(WebSocket.ReceiveUTF8Result.String text) ->
64-
//Echo it back to the client
65-
do! WebSocket.sendMessageAsUTF8 text websocket
66-
| Ok(WebSocket.ReceiveUTF8Result.StreamClosed (status, reason)) ->
67-
printfn "Socket closed %A - %s" status reason
68-
| Error (ex) ->
69-
printfn "Receiving threw an exception %A" ex.SourceException
70-
with e ->
71-
printfn "%A" e
54+
let! websocket =
55+
httpContext.WebSockets.AcceptWebSocketAsync()
56+
|> Async.AwaitTask
57+
// Create a thread-safe WebSocket from an existing websocket
58+
let threadSafeWebSocket =
59+
ThreadSafeWebSocket.createFromWebSocket websocket
60+
61+
while threadSafeWebSocket.State = WebSocketState.Open do
62+
try
63+
let! result =
64+
threadSafeWebSocket
65+
|> ThreadSafeWebSocket.receiveMessageAsUTF8
66+
67+
match result with
68+
| Ok (WebSocket.ReceiveUTF8Result.String text) ->
69+
//Echo it back to the client
70+
do! WebSocket.sendMessageAsUTF8 websocket (text)
71+
| Ok (WebSocket.ReceiveUTF8Result.Closed (status, reason)) -> printfn "Socket closed %A - %s" status reason
72+
| Error (ex) -> printfn "Receiving threw an exception %A" ex.SourceException
73+
with e -> printfn "%A" e
7274
7375
else
74-
do! next()
75-
76-
}
77-
76+
do! next ()
77+
}
7878
7979
//Convenience function for making middleware with F# asyncs and funcs
80-
let fuse (middlware : HttpContext -> (unit -> Async<unit>) -> Async<unit>) (app:IApplicationBuilder) =
81-
app.Use(fun env next ->
82-
middlware env (next.Invoke >> Async.AwaitTask)
83-
|> Async.StartAsTask :> Task)
84-
85-
let configureEchoServer (appBuilder : IApplicationBuilder) =
86-
appBuilder.UseWebSockets()
87-
|> Server.fuse (echoWebSocket)
88-
|> ignore
89-
90-
let getKestrelServer configureServer uri = async {
91-
let configBuilder = new ConfigurationBuilder()
92-
let configBuilder = configBuilder.AddInMemoryCollection()
93-
let config = configBuilder.Build()
94-
config.["server.urls"] <- uri
95-
let host = WebHostBuilder()
96-
.UseConfiguration(config)
97-
.UseKestrel()
98-
.Configure(fun app -> configureServer app )
99-
.Build()
100-
101-
do! host.StartAsync() |> Async.AwaitTask
102-
return host
103-
}
104-
80+
let fuse (middlware: HttpContext -> (unit -> Async<unit>) -> Async<unit>) (app: IApplicationBuilder) =
81+
app.Use
82+
(fun env next ->
83+
middlware env (next.Invoke >> Async.AwaitTask)
84+
|> Async.StartAsTask
85+
:> Task)
86+
87+
88+
let configureEchoServer (appBuilder: IApplicationBuilder) =
89+
appBuilder.UseWebSockets()
90+
|> fuse (echoWebSocket)
91+
|> ignore
92+
93+
let getKestrelServer configureServer uri =
94+
let configBuilder = new ConfigurationBuilder()
95+
let configBuilder = configBuilder.AddInMemoryCollection()
96+
let config = configBuilder.Build()
97+
config.["server.urls"] <- uri
98+
99+
let host =
100+
WebHostBuilder()
101+
.UseConfiguration(config)
102+
.UseKestrel()
103+
.Configure(fun app -> configureServer app)
104+
.Build()
105+
.Start()
106+
107+
host
108+
109+
[<EntryPoint>]
110+
let main argv =
111+
getKestrelServer configureEchoServer "http://localhost:3000"
112+
Console.ReadKey() |> ignore
113+
114+
0
105115
```
106116

107117
---

0 commit comments

Comments
 (0)