![]() |
VOOZH | about |
dotnet add package FSharp.Control.Websockets --version 0.3.0
NuGet\Install-Package FSharp.Control.Websockets -Version 0.3.0
<PackageReference Include="FSharp.Control.Websockets" Version="0.3.0" />
<PackageVersion Include="FSharp.Control.Websockets" Version="0.3.0" />Directory.Packages.props
<PackageReference Include="FSharp.Control.Websockets" />Project file
paket add FSharp.Control.Websockets --version 0.3.0
#r "nuget: FSharp.Control.Websockets, 0.3.0"
#:package FSharp.Control.Websockets@0.3.0
#addin nuget:?package=FSharp.Control.Websockets&version=0.3.0Install as a Cake Addin
#tool nuget:?package=FSharp.Control.Websockets&version=0.3.0Install as a Cake Tool
FSharp.Control.WebSockets wraps dotnet websockets in FSharp friendly functions and has a ThreadSafe version.
Dotnet websockets only allow for one receive and one send at a time. If multiple threads try to write to a websocket, it will throw a System.InvalidOperationException with the message There is already one outstanding 'SendAsync' call for this WebSocket instance. ReceiveAsync and SendAsync can be called simultaneously, but at most one outstanding operation for each of them is allowed at the same time.. This wraps a websocket in a FIFO that allows for multiple threads to write or read at the same time. See Websocket Remarks on Microsoft for more information.
This provides a readMessage type function. This is the biggest stumbling block people have when working with websockets. You have to keep reading from the message until itβs finished. People either donβt and end up having corrupted messages with a small buffer or have a buffer that is giant and can be a memory hog for smaller messages.
Uses RecyclableMemoryStreamManager and ArrayPool to help keep memory usage and GC down.
| GitHub Actions |
|---|
| π GitHub Actions |
| π Build History |
| Name | Stable | Prerelease |
|---|---|---|
| FSharp.Control.Websockets | π NuGet Badge |
π NuGet Badge |
| FSharp.Control.Websockets.TPL | π NuGet Badge |
π NuGet Badge |
open System
open System.Net.WebSockets
open System.Threading.Tasks
open Microsoft.AspNetCore.Builder
open Microsoft.AspNetCore.Hosting
open Microsoft.AspNetCore.Http
open FSharp.Control.Websockets
open Microsoft.Extensions.Configuration
let echoWebSocket (httpContext: HttpContext) (next: unit -> Async<unit>) =
async {
if httpContext.WebSockets.IsWebSocketRequest then
let! websocket =
httpContext.WebSockets.AcceptWebSocketAsync()
|> Async.AwaitTask
// Create a thread-safe WebSocket from an existing websocket
let threadSafeWebSocket =
ThreadSafeWebSocket.createFromWebSocket websocket
while threadSafeWebSocket.State = WebSocketState.Open do
try
let! result =
threadSafeWebSocket
|> ThreadSafeWebSocket.receiveMessageAsUTF8
match result with
| Ok (WebSocket.ReceiveUTF8Result.String text) ->
//Echo it back to the client
do! WebSocket.sendMessageAsUTF8 websocket (text)
| Ok (WebSocket.ReceiveUTF8Result.Closed (status, reason)) -> printfn "Socket closed %A - %s" status reason
| Error (ex) -> printfn "Receiving threw an exception %A" ex.SourceException
with e -> printfn "%A" e
else
do! next ()
}
//Convenience function for making middleware with F# asyncs and funcs
let fuse (middlware: HttpContext -> (unit -> Async<unit>) -> Async<unit>) (app: IApplicationBuilder) =
app.Use
(fun env next ->
middlware env (next.Invoke >> Async.AwaitTask)
|> Async.StartAsTask
:> Task)
let configureEchoServer (appBuilder: IApplicationBuilder) =
appBuilder.UseWebSockets()
|> fuse (echoWebSocket)
|> ignore
let getKestrelServer configureServer uri =
let configBuilder = new ConfigurationBuilder()
let configBuilder = configBuilder.AddInMemoryCollection()
let config = configBuilder.Build()
config.["server.urls"] <- uri
let host =
WebHostBuilder()
.UseConfiguration(config)
.UseKestrel()
.Configure(fun app -> configureServer app)
.Build()
.Start()
host
[<EntryPoint>]
let main argv =
getKestrelServer configureEchoServer "http://localhost:3000"
Console.ReadKey() |> ignore
0
Make sure the following requirements are installed in your system:
> build.cmd // on windows
$ ./build.sh // on unix
CONFIGURATION will set the configuration of the dotnet commands. If not set it will default to Release.
CONFIGURATION=Debug ./build.sh will result in things like dotnet build -c DebugGITHUB_TOKEN will be used to upload release notes and nuget packages to github.
The WatchTests target will use dotnet-watch to watch for changes in your lib or tests and re-run your tests on all TargetFrameworks
./build.sh WatchTests
git add .
git commit -m "Scaffold"
git remote add origin origin https://github.com/user/MyCoolNewLib.git
git push -u origin master
paket config add-token "https://www.nuget.org" 4003d786-cc37-4004-bfdf-c4f3e8ef9b3a
GITHUB_TOKEN to upload release notes and artifacts to githubThen update the RELEASE_NOTES.md with a new version, date, and release notes ReleaseNotesHelper
#### 0.2.0 - 2017-04-20
* FEATURE: Does cool stuff!
* BUGFIX: Fixes that silly oversight
Release target. This will:
Bump version to 0.2.0 and add the release notes to the commit./build.sh Release
To format code run the following target
./build.sh FormatCode
This uses Fantomas to do code formatting. Please report code formatting bugs to that repository.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 was computed. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 was computed. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 is compatible. |
| .NET Framework | net461 net461 is compatible. net462 net462 was computed. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 was computed. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
Showing the top 3 NuGet packages that depend on FSharp.Control.Websockets:
| Package | Downloads |
|---|---|
|
Saturn
Opinionated, web development framework for F# which implements the server-side, functional MVC pattern |
|
|
FSharp.Discord.Gateway
Package Description |
|
|
StreamDeckDotnet
StreamDeckDotnet helps you build event handling pipelines for custom Stream Deck plugins! |
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.3.0 | 122,038 | 3/4/2024 |
| 0.3.0-beta001 | 234 | 3/4/2024 |
| 0.2.3 | 62,026 | 9/18/2022 |
| 0.2.3-beta001 | 398 | 9/18/2022 |
| 0.2.2 | 295,572 | 10/2/2019 |
| 0.2.1 | 1,429 | 9/13/2019 |
| 0.2.0 | 25,015 | 5/24/2019 |
| 0.1.2 | 789 | 5/24/2019 |
| 0.1.1 | 795 | 5/24/2019 |
| 0.1.0 | 4,219 | 5/24/2019 |
## [0.3.0] - 2024-03-04
[0.3.0]: https://github.com/TheAngryByrd/FSharp.Control.WebSockets/compare/v0.2.3...v0.3.0
### Changed
- [Update Microsoft.IO.RecyclableStreams to v3](https://github.com/TheAngryByrd/FSharp.Control.WebSockets/pull/10) thanks @ntwilson