Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a websocket server in .net core console application

Is there any way to create a .NET Core console application which can host a WebSocket server?

I see a lot of stuff but only for using with ASP.NET Core dependency injection.

The NuGet package I end up using must be .NET Core and not full .NET.

If I can use Microsoft.AspNetCore.WebSockets in a console application, how would I do it?

like image 323
Andy Avatar asked Jan 19 '18 14:01

Andy


People also ask

How do I create a WebSocket server?

Creating a WebSocket objectThe URL to which to connect; this should be the URL to which the WebSocket server will respond. This should use the URL scheme wss:// , although some software may allow you to use the insecure ws:// for local connections. Either a single protocol string or an array of protocol strings.

What is WebSocket in .NET core?

WebSocket (RFC 6455) is a protocol that enables two-way persistent communication channels over TCP connections. It's used in apps that benefit from fast, real-time communication, such as chat, dashboard, and game apps. View or download sample code (how to download, how to run).

How do I create a WebSocket server in node?

const webSocket = new WebSocket('ws://localhost:443/'); And then we can create an event for the WebSocket to do something when it gets data. That's it we can now receive data from the WebSocket server.

What is difference between SignalR and WebSocket?

WebSockets is actually the underlying transport that SignalR uses, at least most of the time. SignalR has the ability to fall back to other ways of transporting messages, such as long-polling over HTTP. This is useful in situations where you don't have WebSockets support.


1 Answers

Self-hosted ASP.net Core applications are in fact console applications, using Kestrel as the server you can run it in non-blocking and continue the program as a regular console one, something like this:

public static void Main(string[] args)
{

    var host = new WebHostBuilder()
        .UseKestrel()
        .Build();                     //Modify the building per your needs

    host.Start();                     //Start server non-blocking

    //Regular console code
    while (true)
    {
        Console.WriteLine(Console.ReadLine());
    }
}

The only downside of this is you will get some debug messages at the begining, but you can supress those with this modification:

public static void Main(string[] args)
{

    ConsOut = Console.Out;  //Save the reference to the old out value (The terminal)
    Console.SetOut(new StreamWriter(Stream.Null)); //Remove console output

    var host = new WebHostBuilder()
        .UseKestrel()
        .Build();                     //Modify the building per your needs

    host.Start();                     //Start server non-blocking

    Console.SetOut(ConsOut);          //Restore output

    //Regular console code
    while (true)
    {
        Console.WriteLine(Console.ReadLine());
    }
}

Source about the console output.

like image 101
Gusman Avatar answered Sep 21 '22 11:09

Gusman