SimpleWebsocketServer 1.0.1
See the version list below for details.
dotnet add package SimpleWebsocketServer --version 1.0.1
NuGet\Install-Package SimpleWebsocketServer -Version 1.0.1
<PackageReference Include="SimpleWebsocketServer" Version="1.0.1" />
paket add SimpleWebsocketServer --version 1.0.1
#r "nuget: SimpleWebsocketServer, 1.0.1"
// Install SimpleWebsocketServer as a Cake Addin #addin nuget:?package=SimpleWebsocketServer&version=1.0.1 // Install SimpleWebsocketServer as a Cake Tool #tool nuget:?package=SimpleWebsocketServer&version=1.0.1
SimpleWebSocketServer Library
SimpleWebSocketServer Library is designed as a simplistic library in use wich provides a middleware for the programmer to handle websocket connections. The library targets the most popular frameworks within .NET such as: .NET FrameWork 4.5 to 4.7 and .NET Core 2.0 & 2.1 and should work with Mono. The programmer just needs 4 lines of code to start and stop the server. The middleware has the following features:
- Hubs (multiple websocket end-points).
- Multiple Clients.
- Clients can create their own ID (By appending it to the connection url).
- Clients can connect without specifying a ID (Automatically generated on the server).
- Single Event Handler.
- Configurable Port, End-points & (Receive)BufferSize.
- Pre-configured HTTP Server as negotiater for the WebSocket Protocol. (Default runs at port 80, no SSL support for now).
- Both Asynchronous and Synchronous methods.
In case the programmer want's to use it's own HTTP Server for the negotiation for the WebSocket Protocol, he can use the inner classes of the library such as the WebSocketServer
class. (See down below for a quick setup).
Installation
You can get the NuGet package from here: NuGet. You can get the dll files here: GitHub Releases.
Usage - Middleware
The quickest way to get started:
SimpleWebSocketServer websocketServer = new SimpleWebSocketServer();
websocketServer.WebsocketServerEvent += OnWebsocketEvent;
websocketServer.StartServer();
Console.ReadKey();
bool stopped = websocketServer.StopAll("Server had enough, RIP server.");
And the event handler method:
private static void OnWebsocketEvent(object sender, WebSocketEventArg args)
{
if (args.data != null && args.isText)
{
string received = Encoding.UTF8.GetString(args.data);
_WebsocketServer.SendTextMessage("Client: " + args.clientId + " on url: " + args.clientBaseUrl + ", says: " + received);
}
}
As you can see this is written with synchronous methods. If you want to use asynchronous methods, just add Async behind the method name.
For more information about the WebSocketEventArg class and other classes, go to this page: DoxygenWiki -WebSocketEventArg
.
You can provide the constructor of the middleware class SimpleWebSocketServer
with the following class with parameters to customize your settings, here you can also find the default values used: DoxygenWiki - SimpleWebSocketServerSettings
.
Usage - WebSocketServer
Class (Advanced)
If you just want to get WebSocket to work, just use the MiddleWare class writen above. But, in case you do want full control, or if you already have a HTTP Server and don't want to run a seperate HTTP Server just for WebSockets, and yours has the capability to Negotiate the WebSocket protocol (see: Mozilla WebSocket API), you can use this class to handle further WebSocket protocol communication.
Here is a basic overview on how to use it:
WebSocketClientInfo webSocketClientInfo = new WebSocketClientInfo()
{
clientId = "GENERATE CLIENT ID",
clientBaseUrl = "URL CLIENT USED TO CONNECT",
client = new System.Net.Sockets.TcpClient() // TCP CLIENT RETREIVED FROM HTTP SERVER AFTER WEBSOCKET UPGRADE
};
WebSocketServer newServer = new WebSocketServer(webSocketClientInfo);
newServer.WebSocketServerEvent += OnWebsocketEvent;
newServer.StartServer();
Console.ReadKey();
newServer.StopServer();
If you want to use asynchronous methods instead of synchronous, you can just like with the middleware append Async
to the end of the method name. Example newServer.StartServer()
becomes newServer.StartServerAsync()
.
The eventhandler is the same as with the middleware class.
Full (Doxygen generated) Wiki
Development
The library was designed and made with the intention to be used in hobby projects and to mess with WebSockets. I wrote this to learn about the WebSocket protocol in general and it isn't really intended to be used within an actual production environment. Why? Because I don't want to spend to much time on this side project which in turn leads to the lack of support. I will fix big issues if there are no workarounds, but appart from that, don't expect much development & help.
I will probably be using it alongside my other hobby projects, whenever I encounter an issue with the library, you can expect me to release the fix for it here as well.
Todos
- Write unit tests.
License
MIT
Free Software, Hell Yeah!
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. |
.NET Core | netcoreapp2.0 is compatible. netcoreapp2.1 is compatible. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Framework | net45 is compatible. net451 was computed. net452 was computed. net46 is compatible. net461 was computed. net462 was computed. net463 was computed. net47 is compatible. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
-
.NETCoreApp 2.0
- No dependencies.
-
.NETCoreApp 2.1
- No dependencies.
-
.NETFramework 4.5
- No dependencies.
-
.NETFramework 4.6
- No dependencies.
-
.NETFramework 4.7
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
-Fixed names
-Removed some ugly cross calling between classes.
-Added asynchronous and synchronous methods.