Esempio n. 1
0
        /// <summary>用户客户端上线
        /// </summary>
        protected override void OnOpen()
        {
            //进入锁之前将数据准备好
            string userlogin = Context.QueryString["user"];
            string password  = Context.QueryString["pwd"];

            DBUtil.IDbAccess iDb = Util.iDb_read;

            //准备用户信息
            DataTable dt_user = iDb.GetDataTable(string.Format("select * from CHAT_USER where LOGINNAME={0} and PWD = {1}", iDb.paraPrefix + "uname", iDb.paraPrefix + "upwd"), new IDbDataParameter[] { iDb.CreatePara("uname", userlogin), iDb.CreatePara("upwd", password) });

            if (dt_user.Rows.Count == 0)
            {
                try
                {
                    Context.WebSocket.Send(string.Format("登录失败:用户或密码错误"));
                    try
                    {
                        Close();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        Console.WriteLine(ex.StackTrace);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Console.WriteLine(ex.StackTrace);
                }
                return;
            }
            //构造客户端对象
            Client client = new Client();

            client.ID        = ID;
            client.StartTime = DateTime.Now;
            client.LOGINNAME = userlogin;
            client.UNAME     = dt_user.Rows[0]["UNAME"].ToString();
            client.USERID    = int.Parse(dt_user.Rows[0]["ID0"].ToString());

            //添加连接对象
            WebSockets.TryAdd(ID, Context.WebSocket);

            //准备群组用户群组对象
            DataTable dt_user_group = iDb.GetDataTable(string.Format("select CHAT_GROUP.* from CHAT_USER_GROUP left join CHAT_GROUP on CHAT_USER_GROUP.GID=CHAT_GROUP.ID0 where USERID='{0}'", client.USERID));

            OnLineData.Edit(online =>
            {
                //用户对象
                User user = null;
                online.Link_Clients.Add(ID, client);

                if (!online.Uid_Users.TryGetValue(client.USERID, out user))
                {
                    //当前用户的第一个客户端进行登录
                    user           = new User();
                    user.Name      = dt_user.Rows[0]["UNAME"].ToString();
                    user.ID0       = int.Parse(dt_user.Rows[0]["ID0"].ToString());
                    user.LoginName = dt_user.Rows[0]["LOGINNAME"].ToString();
                    user.Clients.Add(client.ID, client);
                    online.Uid_Users.Add(user.ID0, user);
                    online.Link_Users.Add(ID, user);

                    //构造群组对象
                    if (dt_user_group.Rows.Count > 0)
                    {
                        int count = dt_user_group.Rows.Count;
                        for (int i = 0; i < count; i++)
                        {
                            int gid = int.Parse(dt_user_group.Rows[i]["ID0"].ToString());
                            Group g;
                            if (!online.Gid_Groups.TryGetValue(gid, out g))
                            {
                                //初次建立群组对象
                                g      = new Group();
                                g.Name = dt_user_group.Rows[i]["GNAME"].ToString();
                                g.ID0  = gid;
                                g.Users.Add(user.ID0, user);
                                online.Gid_Groups.Add(g.ID0, g);
                            }
                            else
                            {
                                //已经存在这个群组对象
                                if (!g.Users.ContainsKey(user.ID0))
                                {
                                    //不包含这个用户
                                    g.Users.Add(user.ID0, user);
                                }
                            }
                        }
                    }
                }
                else
                {
                    //当前用户的多个用户端登录
                    user.Clients.Add(client.ID, client);
                    online.Link_Users.Add(ID, user);
                }
                return(true);
            });
            //广播用户上线通知
            SendBroadCast("/BroadUserOnLine", Newtonsoft.Json.JsonConvert.SerializeObject(new
            {
                type      = "BroadUserOnLine",
                loginname = client.LOGINNAME
            }));
        }