Пример #1
0
        public override async Task OnConnected()
        {
            log.Info($"MessageHub-OnConnected {Context.ConnectionId}");
            try
            {
                IDBHelper db   = new MssqlHelper(StaticObject.ConnectionStrings["Kf"]);
                var       data = await db.FindOneAsync <CustomerServiceOnline>($"SELECT TOP 1 * FROM CustomerServiceOnlines WHERE ConnectId='{Context.ConnectionId}'", null, false);

                if (data != null)
                {
                    await db.ExcuteNonQueryAsync($"Insert INTO CustomerInOutLogs (CustomerId,InOutState,CreationTime) VALUES ({data.Id},{(int)InOutState.Login},GetDate())", null, false);

                    await db.ExcuteNonQueryAsync($"Update CustomerServiceOnlines SET ConnectState={(int)ConnectState.Connect} WHERE ConnectId='{Context.ConnectionId}'", null, false);

                    data.ConnectState = (int)ConnectState.Connect;
                    //memberCache.Set(data.OpenID, data, Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(StaticObject.CacheDictionary[StaticObject.Cache_Kf_OpenId2Customer]));
                    //redisCache.RemoveValue(data.OpenID, StaticObject.Cache_OpenId2Customer);

                    await StackExchangeRedisHelper.Set(data.OpenID, StaticObject.Cache_Kf_OpenId2Customer, JsonConvert.SerializeObject(data), StaticObject.CacheDictionary[StaticObject.Cache_Kf_OpenId2Customer]);

                    await StackExchangeRedisHelper.Remove(data.OpenID, StaticObject.Cache_OpenId2Customer);
                }
                await base.OnConnected();
            }
            catch (Exception ex)
            {
                log.Error("MessageHub-OnConnected错误:", ex);
                throw ex;
            }
        }
Пример #2
0
        public async Task setState(string messageToken, int state)
        {
            try
            {
                var customer = await GetCustomer(messageToken);

                if (customer != null)
                {
                    IDBHelper db = new MssqlHelper(StaticObject.ConnectionStrings["Kf"]);
                    await db.ExcuteNonQueryAsync($"Update CustomerServiceOnlines SET OnlineState={state},ConnectId='{Context.ConnectionId}',ConnectState={(int)ConnectState.Connect} WHERE ID='{customer.Id}'", null, false);

                    customer.OnlineState  = state;
                    customer.ConnectId    = Context.ConnectionId;
                    customer.ConnectState = 1;

                    //memberCache.Set(messageToken, customer.OpenID, Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(StaticObject.CacheDictionary[StaticObject.Cache_Kf_MessageToken2CustomerOpenId]));
                    //memberCache.Set(customer.OpenID, customer, Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(StaticObject.CacheDictionary[StaticObject.Cache_Kf_OpenId2Customer]));
                    //redisCache.RemoveValue(customer.OpenID, StaticObject.Cache_OpenId2Customer);



                    await StackExchangeRedisHelper.Set(messageToken, StaticObject.Cache_Kf_MessageToken2CustomerOpenId, customer.OpenID, StaticObject.CacheDictionary[StaticObject.Cache_Kf_MessageToken2CustomerOpenId]);

                    await StackExchangeRedisHelper.Set(customer.OpenID, StaticObject.Cache_Kf_OpenId2Customer, JsonConvert.SerializeObject(customer), StaticObject.CacheDictionary[StaticObject.Cache_Kf_OpenId2Customer]);

                    await StackExchangeRedisHelper.Remove(customer.OpenID, StaticObject.Cache_OpenId2Customer);

                    var a = -1;
                    if (state == (int)OnlineState.OnLine)
                    {
                        a = (int)InOutState.online;
                    }
                    if (state == (int)OnlineState.Leave)
                    {
                        a = (int)InOutState.leave;
                    }
                    if (state == (int)OnlineState.Quit)
                    {
                        a = (int)InOutState.quit;
                    }
                    await db.ExcuteNonQueryAsync($"Insert INTO CustomerInOutLogs (CustomerId,InOutState,CreationTime) VALUES ({customer.Id},{a},GetDate())", null, false);
                }
            }
            catch (Exception ex)
            {
                log.Error("MessageHub-setState错误:", ex);
                throw;
            }
        }