-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Implement WebSocket server skeleton for external integrations #37335
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| // Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. | ||
| // See the LICENCE file in the repository root for full licence text. | ||
|
|
||
| using Newtonsoft.Json; | ||
|
|
||
| namespace osu.Desktop.IPC.Messages | ||
| { | ||
| public class HitCountMessage : OsuWebSocketMessage | ||
| { | ||
| [JsonProperty("new_hits")] | ||
| public long NewHits { get; init; } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| // Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. | ||
| // See the LICENCE file in the repository root for full licence text. | ||
|
|
||
| using Newtonsoft.Json; | ||
| using osu.Framework.Extensions.TypeExtensions; | ||
|
|
||
| namespace osu.Desktop.IPC.Messages | ||
| { | ||
| public abstract class OsuWebSocketMessage | ||
| { | ||
| [JsonProperty("type")] | ||
| public string Type { get; } | ||
|
|
||
| protected OsuWebSocketMessage() | ||
| { | ||
| Type = GetType().ReadableName(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| // Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. | ||
| // See the LICENCE file in the repository root for full licence text. | ||
|
|
||
| using System; | ||
| using System.Linq; | ||
| using System.Threading; | ||
| using osu.Desktop.IPC.Messages; | ||
| using osu.Framework.Allocation; | ||
| using osu.Framework.Bindables; | ||
| using osu.Framework.Extensions; | ||
| using osu.Framework.Graphics; | ||
| using osu.Framework.Logging; | ||
| using osu.Game.Configuration; | ||
| using osu.Game.IPC; | ||
| using osu.Game.Online.Multiplayer; | ||
| using osu.Game.Rulesets.Scoring; | ||
| using osu.Game.Scoring; | ||
| using JsonConvert = Newtonsoft.Json.JsonConvert; | ||
|
|
||
| namespace osu.Desktop.IPC | ||
| { | ||
| public partial class OsuWebSocketProvider : Component | ||
| { | ||
| private WebSocketServer? server; | ||
| private readonly Bindable<ScoreInfo> lastLocalScore = new Bindable<ScoreInfo>(); | ||
|
|
||
| [BackgroundDependencyLoader] | ||
| private void load(SessionStatics sessionStatics) | ||
| { | ||
| server = new WebSocketServer(49727); | ||
| server.StartAsync().FireAndForget(onError: ex => Logger.Error(ex, "Failed to start websocket")); | ||
|
|
||
| sessionStatics.BindWith(Static.LastLocalUserScore, lastLocalScore); | ||
| } | ||
|
|
||
| protected override void LoadComplete() | ||
| { | ||
| base.LoadComplete(); | ||
|
|
||
| lastLocalScore.BindValueChanged(val => | ||
| { | ||
| if (val.NewValue == null) | ||
| return; | ||
|
|
||
| if (server?.IsRunning != true) | ||
| return; | ||
|
|
||
| var msg = new HitCountMessage { NewHits = val.NewValue.Statistics.Where(kv => kv.Key.IsBasic() && kv.Key.IsHit()).Sum(kv => kv.Value) }; | ||
| broadcast(msg); | ||
| }); | ||
| } | ||
|
|
||
| private void broadcast(OsuWebSocketMessage message) | ||
| { | ||
| if (server?.IsRunning != true) | ||
| return; | ||
|
|
||
| string messageString = JsonConvert.SerializeObject(message); | ||
| server.BroadcastAsync(messageString).FireAndForget(); | ||
| } | ||
|
|
||
| protected override void Dispose(bool isDisposing) | ||
| { | ||
| base.Dispose(isDisposing); | ||
|
|
||
| if (server?.IsRunning == true) | ||
| { | ||
| var cts = new CancellationTokenSource(); | ||
| cts.CancelAfter(TimeSpan.FromSeconds(10)); | ||
| server.StopAsync(cts.Token).WaitSafely(); | ||
| server = null; | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| // Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. | ||
| // See the LICENCE file in the repository root for full licence text. | ||
|
|
||
| using System; | ||
| using System.Net.WebSockets; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using osu.Game.IPC; | ||
|
|
||
| namespace osu.Game.Tests.IPC | ||
| { | ||
| public sealed class WebSocketClient : IDisposable | ||
| { | ||
| public event Action<string>? MessageReceived; | ||
| public event Action? Closed; | ||
|
|
||
| private readonly int port; | ||
| private WebSocketChannel? channel; | ||
|
|
||
| public WebSocketClient(int port) | ||
| { | ||
| this.port = port; | ||
| } | ||
|
|
||
| public async Task Start(CancellationToken cancellationToken = default) | ||
| { | ||
| var webSocket = new ClientWebSocket(); | ||
| await webSocket.ConnectAsync(new Uri($@"ws://localhost:{port}/"), cancellationToken); | ||
| channel = new WebSocketChannel(webSocket); | ||
| channel.MessageReceived += (msg) => MessageReceived?.Invoke(msg); | ||
Check warningCode scanning / InspectCode Redundant lambda signature parentheses Warning test
Redundant lambda signature parentheses
|
||
| channel.ClosedPrematurely += () => Closed?.Invoke(); | ||
| channel.Start(cancellationToken); | ||
| } | ||
|
|
||
| public async Task SendAsync(string message) | ||
| { | ||
| if (channel == null) | ||
| throw new InvalidOperationException($@"Must {nameof(Start)} first."); | ||
|
|
||
| await channel.SendAsync(message); | ||
| } | ||
|
|
||
| public async Task StopAsync(CancellationToken stoppingToken = default) | ||
| { | ||
| try | ||
| { | ||
| if (channel != null) | ||
| await channel.StopAsync(stoppingToken).ConfigureAwait(false); | ||
| } | ||
| catch (OperationCanceledException) | ||
| { | ||
| // has to be caught manually because outer task isn't accepting `stoppingToken`. | ||
| } | ||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
| channel?.Dispose(); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.