Skip to content

Instantly share code, notes, and snippets.

@davidfowl
Last active May 15, 2023 05:31
Show Gist options
  • Save davidfowl/c6e4934e49a2045b5b05ed0fa7e8d6b4 to your computer and use it in GitHub Desktop.
Save davidfowl/c6e4934e49a2045b5b05ed0fa7e8d6b4 to your computer and use it in GitHub Desktop.
Simple WS ASP.NET Core
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
namespace SimpleWebSockets
{
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseUrls("http://+:80")
.Configure(app =>
{
app.UseWebSockets();
app.Run(async context =>
{
// Only support websocket requests
if (!context.WebSockets.IsWebSocketRequest)
{
context.Response.StatusCode = 400;
return;
}
using (var webSocket = await context.WebSockets.AcceptWebSocketAsync())
{
// Do WebSocket things
}
});
})
.Build();
host.Run();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment