feat:更新.net网络连接文档
This commit is contained in:
@@ -54,6 +54,7 @@ var host = Host.CreateDefaultBuilder(args)
|
||||
{
|
||||
services.AddSingleton<IPackageHandler<ProtocolFrame>, TcpPackageHandler>();
|
||||
services.AddSingleton<WebSocketMessageHandler>();
|
||||
services.AddSingleton<WebSocketConnectionManager>();
|
||||
|
||||
// 1. 注入 MQTT 配置
|
||||
services.Configure<MqttSettings>(context.Configuration.GetSection("Mqtt"));
|
||||
@@ -70,6 +71,26 @@ var host = Host.CreateDefaultBuilder(args)
|
||||
.AddWebSocketServer(builder =>
|
||||
{
|
||||
builder
|
||||
.UseSessionHandler(
|
||||
session =>
|
||||
{
|
||||
if (session is WebSocketSession wsSession)
|
||||
{
|
||||
var connectionManager = session.Server.ServiceProvider.GetRequiredService<WebSocketConnectionManager>();
|
||||
connectionManager.Add(wsSession);
|
||||
}
|
||||
return ValueTask.CompletedTask;
|
||||
},
|
||||
(session, reason) =>
|
||||
{
|
||||
if (session is WebSocketSession wsSession)
|
||||
{
|
||||
var connectionManager = session.Server.ServiceProvider.GetRequiredService<WebSocketConnectionManager>();
|
||||
connectionManager.Remove(wsSession);
|
||||
}
|
||||
return ValueTask.CompletedTask;
|
||||
}
|
||||
)
|
||||
.UseWebSocketMessageHandler(async (session, package) =>
|
||||
{
|
||||
using var scope = session.Server.ServiceProvider.CreateScope();
|
||||
@@ -198,8 +219,34 @@ class TcpPackageHandler(ILogger<TcpPackageHandler> logger) : IPackageHandler<Pro
|
||||
|
||||
## 2.4 TCPService
|
||||
```cs
|
||||
class TcpService(IServiceProvider serviceProvider, IOptions<ServerOptions> serverOptions) : SuperSocketService<ProtocolFrame>(serviceProvider, serverOptions)
|
||||
class TcpService(IServiceProvider serviceProvider, IOptions<ServerOptions> serverOptions, ILogger<TcpService> logger) : SuperSocketService<ProtocolFrame>(serviceProvider, serverOptions)
|
||||
{
|
||||
private readonly ILogger<TcpService> _logger = logger;
|
||||
|
||||
private readonly ConcurrentDictionary<string, string> _sessionDevices = new();
|
||||
|
||||
public void BindDevice(string sessionId, string deviceNum)
|
||||
{
|
||||
_sessionDevices.AddOrUpdate(sessionId, deviceNum, (k, v) => deviceNum);
|
||||
}
|
||||
|
||||
protected override async ValueTask OnSessionConnectedAsync(IAppSession session)
|
||||
{
|
||||
_logger.LogInformation("TCP Session 连上: {SessionID}", session.SessionID);
|
||||
await base.OnSessionConnectedAsync(session);
|
||||
}
|
||||
|
||||
protected override async ValueTask OnSessionClosedAsync(IAppSession session, CloseEventArgs e)
|
||||
{
|
||||
_logger.LogInformation("TCP Session 断开: {SessionID}, Reason={Reason}", session.SessionID, e.Reason);
|
||||
|
||||
if (_sessionDevices.TryRemove(session.SessionID, out var deviceNum))
|
||||
{
|
||||
_logger.LogInformation("设备 {DeviceNum} 标记离线", deviceNum);
|
||||
}
|
||||
|
||||
await base.OnSessionClosedAsync(session, e);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -218,6 +265,40 @@ class WebSocketMessageHandler(ILogger<WebSocketMessageHandler> logger)
|
||||
}
|
||||
```
|
||||
|
||||
## 3.2 连接管理
|
||||
```cs
|
||||
public class WebSocketConnectionManager(ILogger<WebSocketConnectionManager> logger)
|
||||
{
|
||||
private readonly ConcurrentDictionary<string, WebSocketSession> _sessions = new();
|
||||
private readonly ILogger<WebSocketConnectionManager> _logger = logger;
|
||||
|
||||
public void Add(WebSocketSession session) => _sessions.TryAdd(session.SessionID, session);
|
||||
|
||||
public void Remove(WebSocketSession session) => _sessions.TryRemove(session.SessionID, out _);
|
||||
|
||||
public async Task BroadcastAsync(string json)
|
||||
{
|
||||
var payload = new ReadOnlyMemory<byte>(System.Text.Encoding.UTF8.GetBytes(json));
|
||||
|
||||
var tasks = _sessions.Values
|
||||
.Where(s => s.State == SessionState.Connected)
|
||||
.Select(async s =>
|
||||
{
|
||||
try
|
||||
{
|
||||
await s.SendAsync(json);
|
||||
}
|
||||
catch
|
||||
{
|
||||
Remove(s);
|
||||
}
|
||||
});
|
||||
|
||||
await Task.WhenAll(tasks);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
# 四、 MQTT服务器
|
||||
## 4.1 MqttService
|
||||
```cs
|
||||
|
||||
Reference in New Issue
Block a user