Exemplo n.º 1
0
        private void PushTask()
        {
            Task.Factory.StartNew(async() =>
            {
                while (!_cts.Token.IsCancellationRequested)
                {
                    if (_sendCount >= _option.Total)
                    {
                        await Task.Delay(2000);
                        continue;
                    }

                    if (!_isClientConnected)
                    {
                        await Task.Delay(2000);
                        continue;
                    }


                    try
                    {
                        var message = new TimeRequestMessage()
                        {
                            CreateTime = DateTime.Now,
                            Content    = Encoding.UTF8.GetBytes($"{DateTime.Now:yyyy-MM-dd HH:mm:ss fff}")
                        };
                        var responsePush = await _server.PushAsync(new RequestPush()
                        {
                            Code = 101,
                            Body = _binarySerializer.Serialize(message)
                        }, _socketSession.SessionId);

                        _performanceService.IncrementKeyCount(_performanceKey, (DateTime.Now - message.CreateTime).TotalMilliseconds);
                        Interlocked.Increment(ref _sendCount);
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError(ex, "Send message caught exception,{0}", ex.Message);
                    }
                }
            }, TaskCreationOptions.LongRunning);
        }