SimpleAsyncHttpServer 0.0.2

dotnet add package SimpleAsyncHttpServer --version 0.0.2
                    
NuGet\Install-Package SimpleAsyncHttpServer -Version 0.0.2
                    
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="SimpleAsyncHttpServer" Version="0.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SimpleAsyncHttpServer" Version="0.0.2" />
                    
Directory.Packages.props
<PackageReference Include="SimpleAsyncHttpServer" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add SimpleAsyncHttpServer --version 0.0.2
                    
#r "nuget: SimpleAsyncHttpServer, 0.0.2"
                    
#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.
#:package SimpleAsyncHttpServer@0.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=SimpleAsyncHttpServer&version=0.0.2
                    
Install as a Cake Addin
#tool nuget:?package=SimpleAsyncHttpServer&version=0.0.2
                    
Install as a Cake Tool

Simple async http server

Http Example

    private class ExampleExtension : IHttpServerExtension
    {
        private Random m_Random = new Random();
        public bool Execute(HttpListenerContext Context)
        {   
            //This will allow the page to load
            //return true;
            //It is not 
            //return false;
            return m_Random.Next(2) == 1;
        }
    }
    private class Program
    {
        static void Main(string[] args)
        {
            HttpServer Server = new HttpServer(new string[] { "http://*:80/"});
            //Adds route
            Server.AddRoute("/", (req, res) =>
            {
                //Send text
                res.SendAndClose("Hello world!");
                //Send embed resource
                //res.SendEmbedResourceAndClose("Namespace.Folder.file.type");
                //Send file in relative path
                //res.SendFile("/resources/index.html");
            });
            //Example of extension
            Server.AddRoute("/extension", (req, res) => {
                res.SendAndClose("You are lucky!");
            }, new IHttpServerExtension[] {
                new ExampleExtension();
            });
            //Delete route. After that, the page will be unavailable
            //Server.RemoveRoute("/");
            
            while (true);
        }
    }

WebSocket Example

    //inheritance from the SocketEvent class is required
    private class MessageEvent : SocketEvent 
    {
        public string Message = string.Empty;
        public MessageEvent(string Message) : base("chat-message")
        {
            this.Message = Message;
        }
    }
    private class Program
    {
        static void Main(string[] args)
        {
            WebSocketServer Server = new WebSocketServer(new string[] { "http://*:80/ws"});
            
            //This is called when the correct SocketEvent was received and the Type of the given SocketEvent was equal to 1 argument. Second argument is a callback. Message is a json that received from client. Client is client
            WebSocketServer.On("chat-message", (Message, Client) =>
            {
                //Convert received json from client
                MessageEvent Event = JsonConvert.DeserializeObject(Message);
                if(Event != null)
                {
                    //Send message 
                    WebSocketServer.EmitAll(Event);
                    //Also you can emit to one client
                    //WebSocketServer.Emit(Event, Client);
                }
            });
            
            while (true);
        }
    }

Also this package have class Logger that helps log with queque

Info - Logger.LogInfo("Hello world!");

Warning - Logger.LogWarning("Hellow world!");

Error - Logger.LogError("Hello world!");

Product Compatible and additional computed target framework versions.
.NET Framework net472 is compatible.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

This package has 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.

Version Downloads Last Updated
0.0.2 126 6/7/2024
0.0.1 123 5/26/2024

Main