✨ The websocket handle the ping / pong
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
using DysonNetwork.Shared.Proto;
|
using DysonNetwork.Shared.Proto;
|
||||||
using NodaTime;
|
using NodaTime;
|
||||||
using NodaTime.Serialization.SystemTextJson;
|
using NodaTime.Serialization.SystemTextJson;
|
||||||
@@ -8,8 +9,14 @@ namespace DysonNetwork.Pusher.Connection;
|
|||||||
public class WebSocketPacket
|
public class WebSocketPacket
|
||||||
{
|
{
|
||||||
public string Type { get; set; } = null!;
|
public string Type { get; set; } = null!;
|
||||||
|
|
||||||
|
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
||||||
public object? Data { get; set; } = null!;
|
public object? Data { get; set; } = null!;
|
||||||
|
|
||||||
|
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
||||||
public string? Endpoint { get; set; }
|
public string? Endpoint { get; set; }
|
||||||
|
|
||||||
|
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
||||||
public string? ErrorMessage { get; set; }
|
public string? ErrorMessage { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -74,7 +81,7 @@ public class WebSocketPacket
|
|||||||
ErrorMessage = ErrorMessage
|
ErrorMessage = ErrorMessage
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public static WebSocketPacket FromProtoValue(Shared.Proto.WebSocketPacket packet)
|
public static WebSocketPacket FromProtoValue(Shared.Proto.WebSocketPacket packet)
|
||||||
{
|
{
|
||||||
return new WebSocketPacket
|
return new WebSocketPacket
|
||||||
@@ -84,4 +91,4 @@ public class WebSocketPacket
|
|||||||
ErrorMessage = packet.ErrorMessage
|
ErrorMessage = packet.ErrorMessage
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -109,6 +109,20 @@ public class WebSocketService
|
|||||||
WebSocket socket
|
WebSocket socket
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
|
if (packet.Type == WebSocketPacketType.Ping)
|
||||||
|
{
|
||||||
|
await socket.SendAsync(
|
||||||
|
new ArraySegment<byte>(new WebSocketPacket
|
||||||
|
{
|
||||||
|
Type = WebSocketPacketType.Pong
|
||||||
|
}.ToBytes()),
|
||||||
|
WebSocketMessageType.Binary,
|
||||||
|
true,
|
||||||
|
CancellationToken.None
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (_handlerMap.TryGetValue(packet.Type, out var handler))
|
if (_handlerMap.TryGetValue(packet.Type, out var handler))
|
||||||
{
|
{
|
||||||
await handler.HandleAsync(currentUser, deviceId, packet, socket, this);
|
await handler.HandleAsync(currentUser, deviceId, packet, socket, this);
|
||||||
@@ -176,4 +190,4 @@ public class WebSocketService
|
|||||||
CancellationToken.None
|
CancellationToken.None
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -2,6 +2,8 @@ namespace DysonNetwork.Shared.Data;
|
|||||||
|
|
||||||
public abstract class WebSocketPacketType
|
public abstract class WebSocketPacketType
|
||||||
{
|
{
|
||||||
|
public const string Ping = "ping";
|
||||||
|
public const string Pong = "pong";
|
||||||
public const string Error = "error";
|
public const string Error = "error";
|
||||||
public const string MessageNew = "messages.new";
|
public const string MessageNew = "messages.new";
|
||||||
public const string MessageUpdate = "messages.update";
|
public const string MessageUpdate = "messages.update";
|
||||||
|
Reference in New Issue
Block a user