Skip to content

Commit

Permalink
Sync
Browse files Browse the repository at this point in the history
  • Loading branch information
Zaitonn committed Oct 17, 2023
2 parents ed7e47a + 188e65c commit 80f8b48
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Lagrange.Core/BotContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 12 additions & 2 deletions Lagrange.OneBot/Core/Network/ForwardWSService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ namespace Lagrange.OneBot.Core.Network;

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

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

private readonly WebsocketServer _server;
Expand Down Expand Up @@ -71,7 +73,14 @@ public async Task SendJsonAsync<T>(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);
Expand All @@ -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
Expand All @@ -101,7 +110,7 @@ e.RequestHeaders is null
)
)
{
e.Connection.Websocket.Abort();
await _server.DisconnectConnectionAsync(e.Connection);
}
}

Expand All @@ -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));
}
}
Expand Down
4 changes: 4 additions & 0 deletions Lagrange.OneBot/Core/Network/ReverseWSService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,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 _websocketClient;
Expand Down Expand Up @@ -74,6 +76,7 @@ public async Task StopAsync(CancellationToken cancellationToken)
public Task SendJsonAsync<T>(T json, CancellationToken cancellationToken = default)
{
var payload = JsonSerializer.SerializeToUtf8Bytes(json);
_logger.LogTrace($"[{Tag}] Send: {payload}");
return _websocketClient.SendAsync(payload);
}

Expand All @@ -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 ?? ""));
}
}
Expand Down

0 comments on commit 80f8b48

Please sign in to comment.