Fomoxa.Net.WebSockets
0.1.1
dotnet add package Fomoxa.Net.WebSockets --version 0.1.1
NuGet\Install-Package Fomoxa.Net.WebSockets -Version 0.1.1
<PackageReference Include="Fomoxa.Net.WebSockets" Version="0.1.1" />
<PackageVersion Include="Fomoxa.Net.WebSockets" Version="0.1.1" />
<PackageReference Include="Fomoxa.Net.WebSockets" />
paket add Fomoxa.Net.WebSockets --version 0.1.1
#r "nuget: Fomoxa.Net.WebSockets, 0.1.1"
#:package Fomoxa.Net.WebSockets@0.1.1
#addin nuget:?package=Fomoxa.Net.WebSockets&version=0.1.1
#tool nuget:?package=Fomoxa.Net.WebSockets&version=0.1.1
Fomoxa.Net.WebSockets
WebSocket transport and listener for Fomoxa.Net.
WebSocketTransport implements ITransport and WebSocketListener implements IListenerTransport, so a Fomoxa Connection, or anything built on one such as fRPC, runs over WebSocket without changes.
The library targets netstandard2.1 and needs no engine.
It frames RFC 6455 itself on a non-blocking byte stream and does all its work inside the caller's Send, Receive and Accept calls, with no thread or Task of its own.
Scope
| Included | Not included |
|---|---|
Binary messages up to FomoxaWire.MaxDataFrameSize, fragmented or not |
Text messages (a text frame is a protocol error) |
| Ping answered with pong, close handshake | Extensions and subprotocols |
| HTTP upgrade on both sides: path check, origin allow-list, version 13 | TLS: end wss:// at a proxy, or run the client handshake on your own TLS byte stream |
| Handshake size and time limits | HTTP routing beyond one path |
Using it
Server side:
var settings = new WebSocketSettings { Path = "/fomoxa" };
settings.AllowedOrigins.Add("https://game.example");
using var listener = new WebSocketListener(new IPEndPoint(IPAddress.Any, 7777), settings);
AcceptOutcome accepted = listener.Accept();
if (accepted.Status == AcceptStatus.Accepted)
{
ITransport transport = accepted.Transport!;
}
Accept never blocks. A loop that has nothing else to do can sleep in listener.WaitReady(timeout), which returns when a connection arrives, a pending handshake can move, or one runs out of time. WaitReady(timeout, interrupt) also returns when the interrupt socket becomes readable, so another thread can wake the loop.
Client side, non-blocking:
using var connector = new WebSocketConnector("ws://game.example:7777/fomoxa", 0);
while (connector.Poll(out WebSocketTransport? transport) == WebSocketConnectStatus.Pending)
{
}
or blocking, for a program that owns its thread:
WebSocketTransport transport = WebSocketConnector.Connect("127.0.0.1", 7777, settings);
An address is a host name, an IP address or a ws:// URL. A host or IP uses the port argument and WebSocketSettings.Path; a URL carries its own.
WebSocketClientHandshake runs the client upgrade on any IByteStream. That is how a caller adds TLS: wrap the socket in a stream that encrypts, then hand it over.
Waiting on the socket
A loop that wants to sleep until the transport has work reads three members:
| Member | Meaning |
|---|---|
Socket |
The socket under a SocketByteStream, or null for another stream |
HasPendingInput |
A whole message or frame is already read and not delivered; waiting on the socket would miss it |
HasPendingOutput |
Bytes of an accepted send are still queued; wait for the socket to become writable |
Platform note
SocketByteStream and the listener poll the socket for write readiness before every send.
Under Unity's Mono a non-blocking Socket.Send can block when the send buffer is full; the poll keeps the call non-blocking.
Building and testing
dotnet build Fomoxa.Net.WebSockets.sln -c Release
dotnet test Fomoxa.Net.WebSockets.sln -c Release
The tests run real sockets on loopback, including the .NET ClientWebSocket against the listener and the connector against a .NET server WebSocket.
License
Apache-2.0.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- Fomoxa.Net (>= 0.1.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.