2022-10-28 16:53:28 +08:00
|
|
|
// 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.Diagnostics;
|
|
|
|
using System.Net.WebSockets;
|
|
|
|
using System.Text;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
using osu.Framework.Extensions.TypeExtensions;
|
|
|
|
using osu.Framework.Logging;
|
|
|
|
using osu.Game.Online.API;
|
|
|
|
using osu.Game.Online.Chat;
|
|
|
|
|
|
|
|
namespace osu.Game.Online.Notifications.WebSocket
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// A notifications client which receives events via a websocket.
|
|
|
|
/// </summary>
|
|
|
|
public class WebSocketNotificationsClient : NotificationsClient
|
|
|
|
{
|
|
|
|
private readonly ClientWebSocket socket;
|
|
|
|
private readonly string endpoint;
|
|
|
|
|
|
|
|
public WebSocketNotificationsClient(ClientWebSocket socket, string endpoint, IAPIProvider api)
|
|
|
|
: base(api)
|
|
|
|
{
|
|
|
|
this.socket = socket;
|
|
|
|
this.endpoint = endpoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override async Task ConnectAsync(CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
await socket.ConnectAsync(new Uri(endpoint), cancellationToken).ConfigureAwait(false);
|
2022-11-04 17:48:34 +08:00
|
|
|
await sendMessage(new StartChatRequest(), CancellationToken.None);
|
|
|
|
|
2022-10-28 16:53:28 +08:00
|
|
|
runReadLoop(cancellationToken);
|
|
|
|
|
2022-11-04 17:48:34 +08:00
|
|
|
await base.ConnectAsync(cancellationToken);
|
2022-10-28 16:53:28 +08:00
|
|
|
}
|
|
|
|
|
2022-10-28 17:54:34 +08:00
|
|
|
private void runReadLoop(CancellationToken cancellationToken) => Task.Run(async () =>
|
2022-10-28 16:53:28 +08:00
|
|
|
{
|
|
|
|
byte[] buffer = new byte[1024];
|
|
|
|
StringBuilder messageResult = new StringBuilder();
|
|
|
|
|
|
|
|
while (!cancellationToken.IsCancellationRequested)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
WebSocketReceiveResult result = await socket.ReceiveAsync(buffer, cancellationToken);
|
|
|
|
|
|
|
|
switch (result.MessageType)
|
|
|
|
{
|
|
|
|
case WebSocketMessageType.Text:
|
|
|
|
messageResult.Append(Encoding.UTF8.GetString(buffer[..result.Count]));
|
|
|
|
|
|
|
|
if (result.EndOfMessage)
|
|
|
|
{
|
|
|
|
SocketMessage? message = JsonConvert.DeserializeObject<SocketMessage>(messageResult.ToString());
|
|
|
|
messageResult.Clear();
|
|
|
|
|
|
|
|
Debug.Assert(message != null);
|
|
|
|
|
|
|
|
if (message.Error != null)
|
|
|
|
{
|
|
|
|
Logger.Log($"{GetType().ReadableName()} error: {message.Error}", LoggingTarget.Network);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-11-07 11:11:44 +08:00
|
|
|
Logger.Log($"{GetType().ReadableName()} handling event: {message.Event}");
|
2022-10-28 16:53:28 +08:00
|
|
|
await onMessageReceivedAsync(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WebSocketMessageType.Binary:
|
2022-11-07 11:11:44 +08:00
|
|
|
throw new NotImplementedException("Binary message type not supported.");
|
2022-10-28 16:53:28 +08:00
|
|
|
|
|
|
|
case WebSocketMessageType.Close:
|
|
|
|
throw new Exception("Connection closed by remote host.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
await InvokeClosed(ex);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2022-10-28 17:54:34 +08:00
|
|
|
}, cancellationToken);
|
2022-10-28 16:53:28 +08:00
|
|
|
|
|
|
|
private async Task closeAsync()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
await socket.CloseAsync(WebSocketCloseStatus.NormalClosure, @"Disconnecting", CancellationToken.None).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
// Closure can fail if the connection is aborted. Don't really care since it's disposed anyway.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task sendMessage(SocketMessage message, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
if (socket.State != WebSocketState.Open)
|
|
|
|
return;
|
|
|
|
|
|
|
|
await socket.SendAsync(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(message)), WebSocketMessageType.Text, true, cancellationToken);
|
|
|
|
}
|
|
|
|
|
|
|
|
private Task onMessageReceivedAsync(SocketMessage message)
|
|
|
|
{
|
|
|
|
switch (message.Event)
|
|
|
|
{
|
2022-11-04 19:11:42 +08:00
|
|
|
case @"chat.channel.join":
|
|
|
|
Debug.Assert(message.Data != null);
|
|
|
|
|
|
|
|
Channel? joinedChannel = JsonConvert.DeserializeObject<Channel>(message.Data.ToString());
|
|
|
|
Debug.Assert(joinedChannel != null);
|
|
|
|
|
2022-11-07 10:52:07 +08:00
|
|
|
joinedChannel.Joined.Value = true;
|
2022-11-04 19:11:42 +08:00
|
|
|
HandleJoinedChannel(joinedChannel);
|
|
|
|
break;
|
|
|
|
|
2022-11-07 10:36:55 +08:00
|
|
|
case @"chat.channel.part":
|
|
|
|
Debug.Assert(message.Data != null);
|
|
|
|
|
|
|
|
Channel? partedChannel = JsonConvert.DeserializeObject<Channel>(message.Data.ToString());
|
|
|
|
Debug.Assert(partedChannel != null);
|
|
|
|
|
|
|
|
HandleChannelParted(partedChannel);
|
|
|
|
break;
|
|
|
|
|
2022-10-28 16:53:28 +08:00
|
|
|
case @"chat.message.new":
|
|
|
|
Debug.Assert(message.Data != null);
|
|
|
|
|
|
|
|
NewChatMessageData? messageData = JsonConvert.DeserializeObject<NewChatMessageData>(message.Data.ToString());
|
|
|
|
Debug.Assert(messageData != null);
|
|
|
|
|
2022-11-04 15:42:59 +08:00
|
|
|
foreach (var msg in messageData.Messages)
|
2022-11-07 10:52:07 +08:00
|
|
|
HandleJoinedChannel(new Channel { Id = msg.ChannelId });
|
2022-10-28 16:53:28 +08:00
|
|
|
|
2022-11-04 15:42:59 +08:00
|
|
|
HandleMessages(messageData.Messages);
|
2022-10-28 16:53:28 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override async ValueTask DisposeAsync()
|
|
|
|
{
|
|
|
|
await base.DisposeAsync();
|
|
|
|
await closeAsync();
|
|
|
|
socket.Dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|