aboutsummaryrefslogtreecommitdiff
path: root/Emby.Server.Implementations/HttpServer/WebSocketConnection.cs
diff options
context:
space:
mode:
authorgion <oancaionutandrei@gmail.com>2020-04-17 13:47:00 +0200
committergion <oancaionutandrei@gmail.com>2020-04-27 22:39:37 +0200
commitaad5058d25b3c295e9ea5b4330dde219034ba8c8 (patch)
tree82e03a7528e1ef877a3b5f1e01baaa1903ccb727 /Emby.Server.Implementations/HttpServer/WebSocketConnection.cs
parent40889702d05c7a6f3dc30090e9443e94cb29fbd9 (diff)
Implement KeepAlive for WebSockets
Diffstat (limited to 'Emby.Server.Implementations/HttpServer/WebSocketConnection.cs')
-rw-r--r--Emby.Server.Implementations/HttpServer/WebSocketConnection.cs27
1 files changed, 21 insertions, 6 deletions
diff --git a/Emby.Server.Implementations/HttpServer/WebSocketConnection.cs b/Emby.Server.Implementations/HttpServer/WebSocketConnection.cs
index 2292d86a4..171047e65 100644
--- a/Emby.Server.Implementations/HttpServer/WebSocketConnection.cs
+++ b/Emby.Server.Implementations/HttpServer/WebSocketConnection.cs
@@ -94,6 +94,9 @@ namespace Emby.Server.Implementations.HttpServer
/// <value>The last activity date.</value>
public DateTime LastActivityDate { get; private set; }
+ /// <inheritdoc />
+ public DateTime LastKeepAliveDate { get; set; }
+
/// <summary>
/// Gets the id.
/// </summary>
@@ -158,11 +161,6 @@ namespace Emby.Server.Implementations.HttpServer
return;
}
- if (OnReceive == null)
- {
- return;
- }
-
try
{
var stub = (WebSocketMessage<object>)_jsonSerializer.DeserializeFromString(message, typeof(WebSocketMessage<object>));
@@ -174,7 +172,15 @@ namespace Emby.Server.Implementations.HttpServer
Connection = this
};
- OnReceive(info);
+ if (info.MessageType.Equals("KeepAlive", StringComparison.Ordinal))
+ {
+ SendKeepAliveResponse();
+ }
+
+ if (OnReceive != null)
+ {
+ OnReceive(info);
+ }
}
catch (Exception ex)
{
@@ -233,6 +239,15 @@ namespace Emby.Server.Implementations.HttpServer
return _socket.SendAsync(text, true, cancellationToken);
}
+ private Task SendKeepAliveResponse()
+ {
+ LastKeepAliveDate = DateTime.UtcNow;
+ return SendAsync(new WebSocketMessage<string>
+ {
+ MessageType = "KeepAlive"
+ }, CancellationToken.None);
+ }
+
/// <inheritdoc />
public void Dispose()
{