コード例 #1
0
        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();
                }
            }
        }
コード例 #2
0
        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();
                }
            }
        }
コード例 #3
0
 public byte[] EncodeFrame(PingFrame frame)
 {
     if (!string.IsNullOrEmpty(frame.Data))
     {
         var data = Encoding.UTF8.GetBytes(frame.Data);
         if (data.Length > 125)
             throw new WebSocketException("All control frames must have a payload length of 125 bytes or less.");
         return Encode(frame.OpCode, data, 0, data.Length, isMasked: frame.IsMasked);
     }
     else
     {
         return Encode(frame.OpCode, EmptyArray, 0, 0, isMasked: frame.IsMasked);
     }
 }
コード例 #4
0
 public byte[] EncodeFrame(PingFrame frame)
 {
     if (!string.IsNullOrEmpty(frame.Data))
     {
         var data = Encoding.UTF8.GetBytes(frame.Data);
         if (data.Length > 125)
         {
             throw new WebSocketException("All control frames must have a payload length of 125 bytes or less.");
         }
         return(Encode(frame.OpCode, data, 0, data.Length, isMasked: frame.IsMasked));
     }
     else
     {
         return(Encode(frame.OpCode, EmptyArray, 0, 0, isMasked: frame.IsMasked));
     }
 }
コード例 #5
0
        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();
                }
            }
        }
コード例 #6
0
ファイル: WebSocketClient.cs プロジェクト: cube3power/Cowboy
        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();
                }
            }
        }