diff --git a/Lagrange.Core/BotContext.cs b/Lagrange.Core/BotContext.cs index ea6b42bff..a7022bf3d 100644 --- a/Lagrange.Core/BotContext.cs +++ b/Lagrange.Core/BotContext.cs @@ -8,6 +8,10 @@ public class BotContext : IDisposable { public readonly EventInvoker Invoker; + public uint BotUin => ContextCollection.Keystore.Uin; + + public string? BotName => ContextCollection.Keystore.Info?.Name; + internal readonly Utility.TaskScheduler Scheduler; internal readonly ContextCollection ContextCollection; diff --git a/Lagrange.OneBot/Core/Network/ForwardWSService.cs b/Lagrange.OneBot/Core/Network/ForwardWSService.cs index d2fcf04bf..02507f2f4 100644 --- a/Lagrange.OneBot/Core/Network/ForwardWSService.cs +++ b/Lagrange.OneBot/Core/Network/ForwardWSService.cs @@ -14,6 +14,8 @@ namespace Lagrange.OneBot.Core.Network; public sealed class ForwardWSService : ILagrangeWebService { + private const string Tag = nameof(ForwardWSService); + public event EventHandler OnMessageReceived = delegate { }; private readonly WebsocketServer _server; @@ -71,7 +73,14 @@ public async Task SendJsonAsync(T json, CancellationToken cancellationToken = ); if (connection is not null) + { + _logger.LogTrace($"[{Tag}] Send to {connection.ConnectionId}: {payload}"); await _server.SendToConnectionAsync(payload, connection); + } + else + _logger.LogWarning( + $"[{Tag}] Fail to send to {oneBotResult.Identifier} because the connection has been aborted." + ); } else await _server.BroadcastToAllConnectionsAsync(payload, cancellationToken); @@ -89,7 +98,7 @@ private void OnHeartbeat(object? sender) SendJsonAsync(heartBeat).GetAwaiter().GetResult(); } - private void OnConnection(object sender, WSConnectionServerEventArgs e) + private async void OnConnection(object sender, WSConnectionServerEventArgs e) { if ( _shouldAuthenticate @@ -101,7 +110,7 @@ e.RequestHeaders is null ) ) { - e.Connection.Websocket.Abort(); + await _server.DisconnectConnectionAsync(e.Connection); } } @@ -110,6 +119,7 @@ private void OnMessage(object sender, WSMessageServerEventArgs e) if (e.MessageEventType == MessageEventType.Receive) { string text = _utf8.GetString(e.Bytes); + _logger.LogTrace($"[{Tag}] Receive from {e.Connection.ConnectionId}: {text}"); OnMessageReceived.Invoke(this, new(e.Message ?? "", e.Connection.ConnectionId)); } } diff --git a/Lagrange.OneBot/Core/Network/ReverseWSService.cs b/Lagrange.OneBot/Core/Network/ReverseWSService.cs index 6adff6909..8a217dfcc 100644 --- a/Lagrange.OneBot/Core/Network/ReverseWSService.cs +++ b/Lagrange.OneBot/Core/Network/ReverseWSService.cs @@ -13,6 +13,8 @@ namespace Lagrange.OneBot.Core.Network; public sealed class ReverseWSService : ILagrangeWebService { + private const string Tag = nameof(ReverseWSService); + public event EventHandler OnMessageReceived = delegate { }; private readonly WebsocketClient _websocketClient; @@ -74,6 +76,7 @@ public async Task StopAsync(CancellationToken cancellationToken) public Task SendJsonAsync(T json, CancellationToken cancellationToken = default) { var payload = JsonSerializer.SerializeToUtf8Bytes(json); + _logger.LogTrace($"[{Tag}] Send: {payload}"); return _websocketClient.SendAsync(payload); } @@ -94,6 +97,7 @@ private void OnMessage(object sender, WSMessageClientEventArgs e) if (e.MessageEventType == MessageEventType.Receive) { string text = _utf8.GetString(e.Bytes); + _logger.LogTrace($"[{Tag}] Receive: {text}"); OnMessageReceived.Invoke(this, new(e.Message ?? "")); } }