Chd.Library.Web.Socket 9.2.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package Chd.Library.Web.Socket --version 9.2.1                
NuGet\Install-Package Chd.Library.Web.Socket -Version 9.2.1                
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="Chd.Library.Web.Socket" Version="9.2.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Chd.Library.Web.Socket --version 9.2.1                
#r "nuget: Chd.Library.Web.Socket, 9.2.1"                
#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 Chd.Library.Web.Socket as a Cake Addin
#addin nuget:?package=Chd.Library.Web.Socket&version=9.2.1

// Install Chd.Library.Web.Socket as a Cake Tool
#tool nuget:?package=Chd.Library.Web.Socket&version=9.2.1                

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

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

See also the list of contributors who participated in this project.

🎉 Acknowledgements

Thank you for using my library.

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
9.2.3 69 1/30/2025
9.2.2 71 1/19/2025
9.2.1 67 1/17/2025
9.2.0 65 1/17/2025
9.1.9 68 1/17/2025
9.1.8 77 12/30/2024
9.1.7 90 12/29/2024
8.0.2 79 12/24/2024
7.3.0 177 8/9/2023
1.3.0 170 5/28/2023