Ejemplo n.º 1
0
 /// <summary>
 /// 更新用户登出后界面信息
 /// </summary>
 /// <param name="clientInfo"></param>
 /// <param name="info"></param>
 private void RemoveLogOutUser(ChatClientInfo clientInfo, MessageInfo info)
 {
     if (info == null || clientInfo == null)
     {
         return;
     }
     clientInfo.UesrID = info.MessageFrom.USER_ID;
     tViewUser.BeginInvoke(new Action(() =>
     {
         for (int i = 0; i < tViewUser.Nodes.Count; i++)
         {
             TreeNode item          = tViewUser.Nodes[i];
             EMRDBLib.UserInfo user = item.Tag as EMRDBLib.UserInfo;
             if (user.USER_ID == clientInfo.UesrID)
             {
                 tViewUser.Nodes.Remove(item); break;
             }
         }
     }));
     gBoxUser.BeginInvoke(new Action(() =>
     {
         gBoxUser.Text = string.Format("在线用户:{0}人", lstClientInfo.Count);
     }));
     rtbServerLog.BeginInvoke(new Action(() =>
     {
         rtbServerLog.Text += string.Format("用户{0}从{1}成功退出服务器. {2}{3}",
                                            string.Format("{0}-{1}({2})", info.MessageFrom.DEPT_NAME, info.MessageFrom.USER_NAME, info.MessageFrom.USER_ID),
                                            clientInfo.SocketClient.RemoteEndPoint.ToString(),
                                            DateTime.Now.ToString("yyyy MM-dd HH:mm:ss"), Environment.NewLine);
     }));
 }
Ejemplo n.º 2
0
 /// <summary>
 /// 处理用户登出
 /// </summary>
 /// <param name="clientInfo"></param>
 /// <param name="info"></param>
 private void HandleLogOutMessage(ChatClientInfo clientInfo, MessageInfo info)
 {
     RemoveLogOutUser(clientInfo, info);
     //更新完新增连接用户后通知其他在线用户
     SendMessageUpdateOnLineUserInfo(info);
     lstClientInfo.Remove(clientInfo);
 }
Ejemplo n.º 3
0
 /// <summary>
 /// 更新用户登录后界面信息
 /// </summary>
 /// <param name="clientInfo"></param>
 /// <param name="info"></param>
 private void AddLogInUser(ChatClientInfo clientInfo, MessageInfo info)
 {
     if (info == null || clientInfo == null)
     {
         return;
     }
     clientInfo.UesrID = info.MessageFrom.USER_ID;
     tViewUser.BeginInvoke(new Action(() =>
     {
         TreeNode node = new TreeNode();
         node.Text     = string.Format("{0}-{1}({2})", info.MessageFrom.DEPT_NAME, info.MessageFrom.USER_NAME, info.MessageFrom.USER_ID);
         node.Tag      = info.MessageFrom;
         tViewUser.Nodes.Add(node);
     }));
     gBoxUser.BeginInvoke(new Action(() =>
     {
         gBoxUser.Text = string.Format("在线用户:{0}人", lstClientInfo.Count);
     }));
     rtbServerLog.BeginInvoke(new Action(() =>
     {
         rtbServerLog.Text += string.Format("用户{0}从{1}成功连接服务器. {2}{3}",
                                            string.Format("{0}-{1}({2})", info.MessageFrom.DEPT_NAME, info.MessageFrom.USER_NAME, info.MessageFrom.USER_ID),
                                            clientInfo.SocketClient.RemoteEndPoint.ToString(),
                                            DateTime.Now.ToString("yyyy MM-dd HH:mm:ss"), Environment.NewLine);
     }));
 }
Ejemplo n.º 4
0
 /// <summary>
 /// 处理用户登录
 /// </summary>
 /// <param name="clientInfo"></param>
 /// <param name="info"></param>
 private void HandleLogInMessage(ChatClientInfo clientInfo, MessageInfo info)
 {
     if (!string.IsNullOrEmpty(clientInfo.UesrID))
     {
         return;
     }
     if (!lstClientInfo.Exists(i => i.UesrID == info.MessageFrom.USER_ID))//不存在则登陆
     {
         AddLogInUser(clientInfo, info);
         //更新完新增连接用户后通知其他在线用户
         SendMessageUpdateOnLineUserInfo(info);
     }
     else //已存在该用户连接,则删除后在登陆
     {
         //退出
         ChatClientInfo existClientInfo = lstClientInfo.Find(i => i.UesrID == info.MessageFrom.USER_ID);
         info.MessageAction = (int)ActionType.LogOut;
         HandleLogOutMessage(existClientInfo, info);
         //重新登录
         info.MessageAction = (int)ActionType.LogIn;
         AddLogInUser(clientInfo, info);
         //更新完新增连接用户后通知其他在线用户
         SendMessageUpdateOnLineUserInfo(info);
     }
 }
Ejemplo n.º 5
0
 //回发数据给客户端
 private void RecieveCallBack(IAsyncResult AR)
 {
     try
     {
         Socket RSocket = (Socket)AR.AsyncState;
         int    REnd    = RSocket.EndReceive(AR);
         if (REnd <= 0)//
         {
             return;
         }
         ChatClientInfo clientInfo = lstClientInfo.Find(i => i.SocketClient.Handle == RSocket.Handle);
         if (clientInfo == null)
         {
             return;
         }
         //同时接收客户端回发的数据,用于回发
         RSocket.BeginReceive(clientInfo.MsgBuffer, 0, clientInfo.MsgBuffer.Length, 0, new AsyncCallback(RecieveCallBack), RSocket);
         MessageInfo info = DeserializeObject(clientInfo.MsgBuffer) as MessageInfo;
         HandleMessage(clientInfo, info);
     }
     catch (Exception ex)
     {
         LogManager.Instance.WriteLog("RecieveCallBack失败", ex);
     }
 }
Ejemplo n.º 6
0
        /// <summary>
        /// 接受客户端连接的方法
        /// </summary>
        private void RecieveAccept()
        {
            try
            {
                while (true)
                {
                    //Accept 以同步方式从侦听套接字的连接请求队列中提取第一个挂起的连接请求,然后创建并返回新的 Socket。
                    //在阻止模式中,Accept 将一直处于阻止状态,直到传入的连接尝试排入队列。连接被接受后,原来的 Socket 继续将传入的连接请求排入队列,直到您关闭它。
                    ChatClientInfo clientInfo = new ChatClientInfo();
                    clientInfo.SocketClient = ServerSocket.Accept();
                    lstClientInfo.Add(clientInfo);//添加至客户端列表

                    clientInfo.SocketClient.BeginReceive(clientInfo.MsgBuffer, 0, clientInfo.MsgBuffer.Length, SocketFlags.None,
                                                         new AsyncCallback(RecieveCallBack), clientInfo.SocketClient);
                    clientInfo.SocketClient.Send(GetOnLineUserInfos());
                }
            }
            catch (SocketException ex)
            {
                LogManager.Instance.WriteLog("RecieveAccept失败", ex);
            }
            catch (Exception ex)
            {
                LogManager.Instance.WriteLog("RecieveAccept失败", ex);
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// 发送聊天消息给客户端
        /// </summary>
        /// <param name="info"></param>
        private void HandleSendMessage(MessageInfo info)
        {
            if (info == null)
            {
                return;
            }

            ChatClientInfo clientInfo = lstClientInfo.Find(i => i.UesrID == info.MessageTo.USER_ID);//获取接收端socket信息

            if (clientInfo == null || !clientInfo.SocketClient.Connected)
            {
                return;
            }
            clientInfo.SocketClient.Send(SerilizeContent(info));
        }
Ejemplo n.º 8
0
        private void ClientInfo_Login(ChatClientInfo client, ChatClient.MessageInfo message)
        {
            if (client == null || message == null)
            {
                return;
            }
            //更新在线信息
            if (message.MessageAction != (int)MedQC.ChatClient.ActionType.LogIn)
            {
                return;
            }
            client.UesrID = message.MessageFrom;
            TreeNode node = new TreeNode();

            node.Text = client.UesrID;
            tViewUser.Nodes.Add(node);

            gBoxUser.Text = string.Format("在线用户:{0}人", tViewUser.Nodes.Count);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// 处理信息
        /// </summary>
        /// <param name="info"></param>
        private void HandleMessage(ChatClientInfo clientInfo, MessageInfo info)
        {
            if (info == null || clientInfo == null)
            {
                return;
            }
            switch (info.MessageAction)
            {
            case (int)ActionType.LogIn:
                HandleLogInMessage(clientInfo, info); break;

            case (int)ActionType.LogOut:
                HandleLogOutMessage(clientInfo, info); break;

            case (int)ActionType.SendMessage:
            case (int)ActionType.SendPIc:
                HandleSendMessage(info); break;

            default: break;
            }
        }
Ejemplo n.º 10
0
 /// <summary>
 /// 接受客户端连接的方法
 /// </summary>
 private void RecieveAccept()
 {
     try
     {
         while (true)
         {
             //Accept 以同步方式从侦听套接字的连接请求队列中提取第一个挂起的连接请求,然后创建并返回新的 Socket。
             //在阻止模式中,Accept 将一直处于阻止状态,直到传入的连接尝试排入队列。连接被接受后,原来的 Socket 继续将传入的连接请求排入队列,直到您关闭它。
             ChatClientInfo clientInfo = new ChatClientInfo();
             clientInfo.SocketClient = ServerSocket.Accept();
             lstClientInfo.Add(clientInfo);
             clientInfo.SocketClient.BeginReceive(clientInfo.MsgBuffer, 0, clientInfo.MsgBuffer.Length, SocketFlags.None,
                                                  new AsyncCallback(RecieveCallBack), clientInfo.SocketClient);
             //Object o = DeserializeObject(clientInfo.MsgBuffer);
             //MedQC.ChatClient.MessageInfo message = (ChatClient.MessageInfo)o;
             //tViewUser.BeginInvoke(new EventHandler(delegate { ClientInfo_Login(clientInfo, message); }));
             AddLog(clientInfo.SocketClient.RemoteEndPoint.ToString() + " 成功连接服务器.");
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
 }