Skip to content

Commit

Permalink
[OneBot] log the information received and sent
Browse files Browse the repository at this point in the history
  • Loading branch information
Linwenxuan authored and Linwenxuan committed Oct 17, 2023
1 parent a4fa6fb commit 188e65c
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions Lagrange.OneBot/Core/Network/ReverseWSService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace Lagrange.OneBot.Core.Network;

public sealed class ReverseWSService : ILagrangeWebService
{
private const string Tag = nameof(ReverseWSService);

public event EventHandler<MsgRecvEventArgs> OnMessageReceived = delegate { };

private readonly WebsocketClient _socket;
Expand Down Expand Up @@ -45,7 +47,11 @@ public ReverseWSService(IConfiguration config, ILogger<LagrangeApp> logger)
});

_timer = new Timer(OnHeartbeat, null, int.MaxValue, config.GetValue<int>("Implementation:ReverseWebSocket:HeartBeatInterval"));
_socket.MessageReceived.Subscribe(resp => OnMessageReceived.Invoke(this, new(resp.Text ?? "")));
_socket.MessageReceived.Subscribe(resp =>
{
_logger.LogTrace($"[{Tag}] Receive: {resp.Text}");
OnMessageReceived.Invoke(this, new MsgRecvEventArgs(resp.Text ?? ""));
});
}

public async Task StartAsync(CancellationToken cancellationToken)
Expand All @@ -66,7 +72,9 @@ public Task StopAsync(CancellationToken cancellationToken)

public Task SendJsonAsync<T>(T json, CancellationToken cancellationToken = default)
{
var payload = JsonSerializer.SerializeToUtf8Bytes(json);
string payload = JsonSerializer.Serialize(json);

_logger.LogTrace($"[{Tag}] Send: {payload}");
return _socket.SendInstant(payload);
}

Expand Down

0 comments on commit 188e65c

Please sign in to comment.