mirror of
https://github.com/yawaflua/WebSockets.git
synced 2025-12-08 19:39:30 +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;
|
namespace Examples;
|
||||||
|
|
||||||
@@ -26,6 +29,19 @@ internal class Startup
|
|||||||
services.AddHttpLogging();
|
services.AddHttpLogging();
|
||||||
services.AddSingleton<TestWebSocketServer>();
|
services.AddSingleton<TestWebSocketServer>();
|
||||||
services.AddSingleton<ChatController>();
|
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()
|
services.AddScoped<IConfiguration>(k => new ConfigurationBuilder()
|
||||||
.AddJsonFile("appsettings.json", true)
|
.AddJsonFile("appsettings.json", true)
|
||||||
.Build());
|
.Build());
|
||||||
|
|||||||
@@ -112,6 +112,7 @@ public class ChatController : WebSocketController
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Run any code on connection to
|
||||||
|
|
||||||
## Lifecycle Management
|
## Lifecycle Management
|
||||||
1. **Connection** - Automatically handled by middleware
|
1. **Connection** - Automatically handled by middleware
|
||||||
|
|||||||
@@ -11,18 +11,24 @@ internal class WebSocket : IWebSocket
|
|||||||
private readonly string? _message;
|
private readonly string? _message;
|
||||||
|
|
||||||
public WebSocketState State => _webSocket.State;
|
public WebSocketState State => _webSocket.State;
|
||||||
|
public IWebSocketManager WebSocketManager { get; }
|
||||||
public WebSocketCloseStatus? CloseStatus => _webSocket.CloseStatus;
|
public WebSocketCloseStatus? CloseStatus => _webSocket.CloseStatus;
|
||||||
public string? SubProtocol => _webSocket.SubProtocol;
|
public string? SubProtocol => _webSocket.SubProtocol;
|
||||||
public string? CloseStatusDescription => _webSocket.CloseStatusDescription;
|
public string? CloseStatusDescription => _webSocket.CloseStatusDescription;
|
||||||
public string? Message => _message;
|
public string? Message => _message;
|
||||||
public WebSocketMessageType? MessageType => _webSocketReceiveResult?.MessageType;
|
public WebSocketMessageType? MessageType => _webSocketReceiveResult?.MessageType;
|
||||||
public IWebSocketClient Client { get; }
|
public IWebSocketClient Client { get; }
|
||||||
internal WebSocket(System.Net.WebSockets.WebSocket webSocket, WebSocketReceiveResult? webSocketReceiveResult, string? message, IWebSocketClient client)
|
internal WebSocket(System.Net.WebSockets.WebSocket webSocket,
|
||||||
|
IWebSocketClient client,
|
||||||
|
IWebSocketManager webSocketManager,
|
||||||
|
string? message = null,
|
||||||
|
WebSocketReceiveResult? webSocketReceiveResult = null)
|
||||||
{
|
{
|
||||||
_webSocket = webSocket;
|
_webSocket = webSocket;
|
||||||
_webSocketReceiveResult = webSocketReceiveResult;
|
_webSocketReceiveResult = webSocketReceiveResult;
|
||||||
_message = message;
|
_message = message;
|
||||||
Client = client;
|
Client = client;
|
||||||
|
WebSocketManager = webSocketManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task SendAsync(string m, WebSocketMessageType messageType = WebSocketMessageType.Text, CancellationToken cts = default)
|
public async Task SendAsync(string m, WebSocketMessageType messageType = WebSocketMessageType.Text, CancellationToken cts = default)
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ public class WebSocketRouter
|
|||||||
|
|
||||||
var parameters = func.GetParameters();
|
var parameters = func.GetParameters();
|
||||||
if (parameters.Length != 2 ||
|
if (parameters.Length != 2 ||
|
||||||
parameters[0].ParameterType != typeof(WebSocket) ||
|
parameters[0].ParameterType != typeof(IWebSocket) ||
|
||||||
parameters[1].ParameterType != typeof(HttpContext) ||
|
parameters[1].ParameterType != typeof(HttpContext) ||
|
||||||
func.ReturnType != typeof(Task))
|
func.ReturnType != typeof(Task))
|
||||||
{
|
{
|
||||||
@@ -147,13 +147,14 @@ public class WebSocketRouter
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
var webSocketManager = new WebSocketManager();
|
||||||
var client = new WebSocketClient(context, webSocket, path);
|
var client = new WebSocketClient(context, webSocket, path);
|
||||||
Clients.Add(client);
|
Clients.Add(client);
|
||||||
|
|
||||||
await Task.Run(async () =>
|
await Task.Run(async () =>
|
||||||
{
|
{
|
||||||
if (_webSocketConfig?.OnOpenHandler != null)
|
if (_webSocketConfig?.OnOpenHandler != null)
|
||||||
await _webSocketConfig.OnOpenHandler((webSocket as IWebSocket)!, context);
|
await _webSocketConfig.OnOpenHandler(new WebSocket(webSocket, client, webSocketManager)!, context);
|
||||||
}, cts);
|
}, cts);
|
||||||
|
|
||||||
var buffer = new byte[1024 * 4];
|
var buffer = new byte[1024 * 4];
|
||||||
@@ -164,13 +165,16 @@ public class WebSocketRouter
|
|||||||
await handler(
|
await handler(
|
||||||
new WebSocket(
|
new WebSocket(
|
||||||
webSocket,
|
webSocket,
|
||||||
result,
|
client,
|
||||||
|
webSocketManager,
|
||||||
Encoding.UTF8.GetString(buffer, 0, result.Count),
|
Encoding.UTF8.GetString(buffer, 0, result.Count),
|
||||||
client),
|
result), context);
|
||||||
context);
|
|
||||||
else
|
else
|
||||||
Clients.Remove(client);
|
Clients.Remove(client);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (Clients.Any(k => k.Id == client.Id))
|
||||||
|
Clients.Remove(client);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
using System.Net.WebSockets;
|
using System.Net.WebSockets;
|
||||||
|
using yawaflua.WebSockets.Core;
|
||||||
|
|
||||||
namespace yawaflua.WebSockets.Models.Interfaces;
|
namespace yawaflua.WebSockets.Models.Interfaces;
|
||||||
|
|
||||||
public interface IWebSocket : IDisposable
|
public interface IWebSocket : IDisposable
|
||||||
{
|
{
|
||||||
WebSocketState State { get; }
|
WebSocketState State { get; }
|
||||||
|
IWebSocketManager WebSocketManager { get; }
|
||||||
WebSocketCloseStatus? CloseStatus { get; }
|
WebSocketCloseStatus? CloseStatus { get; }
|
||||||
string? SubProtocol { get; }
|
string? SubProtocol { get; }
|
||||||
string? CloseStatusDescription { get; }
|
string? CloseStatusDescription { get; }
|
||||||
|
|||||||
@@ -8,12 +8,14 @@ namespace yawaflua.WebSockets;
|
|||||||
|
|
||||||
public static class ServiceBindings
|
public static class ServiceBindings
|
||||||
{
|
{
|
||||||
public static IServiceCollection SettingUpWebSockets(this IServiceCollection isc, Action<WebSocketOptions>? socketOptions = null)
|
public static IServiceCollection SettingUpWebSockets(this IServiceCollection isc, WebSocketConfig? socketOptions = null)
|
||||||
{
|
{
|
||||||
isc.AddSingleton<WebSocketRouter>();
|
isc.AddSingleton<WebSocketRouter>();
|
||||||
|
if (socketOptions != null) isc.AddSingleton(socketOptions);
|
||||||
|
if (isc.All(k => k.ServiceType != typeof(WebSocketConfig)))
|
||||||
|
isc.AddSingleton(new WebSocketConfig());
|
||||||
isc.AddScoped<IWebSocketManager, WebSocketManager>();
|
isc.AddScoped<IWebSocketManager, WebSocketManager>();
|
||||||
isc.AddSingleton<WebSocketMiddleware>();
|
isc.AddSingleton<WebSocketMiddleware>();
|
||||||
isc.Configure("WebSocketOptions", socketOptions);
|
|
||||||
return isc;
|
return isc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user