Net Core MVC6 WebSockets Middleware
<p>Firstly, I know SignalR is the best websocket framework and i should use it instead of having fun with low level implementation.</p>

<p>But, Is there anyone who know how to correctly consume websocket in controller ?
I am totally green.</p>

<p>Any good tutorial will be very helpful :)</p>

<p>I've tried do sth like this.</p>

<p>Middleware </p>

<pre><code> app.UseWebSockets();

 app.Use(async (context, next) =&gt;
 {
        var http = (HttpContext)context;

        if (http.WebSockets.IsWebSocketRequest)
        {
         WebSocket webSocket = await http.WebSockets.AcceptWebSocketAsync();     
        }
         else
         {
            await next();
         }
 });
 //thanks to next(); app.UseMvc();
</code></pre>

<p>Controller</p>

<pre><code>[Route("ws")]
    public class MonitorController : Controller
    {
        HttpContext context;
        WebSocket socket;

        public MonitorController(IHttpContextAccessor accessor)
        {
            this.context = accessor.HttpContext;
            if (!this.context.WebSockets.IsWebSocketRequest)
                throw new InvalidOperationException("incoming request is not a websocket request");
        }

        [HttpGet]
        public IActionResult Get()
        {
            return new MonitorWaiter(Wait);
        }
        .... some code MonitorWaiter.....
</code></pre>

<p>localhost:5000/ws</p>

<pre><code>GET HTTP/1.1: 
Host: localhost:5000/ws
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Sec-WebSocket-Version: 13
</code></pre>

<p>During debugging it get into middleware but it skip controller.</p>

<p>Thanks a lot for some advices.</p>

<hr>

<p>I saw it can be done by adding <strong>server</strong> to handler -> app.Use(); and in controller use some <strong>client</strong> implementation, but the question remains what when i want to create <strong>a few servers</strong> ?</p>

<hr>

<p>Related post: <a href="https://stackoverflow.com/questions/25668398/using-websockets-with-asp-net-web-api">Using WebSockets with ASP.NET Web API</a></p>

<p><strong>I dont know if is it true but i think it should be used normally websocket handler than trying to do sth with api controller.</strong></p>