Skip to content

Commit e969fb0

Browse files
committed
Support for keepalives. Resolves #12
1 parent 0e27fb9 commit e969fb0

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
namespace Lib.AspNetCore.ServerSentEvents
22
{
33
/// <summary>
4-
/// The keepalive event sending mode.
4+
/// The keepalive sending mode.
55
/// </summary>
66
public enum ServerSentEventsKeepaliveMode
77
{
88
/// <summary>
9-
/// Always send keepalive event.
9+
/// Always send keepalive.
1010
/// </summary>
1111
Always,
1212
/// <summary>
13-
/// Never send keepalive event.
13+
/// Never send keepalive.
1414
/// </summary>
15-
Never
15+
Never,
16+
/// <summary>
17+
/// Send keepalive if ANCM is detected.
18+
/// </summary>
19+
BehindAncm
1620
}
1721
}

Lib.AspNetCore.ServerSentEvents/ServerSentEventsKeepaliveService.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ internal class ServerSentEventsKeepaliveService<TServerSentEventsService> : IHos
1111
where TServerSentEventsService : ServerSentEventsService
1212
{
1313
#region Fields
14+
private readonly bool _isBehindAncm = IsBehindAncm();
1415
private readonly static ServerSentEventBytes _keepaliveServerSentEventBytes = ServerSentEventsHelper.GetCommentBytes("KEEPALIVE");
1516

1617
private readonly CancellationTokenSource _stoppingCts = new CancellationTokenSource();
@@ -32,7 +33,7 @@ public ServerSentEventsKeepaliveService(TServerSentEventsService serverSentEvent
3233
#region Methods
3334
public virtual Task StartAsync(CancellationToken cancellationToken)
3435
{
35-
if (_options.KeepaliveMode == ServerSentEventsKeepaliveMode.Always)
36+
if ((_options.KeepaliveMode == ServerSentEventsKeepaliveMode.Always) || ((_options.KeepaliveMode == ServerSentEventsKeepaliveMode.BehindAncm) && _isBehindAncm))
3637
{
3738
_executingTask = ExecuteAsync(_stoppingCts.Token);
3839

@@ -73,6 +74,13 @@ private async Task ExecuteAsync(CancellationToken stoppingToken)
7374
}
7475
}
7576

77+
private static bool IsBehindAncm()
78+
{
79+
return !String.IsNullOrEmpty(Environment.GetEnvironmentVariable($"ASPNETCORE_PORT"))
80+
&& !String.IsNullOrEmpty(Environment.GetEnvironmentVariable($"ASPNETCORE_APPL_PATH"))
81+
&& !String.IsNullOrEmpty(Environment.GetEnvironmentVariable($"ASPNETCORE_TOKEN"));
82+
}
83+
7684
public virtual void Dispose()
7785
{
7886
_stoppingCts.Cancel();

Lib.AspNetCore.ServerSentEvents/ServerSentEventsServiceOptions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ public class ServerSentEventsServiceOptions<TServerSentEventsService> where TSer
1313
private int _keepaliveInterval = DEFAULT_KEEPALIVE_INTERVAL;
1414

1515
/// <summary>
16-
/// Gets or sets the keepalive event sending mode.
16+
/// Gets or sets the keepalive sending mode.
1717
/// </summary>
18-
public ServerSentEventsKeepaliveMode KeepaliveMode { get; set; } = ServerSentEventsKeepaliveMode.Never;
18+
public ServerSentEventsKeepaliveMode KeepaliveMode { get; set; } = ServerSentEventsKeepaliveMode.BehindAncm;
1919

2020
/// <summary>
21-
/// Gets or sets the keepalive event interval (in seconds).
21+
/// Gets or sets the keepalive interval (in seconds).
2222
/// </summary>
2323
public int KeepaliveInterval
2424
{

0 commit comments

Comments
 (0)