deniszykov.WebSocketListener 4.2.14

There is a newer version of this package available.
See the version list below for details.
dotnet add package deniszykov.WebSocketListener --version 4.2.14
NuGet\Install-Package deniszykov.WebSocketListener -Version 4.2.14
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="deniszykov.WebSocketListener" Version="4.2.14" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add deniszykov.WebSocketListener --version 4.2.14
#r "nuget: deniszykov.WebSocketListener, 4.2.14"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install deniszykov.WebSocketListener as a Cake Addin
#addin nuget:?package=deniszykov.WebSocketListener&version=4.2.14

// Install deniszykov.WebSocketListener as a Cake Tool
#tool nuget:?package=deniszykov.WebSocketListener&version=4.2.14

WebSocketListener

The WebSocketListener class provides simple methods that listen for and accept incoming WebSocket connection requests asynchronously. It is a lightweight listener with an API very similar to the System.Net.TcpListener class.

It does not use the Microsoft's System.Net.WebSockets namespace. It should work in any operating system running Microsoft.NET/Mono v4.5. This class is perfect for creating endpoints with WebSockets in Windows 2008 or Windows 7, which are not supported by System.Net.WebSockets. Also works on Linux through Mono.

WebSocketListener has been designed to provide WebSocket connectivity to other applications, in the same way that System.Net.TcpListener provides TCP connectivity. It is not a communication framework on its own and it does not provide any kind of publisher/subscriber pattern or reliable messaging beyond TCP.

  • It can work with both Text or Binary messages.
  • It supports wss://(secure). More info.
  • It supports per-message deflate compression. More info.
  • It can work with multiple WebSocket standards simultaneously. More info
  • It is extensible. More info.
  • It is asynchronous.
  • It supports Mono. [More info] (//github.com/vtortola/WebSocketListener/wiki/Mono-support)
  • It supports .NET Framework 4.5 and up, NETStandard 1.3 and up and Universal Windows Platform 10 (uap10.0).
  • It has the Ping/Pong functionality built-in.
  • It can measure connection latency. More info
  • It can work with cookies and custom HTTP response statuses. More info
  • It detects and disconnects half open connections.
  • It allows to send and receive messages as streams. WebSocket messages are represented as delimited stream-like objects, that allows integration with other .NET objects like e.g. StreamReader and StreamWriter. Two different WebSocket messages, yield two different streams.
  • Messages reads and writes are streamed. Big messages are not held in memory during reads or writes.
  • It handles partial frames transparently. The WebSocket specification states that a single message can be sent across multiple individual frames. The message stream will allow to read all the message data, no matter if it was sent in a single or multiple frames.
  • It handles interleaved control frames transparently. The WebSocket specification states that control frames can appear interleaved with data frames, including between partial frames of the same message. The message stream will allow to read just the message data, it will skip the control frames.

Take a look on the performance and load tests on a simple 'echo' server.

What's new in v4 (this fork)

This is a fork from project. There is some new features and bug fixes.

Major Features:

  • WebSocketClient

    • Same features and options as WebSocketListener
    • TLS support
    • works with IPv4/IPv6
  • Transports

    • TCP, Unix Sockets, Named Pipes (why not)
    • System.Net.Socket is abstracted as NetworkConnection
    • Graceful disconnection
    • Fine tuning with WebSocketListenerOptions. Fluent confuration API.

Minor Features:

  • Fully async API (it is strongly discouraged to use synchronous API with IO operations)
  • Logging via abstract ILogger
  • No additional dependencies (ServiceModel, TPL Dataflow)
  • New Headers<HeadersT> collections with fast access to known headers
  • Custom BufferManager, and use of BufferManager in all operations with buffers
  • WebSocketListener can now listen multiple endpoints from different transports
  • Pings now processed in batches or could be manually batched and sent (WebSocket.PingAsync)
  • WebSocketMessageWriteStream now can be gracefully closed with CloseAsync()
  • Tested to work under load
  • More unit-tests

Lost Features:

  • UWP target platform (netstandard1.3 is added instead)
  • synchronous IO methods on WebSocket and Streams (Write/Read/Close)
  • Cookies collection on WebSocketHttpRequest (class is still there)
  • Removed potentially dangerous feature 'RemoveBOM' on WSWriteStream.Write and refactored WriteStringAsync(), ReadStringAsync methods to use UTF-8 without BOM

Known Problems:

  • WebSocketDeflateStream uses sync Stream methods, it should be rewritten to fully support async operations.
  • Mono can't handle IPv6 Dual Mode sockets properly (exception in Socket.RemoteEndPoint and Socket.LocalEndPoint).

Quickstart

Install

WebSocketListener is available through NuGet

PM> Install-Package deniszykov.WebSocketListener
Product 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. 
.NET Core netcoreapp1.0 was computed.  netcoreapp1.1 was computed.  netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard1.3 is compatible.  netstandard1.4 was computed.  netstandard1.5 was computed.  netstandard1.6 was computed.  netstandard2.0 was computed.  netstandard2.1 was computed. 
.NET Framework net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen30 was computed.  tizen40 was computed.  tizen60 was computed. 
Universal Windows Platform uap was computed.  uap10.0 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (4)

Showing the top 4 NuGet packages that depend on deniszykov.WebSocketListener:

Package Downloads
Buttplug.Client.Connectors.WebsocketConnector

Websocket Connection Capabilities for Buttplug Clients. (.Net Standard 2.0+)

Buttplug.Server.Connectors.WebsocketServer

Websocket Connection Capabilities for Buttplug Servers. (.Net Framework 4.7+/.Net Standard 2.0)

Xamarin.Android.V8

V8 JavaScript Engine Bindings for Xamarin.Android with Inspector Enabled

Iterum.WebSockets

WebSockets-based Network

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on deniszykov.WebSocketListener:

Repository Stars
Ulterius/server
[WIP] Ulterius™ server where all the magic happens :rocket: :feelsgood:
Version Downloads Last updated
4.2.16 2,695 5/17/2023
4.2.15 8,933 4/6/2022
4.2.14 2,533 5/15/2021
4.2.10 1,319 9/8/2020
4.2.9 1,243 7/3/2020
4.2.8-beta 558 2/27/2020
4.2.7-alpha 730 6/27/2019
4.2.6-alpha 1,224 10/29/2018
4.2.4 88,858 7/3/2018
4.2.2 4,689 3/22/2018
4.1.3 2,024 11/7/2017
4.0.0 2,480 6/21/2017

v.4.2.14
fixed connection close reason parsing and hung(and timeout) after receiving close frame from client.

v.4.2.11
added close reply if remote party is requesting connection close.

v.4.2.10
fixed crash while debug logging Unix Domain Socket connection in `AcceptSocketAsConnection` method.
added additional checks for errord Socket.RemoteEndPoint and Socket.LocalEndPoint, now they produces 'WebSocketHttpRequest.NoAddress'

v.4.2.9
reverted gzip per-message compression back to original code
added WSClient gzip per-message compression negotiation

v.4.2.8
fix LocalEndPoint value on WebSocketConnectionRfc6455

v.4.2.7
[WS] added WriteAndCloseAsync to WebSocketWriteStream
[Compression] added custom async inflater/deflater in WS deflate extension. This would allow make inflation/deflation during WriteAsync/ReadAsync
[WS] replaced extra allocation of Stopwatch with less precise DateTime when measuring negotiation queue
[WS] add int overload to CloseAsync()
[WS] Expose client close codes


v.4.2.6
[WS] fixed ping timeout error on busy WebSockets

v.4.2.5
fixed error in WSWriterStream.Dispose() method
reset latency value on WS close or ping timeout
changed WSConnection Dispose() routine. Now it is not waiting for graceful close.
changed Ping timeout now cause WSConnection.Dispose instead on CloseAsync
fixed Ping timeout now properly calculated based on WS activity

v.4.2.4
[Common] replaced all DynamicMethod invocation with slower reflection alternative in case AOT-only runtime.
[WS] better error message while writing to closed stream
[Transports] clean-up SocketAsyncEventArgs after each operation to prevent holding GC references on used buffers

v.4.2.1
added DualMode option for TcpTransport
fixed race condition in WebSocket.Write() (Thanks @fuzzykiller)

v.4.2.0
stable version

v.4.1.6
fixed ping queue stop on error
added in/out buffers for ping/pong to reduce change of collision if both side pinging
replaced masking/demasking algorithm with faster unsafe implementation
added IpProtectionLevel option for tcp transport
fixed unbserved exception in WSMessageWriteStream.CloseAsync method

v.4.1.5
removed unused code from helpers
Prevent crash when value contains only empty spaces

v.4.1.4
added few changes in SendFrameAsync to prevent unobserved exceptions
added better error message for parallel AcceptWebSocketAsync call
added better error message when handshake error occurred

v.4.1.3
fixed unobserved exception at WebSocketSecureConnectionExtension.ExtendConnectionAsync

v.4.1.2
fixed infinite loop in timed queue (batch-ping related stuff)

v.4.1.1
fixed bug in BandwidthSaving ping strategy

v.4.1.0
added request header to WebSocketClient.ConnectAsync signature
PingQueue list re-use
Echo refactoring (perf. counters removed)
JetBrains.Annotations is now internal

v4.0.4
fixed TimedQueue implementation (atomic Int64 reading for 32 bit systems)
fixed Headers add NameValueCollection implementation to threat header values as not splitted headers
removed AsyncResultTask and refactored BeginRead/BeginWrite on streams
updated AsyncQueue implementation

v.4.0.3
added 'Connection:close' header to 'Bad Request' response of WebSocket handshaker
fixed Unobserved exception because wrong implementation of TaskHelper.IgnoreFault.
refactored TaskHelper.IgnoreFault and TaskHelper.PropagateResutTo to shortcut on Completed tasks
fixed ping errors on WebSockets
fixed TimedQueue impl
Headers refactoring, added HeaderFlags parameter to headers
refactored Socket shutdown sequence to prevent 'connection reset by peer' errors

UNIX Domain Sockets are tested on Linux