Skip to content

Commit

Permalink
[OneBot] 反向ws兼容host直接配置协议
Browse files Browse the repository at this point in the history
  • Loading branch information
Linwenxuan authored Jan 15, 2024
2 parents edd8d13 + 3cb5bb3 commit 822f42f
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions Lagrange.OneBot/Core/Network/Service/ReverseWSService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public partial class ReverseWSService(IOptionsSnapshot<ReverseWSServiceOptions>
: BackgroundService, ILagrangeWebService
{
protected const string Tag = nameof(ReverseWSService);

public event EventHandler<MsgRecvEventArgs>? OnMessageReceived;

protected readonly ReverseWSServiceOptions _options = options.Value;
Expand Down Expand Up @@ -42,7 +42,7 @@ public ValueTask SendJsonAsync<T>(T payload, string? identifier, CancellationTok
{
var connCtx = _connCtx ?? throw new InvalidOperationException("Reverse webSocket service was not running");
var connTask = connCtx.ConnectTask;

return !connTask.IsCompletedSuccessfully
? SendJsonAsync(connCtx.WebSocket, connTask, payload, connCtx.Token)
: SendJsonAsync(connCtx.WebSocket, payload, connCtx.Token);
Expand Down Expand Up @@ -75,13 +75,17 @@ protected ClientWebSocket CreateDefaultWebSocket()

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
string urlstr = $"ws://{_options.Host}:{_options.Port}{_options.Suffix}";
string urlstr = $"{_options.Host}:{_options.Port}{_options.Suffix}";
if (!_options.Host.StartsWith("ws://") && !_options.Host.StartsWith("wss://"))
{
urlstr = "ws://" + urlstr;
}
if (!Uri.TryCreate(urlstr, UriKind.Absolute, out var url))
{
Log.LogInvalidUrl(_logger, Tag, urlstr);
return;
}

while (true)
{
try
Expand Down Expand Up @@ -167,4 +171,4 @@ private static partial class Log
[LoggerMessage(EventId = 10, Level = LogLevel.Error, Message = "[{tag}] Invalid configuration was detected, url: {url}")]
public static partial void LogInvalidUrl(ILogger logger, string tag, string url);
}
}
}

0 comments on commit 822f42f

Please sign in to comment.