File tree Expand file tree Collapse file tree 3 files changed +20
-8
lines changed
Lib.AspNetCore.ServerSentEvents Expand file tree Collapse file tree 3 files changed +20
-8
lines changed Original file line number Diff line number Diff line change 11namespace 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}
Original file line number Diff line number Diff 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 ( ) ;
Original file line number Diff line number Diff 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 {
You can’t perform that action at this time.
0 commit comments