Skip to content

Instantly share code, notes, and snippets.

@BrennanConroy
Created November 3, 2021 22: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 BrennanConroy/02e8459d63305b4acaa0a021686f54c7 to your computer and use it in GitHub Desktop.
Save BrennanConroy/02e8459d63305b4acaa0a021686f54c7 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Net.WebSockets;
using System.Threading.Tasks;
namespace ConsoleApp
{
internal class Program
{
public static async Task Main(string[] args)
{
var connectionCount = 10000;
var tasks = new List<ClientWebSocket>(connectionCount);
for (var i = 0; i < connectionCount; i++)
{
var client = new ClientWebSocket();
client.Options.KeepAliveInterval = TimeSpan.Zero;
await client.ConnectAsync(new Uri("wss://localhost:5000"), default);
tasks.Add(client);
}
Console.WriteLine("All connected. Press enter to close connections.");
Console.ReadLine();
foreach (var s in tasks)
{
await s.CloseAsync(WebSocketCloseStatus.NormalClosure, "", default);
}
}
}
}
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFrameworks>net48;net5.0;net6.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net48'">
<PackageReference Include="Microsoft.Extensions.Hosting" Version="2.1.1" />
<PackageReference Include="Microsoft.AspNetCore" Version="2.1.7" />
<PackageReference Include="Microsoft.AspNetCore.Connections.Abstractions" Version="2.1.3" />
<PackageReference Include="Microsoft.AspNetCore.WebSockets" Version="2.1.7" />
</ItemGroup>
</Project>
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
namespace WebApplication7
{
public class Program
{
public static void Main(string[] args)
{
WebHost.CreateDefaultBuilder()
.UseStartup<Startup>()
.UseKestrel(o =>
{
o.ListenAnyIP(5000, o2 =>
{
o2.UseHttps();
});
})
.Build()
.Run();
}
}
}
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Connections;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
namespace WebApplication
{
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app)
{
app.UseWebSockets();
app.Run(async c =>
{
if (!c.WebSockets.IsWebSocketRequest)
{
return;
}
var ws = await c.WebSockets.AcceptWebSocketAsync();
#if !NET48
var buffer = ArraySegment<byte>.Empty;
#else
var buffer = new ArraySegment<byte>(new byte[0]);
#endif
await ws.ReceiveAsync(buffer, default);
await ws.CloseAsync(System.Net.WebSockets.WebSocketCloseStatus.NormalClosure, "", default);
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment