CarterCommunity / Carter

Carter is framework that is a thin layer of extension methods and functionality over ASP.NET Core allowing code to be more explicit and most importantly more enjoyable.
MIT License
2.11k stars 174 forks source link

Websocket handling #241

Closed mika76 closed 2 years ago

mika76 commented 4 years ago

Would be great to be able to handle web sockets in a carter way, or is this possible already?

jchannon commented 4 years ago

Not tried but it might just work if you have a route for /ws for example and use signalr

On Sat, 7 Mar 2020 at 08:33, Mladen Mihajlović notifications@github.com wrote:

Would be great to be able to handle web sockets in a carter way, or is this possible already?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/CarterCommunity/Carter/issues/241?email_source=notifications&email_token=AAAZVJXVSVJGJ2SSJPJNZEDRGIBD5A5CNFSM4LDNVK2KYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4ITI5I4Q, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAZVJQUMODPQJNKYRQ2O5LRGIBD5ANCNFSM4LDNVK2A .

mika76 commented 4 years ago

I will give it a try, but I meant websockets directly, not signalR - sometimes just the basics are required :)

davidfowl commented 4 years ago

You can do Websockets with Carter like you would with any ASP.NET Core application.

public class WebSocketModule : CarterModule
{
    public WebSocketModule()
    {
        Get("/ws", async http =>
        {
            if (!http.WebSockets.IsWebSocketRequest)
            {
                http.Response.StatusCode = 400;
                return;
            }

            var websocket = await http.WebSockets.AcceptWebSocketAsync();
            await Echo(websocket);

        });

        static async Task Echo(WebSocket webSocket)
        {
            var buffer = new byte[1024 * 4];
            WebSocketReceiveResult result = await webSocket.ReceiveAsync(buffer, CancellationToken.None);
            while (!result.CloseStatus.HasValue)
            {
                await webSocket.SendAsync(buffer.AsMemory(0, result.Count), result.MessageType, result.EndOfMessage, CancellationToken.None);

                result = await webSocket.ReceiveAsync(buffer, CancellationToken.None);
            }
            await webSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None);
        }
    }
}

See https://docs.microsoft.com/en-us/aspnet/core/fundamentals/websockets?view=aspnetcore-3.1 for more information.

jchannon commented 4 years ago

Awesome. Thanks. Might be worth adding to the sample project

On Sat, 14 Mar 2020 at 23:18, David Fowler notifications@github.com wrote:

You can do Websockets with Carter like you would with any ASP.NET Core application.

public class WebSocketModule : CarterModule { public WebSocketModule() { Get("/ws", async http => { if (!http.WebSockets.IsWebSocketRequest) { http.Response.StatusCode = 400; return; }

        var websocket = await http.WebSockets.AcceptWebSocketAsync();
        await Echo(websocket);

    });

    static async Task Echo(WebSocket webSocket)
    {
        var buffer = new byte[1024 * 4];
        WebSocketReceiveResult result = await webSocket.ReceiveAsync(buffer, CancellationToken.None);
        while (!result.CloseStatus.HasValue)
        {
            await webSocket.SendAsync(buffer.AsMemory(0, result.Count), result.MessageType, result.EndOfMessage, CancellationToken.None);

            result = await webSocket.ReceiveAsync(buffer, CancellationToken.None);
        }
        await webSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None);
    }
}

}

See https://docs.microsoft.com/en-us/aspnet/core/fundamentals/websockets?view=aspnetcore-3.1 for more information.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/CarterCommunity/Carter/issues/241#issuecomment-599146652, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAZVJT77ZBBGPDBPRUJDZDRHQGDNANCNFSM4LDNVK2A .

mika76 commented 4 years ago

Brilliant thanks @davidfowl - @jchannon this issue could be closed unless you wanna keep it open for the sample docs?

jchannon commented 4 years ago

We can keep it open until someone ( 😉 ) adds it to the sample app