Skip to content

Instantly share code, notes, and snippets.

@andrijac
Forked from davidfowl/websockets.cs
Created November 18, 2019 11:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrijac/c66d3593ef586d637c34ec1fdfd70380 to your computer and use it in GitHub Desktop.
Save andrijac/c66d3593ef586d637c34ec1fdfd70380 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