Last active
May 15, 2023 05:31
-
-
Save davidfowl/c6e4934e49a2045b5b05ed0fa7e8d6b4 to your computer and use it in GitHub Desktop.
Simple WS ASP.NET Core
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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