dotnet / WatsonWebserver

Watson is the fastest, easiest way to build scalable RESTful web servers and services in C#.
MIT License
403 stars 83 forks source link

How to listen to incoming messages to Server and send messages from Server to Clients? #83

Closed saklanmaz closed 2 years ago

saklanmaz commented 2 years ago

Hi,

I am opening a server as below. With which activity can I listen to the requests coming to this server? How can I send a message from the server? Is it possible to assign Receivedfunction and send data like .Send() as in SuperSimpleTcp?

public bool Connect(Server httpServer)
        {
            try
            {
                httpServer.Events.ConnectionReceived += Events_ConnectionReceived;
                httpServer.Events.ExceptionEncountered += Events_ExceptionEncountered;
                httpServer.Events.RequestDenied += Events_RequestDenied;
                httpServer.Events.RequestReceived += Events_RequestReceived;
                httpServer.Events.ServerDisposing += Events_ServerDisposing;

                httpServer.Start();
                return httpServer.IsListening;
            }
            catch
            {
                return false;
            }
        }
jchristn commented 2 years ago

Hi @saklanmaz a simple example is shown below from the README. You have to define a default route.

using System.IO;
using System.Text;
using WatsonWebserver;

static void Main(string[] args)
{
  Server server = new Server("127.0.0.1", 9000, false, DefaultRoute);
  server.Start();
  Console.ReadLine();
}

static async Task DefaultRoute(HttpContext ctx)
{  
  ctx.Response.StatusCode = 200;
  await ctx.Response.Send("Hello from the default route!");
}

Then, open your browser to http://127.0.0.1:9000/.

saklanmaz commented 2 years ago

I'm sorry, it was applied beautifully and simply, it escaped my notice. Thank you for the library.

jchristn commented 2 years ago

Very glad to help @saklanmaz please let me know if anything else comes up! And thanks for the kind words.