Update: Seems everything is working now?
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json;
|
||||
using RelayClient.Crypto;
|
||||
using RelayShared.Services;
|
||||
using WebSocketSharp;
|
||||
@@ -12,7 +12,6 @@ public sealed class RelaySocketClient
|
||||
|
||||
public string? ServerPublicKey { get; private set; }
|
||||
|
||||
public event Action<string>? RawMessageReceived;
|
||||
public event Action<SocketChannelList>? ChannelListReceived;
|
||||
public event Action<SocketEncryptedMessage>? EncryptedChatReceived;
|
||||
public event Action<SocketRtcSignalMessage>? EncryptedRtcSignalReceived;
|
||||
@@ -32,21 +31,65 @@ public sealed class RelaySocketClient
|
||||
|
||||
var publicKey = KeyStorage.LoadPublicKey(_username);
|
||||
|
||||
SendRaw($"AUTHENTICATE_USER|{_username}|{MainPage._userToken}");
|
||||
SendRaw($"REGISTER_KEY|{_username}|{publicKey}");
|
||||
SendRaw("GET_SERVER_KEY");
|
||||
SendRaw("GET_CHANNELS");
|
||||
SendControlMessage(new WsControlMessage
|
||||
{
|
||||
Action = WsAction.Authenticate,
|
||||
Username = _username,
|
||||
Token = MainPage._userToken
|
||||
});
|
||||
|
||||
SendControlMessage(new WsControlMessage
|
||||
{
|
||||
Action = WsAction.RegisterKey,
|
||||
Username = _username,
|
||||
PublicKey = publicKey
|
||||
});
|
||||
|
||||
SendControlMessage(new WsControlMessage { Action = WsAction.GetServerKey });
|
||||
SendControlMessage(new WsControlMessage { Action = WsAction.GetChannels });
|
||||
}
|
||||
|
||||
public void SendRaw(string message)
|
||||
public void SendGetHistory(string channelId)
|
||||
{
|
||||
if (_socket.ReadyState == WebSocketState.Open)
|
||||
_socket.Send(message);
|
||||
SendControlMessage(new WsControlMessage
|
||||
{
|
||||
Action = WsAction.GetHistory,
|
||||
Username = _username,
|
||||
ChannelId = channelId
|
||||
});
|
||||
}
|
||||
|
||||
public void SendRtcJoinChannel(string channelId)
|
||||
{
|
||||
SendControlMessage(new WsControlMessage
|
||||
{
|
||||
Action = WsAction.RtcJoin,
|
||||
Username = _username,
|
||||
ChannelId = channelId
|
||||
});
|
||||
}
|
||||
|
||||
public void SendRtcLeaveChannel(string channelId)
|
||||
{
|
||||
SendControlMessage(new WsControlMessage
|
||||
{
|
||||
Action = WsAction.RtcLeave,
|
||||
Username = _username,
|
||||
ChannelId = channelId
|
||||
});
|
||||
}
|
||||
|
||||
private void SendControlMessage(WsControlMessage msg)
|
||||
{
|
||||
SendJson(msg);
|
||||
}
|
||||
|
||||
public void SendJson<T>(T payload)
|
||||
{
|
||||
SendRaw(JsonSerializer.Serialize(payload));
|
||||
var json = JsonSerializer.Serialize(payload);
|
||||
|
||||
if (_socket.ReadyState == WebSocketState.Open)
|
||||
_socket.Send(json);
|
||||
}
|
||||
|
||||
public void Disconnect()
|
||||
@@ -59,13 +102,6 @@ public sealed class RelaySocketClient
|
||||
|
||||
private void OnMessage(object? sender, MessageEventArgs e)
|
||||
{
|
||||
if (e.Data.StartsWith("SERVER:REGISTERED_KEY:"))
|
||||
{
|
||||
Log?.Invoke(e.Data);
|
||||
return;
|
||||
}
|
||||
|
||||
RawMessageReceived?.Invoke(e.Data);
|
||||
Log?.Invoke($"[{_username}] RAW WS DATA: {e.Data}");
|
||||
|
||||
try
|
||||
@@ -73,6 +109,27 @@ public sealed class RelaySocketClient
|
||||
using var doc = JsonDocument.Parse(e.Data);
|
||||
var root = doc.RootElement;
|
||||
|
||||
if (root.TryGetProperty("Event", out var eventProp))
|
||||
{
|
||||
var wsEvent = (WsEvent)eventProp.GetInt32();
|
||||
|
||||
switch (wsEvent)
|
||||
{
|
||||
case WsEvent.Authenticated:
|
||||
Log?.Invoke($"[{_username}] Authenticated.");
|
||||
return;
|
||||
case WsEvent.KeyRegistered:
|
||||
Log?.Invoke($"[{_username}] Key registered.");
|
||||
return;
|
||||
case WsEvent.Error:
|
||||
var detail = root.TryGetProperty("Detail", out var d) ? d.GetString() : null;
|
||||
Log?.Invoke($"[{_username}] Server error: {detail}");
|
||||
return;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (!root.TryGetProperty("Type", out var typeElement))
|
||||
return;
|
||||
|
||||
@@ -85,7 +142,6 @@ public sealed class RelaySocketClient
|
||||
var channelList = JsonSerializer.Deserialize<SocketChannelList>(e.Data);
|
||||
if (channelList is not null)
|
||||
ChannelListReceived?.Invoke(channelList);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -97,7 +153,6 @@ public sealed class RelaySocketClient
|
||||
ServerPublicKey = serverKeyMessage.PublicKey;
|
||||
ServerPublicKeyReceived?.Invoke(serverKeyMessage.PublicKey);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -106,7 +161,6 @@ public sealed class RelaySocketClient
|
||||
var payload = JsonSerializer.Deserialize<SocketRtcSignalMessage>(e.Data);
|
||||
if (payload is not null)
|
||||
EncryptedRtcSignalReceived?.Invoke(payload);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -115,7 +169,6 @@ public sealed class RelaySocketClient
|
||||
var payload = JsonSerializer.Deserialize<SocketEncryptedMessage>(e.Data);
|
||||
if (payload is not null)
|
||||
EncryptedChatReceived?.Invoke(payload);
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -125,4 +178,4 @@ public sealed class RelaySocketClient
|
||||
Log?.Invoke($"[{_username}] failed to process websocket message: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ public sealed class RtcBridgeService
|
||||
if (string.IsNullOrWhiteSpace(channelId))
|
||||
return Task.CompletedTask;
|
||||
|
||||
_socket.SendRaw($"RTC_JOIN_CHANNEL|{_username}|{channelId}");
|
||||
_socket.SendRtcJoinChannel(channelId);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ public sealed class RtcBridgeService
|
||||
if (string.IsNullOrWhiteSpace(channelId))
|
||||
return;
|
||||
|
||||
_socket.SendRaw($"RTC_LEAVE_CHANNEL|{_username}|{channelId}");
|
||||
_socket.SendRtcLeaveChannel(channelId);
|
||||
}
|
||||
|
||||
public void SendRtcSignal(string json)
|
||||
|
||||
Reference in New Issue
Block a user