Chd.Library.Web.Socket
9.2.3
dotnet add package Chd.Library.Web.Socket --version 9.2.3
NuGet\Install-Package Chd.Library.Web.Socket -Version 9.2.3
<PackageReference Include="Chd.Library.Web.Socket" Version="9.2.3" />
paket add Chd.Library.Web.Socket --version 9.2.3
#r "nuget: Chd.Library.Web.Socket, 9.2.3"
// Install Chd.Library.Web.Socket as a Cake Addin #addin nuget:?package=Chd.Library.Web.Socket&version=9.2.3 // Install Chd.Library.Web.Socket as a Cake Tool #tool nuget:?package=Chd.Library.Web.Socket&version=9.2.3
Web Socket library for .Net Core
Chd (cleverly handle difficulty) library helps you cleverly handle difficulty, writing code fastly and do your application stable.
📝 Table of Contents
- About
- Usage
- Injection WebSocket Into The Project
- Apsettings Configurations
- Run Application and Test
- Sending Messages to All Clients In Controller
- Authors
- Acknowledgments
🧐 About
Websocket is a communications protocol that provides full-duplex communication channels over a single TCP connection.
🏁 Getting Started
Websockets are often used in chat applications, online gaming, and other applications that require real-time communication. This library simplfy WebSocket using on .net core applications.
Prerequisites
You must use .net core 9.0 or higher.
🎈 Usage
We will create a sample WebSocketConnection which inherits WebSocketConnection class.We override OnMessage, OnOpen and OnClose methods. We can add our custom code in these methods. We can also inject services into this class. We can use this class for sending messages to clients. We can also use this class for receiving messages from clients.
public class SampleWebSocket : WebSocketConnectionManager
{
protected override void OnMessage(MessageEventArgs e)
{
base.OnMessage(e);
}
protected override void OnOpen()
{
base.OnOpen();
}
protected override void OnClose(CloseEventArgs e)
{
base.OnClose(e);
}
}
💉 Injection WebSocket Into The Project
We will inject our WebSocketConnection into the project. We will use AddWebSocketServices method for this. We will add our WebSocketConnection class as a generic parameter.
var builder = WebApplication.CreateBuilder(args);
....
....
builder.Services.AddWebSocketServices<SampleWebSocket>();
⚙️ Apsettings Configurations
You must add code below in appsettings.json
"WebSocketConfig": {
"BaseUrl": "ws://localhost", //change base url
"Endpoint": "Chd",
"Port": "6501" //change port
}
🔧 Run Application and Test
Now when we run the application we can connect to the WebSocket server using the URL ws://localhost:6501/Chd. We can send messages to the server and receive messages from the server. We can send messages to all clients to use the Simple Web Socket Client chrome extension. The extension is available on the chrome web store. We may search for simplewebsocket on the chrome web store and install it. We should connect to the WebSocket server using the URL ws://localhost:6501/Chd. We can send messages to the server and receive messages from the server.
🏹 Sending Messages to All Clients In Controller
We may send messages to all clients using BroadcastMessage method. We may use this method in our controller.
public class WebSocketTestController : ControllerBase
{
private readonly WebSocketConnectionManager _webSocketConnectionManager;
public WebSocketTestController(WebSocketConnectionManager webSocketConnectionManager)
{
_webSocketConnectionManager = webSocketConnectionManager;
}
[Route("BroadcastMessage")]
[HttpGet]
public string? BroadcastMessage()//For testing on swagger or etc..
{
_webSocketConnectionManager.BroadcastMessage("Hello All");
return "Ok";
}
}
✍️ Authors
- Mehmet Yoldaş - Linkedin
See also the list of contributors who participated in this project.
🎉 Acknowledgements
Thank you for using my library.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net9.0 is compatible. 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. |
-
net9.0
- Chd.Library.Common (>= 9.1.0)
- Chd.Library.Logging (>= 9.1.7)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
- Ninject (>= 3.3.6)
- websocketsharp.core (>= 1.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.