mirror of
https://github.com/yawaflua/WebSockets.git
synced 2025-12-09 20:09:32 +02:00
Adds example of using preConnectionHandler, create OnConnectionHandler(preConnectionHandler), use WebSocketOptions for provides data
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
using yawaflua.WebSockets;
|
||||
using System.Net.WebSockets;
|
||||
using yawaflua.WebSockets;
|
||||
using yawaflua.WebSockets.Core;
|
||||
using yawaflua.WebSockets.Models.Interfaces;
|
||||
|
||||
namespace Examples;
|
||||
|
||||
@@ -26,6 +29,19 @@ internal class Startup
|
||||
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());
|
||||
|
||||
Reference in New Issue
Block a user