-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
58 lines (53 loc) · 1.78 KB
/
Program.cs
File metadata and controls
58 lines (53 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System.Net.WebSockets;
using yawaflua.WebSockets;
using yawaflua.WebSockets.Core;
using yawaflua.WebSockets.Models.Interfaces;
namespace Examples;
class Program
{
static async Task Main(string[] args)
{
await Host.CreateDefaultBuilder(args)
.ConfigureLogging(k => k.AddConsole().AddDebug())
.ConfigureWebHost(k =>
{
k.UseKestrel(l => l.ListenAnyIP(80));
k.UseStartup<Startup>();
})
.RunConsoleAsync();
}
}
internal class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.SettingUpWebSockets();
services.AddRouting();
services.AddHttpLogging();
services.AddSingleton<TestWebSocketServer>();
services.AddSingleton<ChatController>();
services.AddSingleton(new WebSocketConfig()
{
OnOpenHandler = async (socket, context) =>
{
if (socket.WebSocketManager!.GetAllClients().Count(k =>
Equals(k.ConnectionInfo!.RemoteIpAddress!.MapToIPv4(),
socket.Client.ConnectionInfo!.RemoteIpAddress!.MapToIPv4())) >= 3)
{
await socket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Too many users");
}
Console.WriteLine($"{socket.Client.Id} has been connected to {socket.Client.Path}");
}
});
services.AddScoped<IConfiguration>(k => new ConfigurationBuilder()
.AddJsonFile("appsettings.json", true)
.Build());
}
public void Configure(IApplicationBuilder app)
{
app.ConnectWebSockets();
app.UseRouting();
app.UseHttpLogging();
app.UseWelcomePage();
}
}