Enable HTTP communication on your server - marcos8154/SocketAppServer GitHub Wiki

Starting with version 1.5.1 of the framework, support for third-party extensions has been added to add new functionality to the MobileAppServer framework

The first extension launched is HttpServer, which can be easily obtained via Nuget, with just one line of code to enable it on your Socket server

You can get it by searching the nuget manually for "MobileAppServer.Extensions.HttpServer" or via the package manager command prompt as follows:

"Install-Package MobileAppServer.Extensions.HttpServer -Version 1.0.0" | or higher

This extension will install some additional dependencies like AspNet Self-Hosting and others

Having installed the extension package, let's go to the code

static void Main(string[] args)
{
    Server server = new Server();
    server.Port = 4000;
    server.BufferSize = 10000;
    server.ServerEncoding = Encoding.UTF8;
    server.RegisterController("ContatosController", typeof(ContatosController));
    server.RegisterModelType(typeof(Contato));
    server.EnableExtension(new HttpModule("http://localhost:9000"));
    server.Start();
}

The EnableExtension() method

This method is responsible for enabling third-party extensions and attaching them to your server

It requests as an parameter an instance of the "IExtensibleFrameworkInterface" interface, available in any extension package

You must inform the HttpModule builder the base address of the HTTP server, along with its port, as shown in the previous snippet

That alone is enough for you to have an HTTP server attached to your Socket Server. You should now forward the requests to the http server, which in turn will redirect them to the server socket

The HttpModule class

This class will be responsible for raising the http server and making POST and GET methods available (in future versions more http verbs will be supported)

Making http requests to your server

Below is an example using Postman of what a POST HTTP request should look like for your server:

HTTP-POST Requests

You must call uri as follows: http://yourserveraddress/api/http

HTTP-GET Requests

In the case of HTTP-GET, you must call the URI /api/http/{ControllerName}/{ActionName}, and if there are any parameters in the action, they must be passed in the request body as a Json, in the same pattern as traditional Socket calls which you are used to