private void OnKeepAlive() { if (_keepAliveLocker.WaitOne(0)) { try { if (State != WebSocketState.Open) { return; } if (_keepAliveTracker.ShouldSendKeepAlive()) { var keepAliveFrame = new PingFrame().ToArray(_frameBuilder); SendFrame(keepAliveFrame); StartKeepAliveTimeoutTimer(); _keepAliveTracker.ResetTimer(); } } catch (Exception ex) { _log(ex.Message); Close(WebSocketCloseCode.EndpointUnavailable); } finally { _keepAliveLocker.Release(); } } }
private async void OnKeepAlive() { if (await _keepAliveLocker.WaitAsync(0)) { try { if (State != WebSocketState.Open) { return; } if (_keepAliveTracker.ShouldSendKeepAlive()) { var keepAliveFrame = new PingFrame(false).ToArray(_frameBuilder); await SendFrame(keepAliveFrame); StartKeepAliveTimeoutTimer(); #if DEBUG _log.DebugFormat("Session [{0}] sends server side ping frame [{1}].", this, string.Empty); #endif _keepAliveTracker.ResetTimer(); } } catch (Exception ex) { _log.Error(string.Format("Session [{0}] exception occurred, [{1}].", this, ex.Message), ex); await Close(WebSocketCloseCode.EndpointUnavailable); } finally { _keepAliveLocker.Release(); } } }