Exemple #1
0
        //public override System.Threading.Tasks.Task OnConnected()
        //{
        //    string clientName = Context.QueryString["clientName"].ToString();
        //    string clientId = Context.QueryString["clientId"].ToString();
        //    OnLineUsers.AddOrUpdate(clientId, clientName, (key, value) => clientName);
        //    Clients.All.userChange(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), string.Format("{0} 加入了。", clientName), OnLineUsers.ToArray());
        //    return base.OnConnected();
        //}

        //public override System.Threading.Tasks.Task OnDisconnected(bool stopCalled)
        //{
        //    string clientName = Context.QueryString["clientName"].ToString();
        //    string clientId = Context.QueryString["clientId"].ToString();
        //    Clients.All.userChange(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), string.Format("{0} 离开了。", clientName), OnLineUsers.ToArray());
        //    OnLineUsers.TryRemove(clientId, out clientName);
        //    return base.OnDisconnected(stopCalled);
        //}

        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public override Task OnConnectedAsync()
        {
            var    orgNo      = "";
            var    bgNo       = "";
            var    result     = 0;
            var    user       = new SysUsrMstr();
            string clientId   = Context.GetHttpContext().Request.Query["clientId"].ToString();
            string clientName = Context.GetHttpContext().Request.Query["clientName"];

            if (string.IsNullOrEmpty(clientName))
            {
                user       = _chatHelper.GetUserInfo(clientId);
                clientName = user.USR_REAL_NAME;
            }

            _chatHelper.GetStoreNo(clientId, ref orgNo, ref bgNo);
            var log = new Log("Chat/Connect/" + bgNo + "");

            log.Write("connect:" + Context.ConnectionId + "");
            try
            {
                UserList = UserList.Where(c => c.BgNo == bgNo).ToList();
                var onlineUser = UserList.Where(p => p.ConnectionId == Context.ConnectionId && p.BgNo == bgNo).FirstOrDefault();
                log.Write("onlineUser:"******"");
                if (user != null)
                {
                    UserList.Remove(onlineUser);
                }
                // RemoveMessage(clientId);
                var newUser = new OnlineUserInfo();
                if (int.TryParse(clientId, out result))
                {
                    newUser.UserType = int.Parse(user.USR_TYPE);
                }
                newUser.UserId       = clientId;
                newUser.ConnectionId = Context.ConnectionId;
                newUser.UserNickName = clientName;
                newUser.Url          = _chatHelper.GetWxUrl(clientId);
                newUser.BgNo         = bgNo;
                UserList.Add(newUser);
                Clients.Clients(GetUserConnectionIds()).SendAsync("userChange", clientId, true);
            }
            catch (Exception ex)
            {
                log.Write(ex.Message);
            }

            return(base.OnConnectedAsync());
        }