Closed mika76 closed 2 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 .
I will give it a try, but I meant websockets directly, not signalR - sometimes just the basics are required :)
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.
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 .
Brilliant thanks @davidfowl - @jchannon this issue could be closed unless you wanna keep it open for the sample docs?
We can keep it open until someone ( 😉 ) adds it to the sample app
Would be great to be able to handle web sockets in a carter way, or is this possible already?