コード例 #1
0
        public async Task ShouldAddEventToRedisCacheOnLine()
        {
            // Arrange
            string     UDID      = _baseTest.Fixture.Create <string>();
            SelectFunc funcType  = SelectFunc.Online;
            string     objkeyold = JsonConvert.SerializeObject(new DeviceNotificationKey()
            {
                FuncType = SelectFunc.Offline,
                Udid     = UDID,
            });
            await cache.RedisCache.StringSetAsync(objkeyold, UDID);

            // Act
            await _notificationSender.SendAlertOnOffLinePrepareByExpirationTime(funcType, UDID);

            string objkey = JsonConvert.SerializeObject(new DeviceNotificationKey()
            {
                FuncType = SelectFunc.Online,
                Udid     = UDID,
            });

            //Assert
            string result = await cache.RedisCache.StringGetAsync(objkey);

            string resultOld = await cache.RedisCache.StringGetAsync(objkeyold);

            Assert.Null(resultOld);
            Assert.NotNull(result);
        }
コード例 #2
0
        public override async Task OnConnectedAsync()
        {
            try
            {
                var    httpContext = Context.Features.Get <IHttpContextFeature>().HttpContext;
                string udid        = httpContext.Request.Query["udid"];
                string udidPrefix  = KeyPrefix + udid;
                var    key         = KeyPrefix + Context.ConnectionId;

                _logger.LogInformation($"Start OnConnectedAsync {Context.ConnectionId} udid: {udid}", Context.ConnectionId, udid);
                _badDisconnectSocketService.InitConnectionMonitoring(Context);

                await _rediscache.StringSetAsync(key, udid);

                if (await _rediscache.KeyExistsAsync(udidPrefix))
                {
                    string oldConnectionId = await _rediscache.StringGetSetAsync(udidPrefix, Context.ConnectionId);

                    _badDisconnectSocketService.DisconnectClient(oldConnectionId);
                }
                else
                {
                    await _rediscache.StringSetAsync(udidPrefix, Context.ConnectionId);
                }

                var deviceFromDb = await _deviceService.GetDeviceByUdidAsync(udid);

                if (deviceFromDb == null)
                {
                    await _deviceService.Setup(new SetupDeviceRequest { Udid = udid });

                    deviceFromDb = await _deviceService.GetDeviceByUdidAsync(udid);

                    var lastHistoryByDevice = (await _deviceHistoryRepository.GetAsync(x => x.DeviceId == deviceFromDb.Id))
                                              .OrderByDescending(y => y.CreatedOn)
                                              .FirstOrDefault();

                    await AddAndSendDeviceHistoryToAll(CreateDeviceHistoryOnConnect(deviceFromDb, lastHistoryByDevice));
                }
                else
                {
                    var lastHistoryByDevice = (await _deviceHistoryRepository.GetAsync(x => x.DeviceId == deviceFromDb.Id))
                                              .OrderByDescending(y => y.CreatedOn)
                                              .FirstOrDefault();

                    await AddAndSendDeviceHistory(udid, CreateDeviceHistoryOnConnect(deviceFromDb, lastHistoryByDevice));

                    await _notificationSenderExtention.SendAlertOnOffLinePrepareByExpirationTime(SelectFunc.Online, udid);
                }

                await base.OnConnectedAsync();

                _memoryCache.Set(Context.ConnectionId, string.Empty, GetCacheEntryOptions());
                _logger.LogInformation($"Finish OnConnectedAsync: {Context.ConnectionId}, udid: {udid}", Context.ConnectionId, udid);
            }
            catch (RedisConnectionException exm)
            {
                RedisConnection.ForceReconnect();
                _logger.LogError($"Error RedisConnectionException ContextID: {Context.ConnectionId} EXC: {exm.Message} {exm.StackTrace} {exm.Source}");
            }
            catch (ObjectDisposedException ex)
            {
                RedisConnection.ForceReconnect();
                _logger.LogCritical("Cannot force reconnect to redis cache!!", ex);
            }
        }