Exemple #1
0
        public override async Task OnConnectedAsync()
        {
            var ci = Context.GetClient();

            var skip = false;

            if (!Context.GetHttpRequest().IsAccess(_options))
            {
                _logger.LogWarning(
                    $"{ci} access denied");
                skip = true;
            }

            await base.OnConnectedAsync();

            if (!skip && (string.IsNullOrWhiteSpace(ci.Name) || string.IsNullOrWhiteSpace(ci.Ip)))
            {
                _logger.LogWarning(
                    $"{ci} is not valid");
                skip = true;
            }

            if (!skip)
            {
                try
                {
                    var client = await _store.GetClient(ci.Name, ci.Group);

                    if (client == null)
                    {
                        ci.IsConnected = true;
                        await _store.AddClient(ci);

                        _logger.LogInformation(
                            $"{ci} register success");
                        return;
                    }

                    if (client.IsConnected)
                    {
                        _logger.LogInformation(
                            $"{ci} is connected");
                    }
                    else
                    {
                        await _store.ConnectClient(ci.Name, ci.Group, ci.ConnectionId);

                        _logger.LogInformation(
                            $"{ci} register success");
                        return;
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex,
                                     $"{ci} register failed: {ex.Message}");
                }
            }

            Context.Abort();
        }