Example #1
0
        } // GroupTalk_Load

        /// <summary>
        /// 从MsgList中找到有效的数据并且填入窗口
        /// </summary>
        /// <param name="cancellationToken">a symbol of whether canceling task</param>
        private void pollMsg(CancellationToken cancellationToken)
        {
            while (!cancellationToken.IsCancellationRequested)
            {
                MsgList msgList = MsgList.GetInstance();

                // 已提取到的消息的下标
                ArrayList indexList = new ArrayList();
                int       i         = 0;

                if (msgList.recvMsgList.Count != 0)
                {
                    foreach (RecvdMsg msg in msgList.recvMsgList)
                    {
                        if (msg.type == "group" && msg.destName == "all")
                        {
                            string finalStr = msg.userName + "发来:" + msg.content;
                            lstBox_recMsg.Invoke(appendString, lstBox_recMsg, finalStr);
                            indexList.Add(i); // 下标索引
                        } // if

                        i++;
                    } // foreach
                }     // if

                // 删掉已经读取的消息
                foreach (int index in indexList)
                {
                    RecvdMsg msg = msgList.recvMsgList[index];
                    msgList.recvMsgList.Remove(msg);
                } // foreach
            }     // while
        }         // pollMsg
Example #2
0
        /// <summary>
        /// 回调函数:接受消息
        /// </summary>
        /// <param name="ar">the ar</param>
        public static void recCallBack(IAsyncResult ar)
        {
            try
            {
                // 接受消息的缓冲槽
                byte[] recvBuffer = (byte[])ar.AsyncState;

                // 当前用户的实例
                User currentUser = User.GetInstance();

                // 接受到的每一条消息
                RecvdMsg recvMsg = new RecvdMsg();

                // 消息列表实例
                MsgList msgList = MsgList.GetInstance();

                try
                {
                    int    i        = currentUser.socket.EndReceive(ar);
                    string str_data = Encoding.UTF8.GetString(recvBuffer, 0, i);

                    // 解析数据
                    C2CAnayMsg c2cAnayMsg = new C2CAnayMsg(str_data);

                    // 初始化收到信息类
                    recvMsg.type     = c2cAnayMsg.type;
                    recvMsg.userName = c2cAnayMsg.srcUser;
                    recvMsg.srcIP    = c2cAnayMsg.srcIP;
                    recvMsg.destName = c2cAnayMsg.destName;
                    recvMsg.content  = c2cAnayMsg.content;

                    if (recvMsg.type != null)
                    {
                        msgList.recvMsgList.Add(recvMsg);
                    } // if

                    recvBuffer = new byte[currentUser.socket.SendBufferSize];

                    //循环接收
                    AsyncCallback callBack = new AsyncCallback(recCallBack);

                    //异步调用
                    currentUser.socket.BeginReceive(recvBuffer, 0, recvBuffer.Length, SocketFlags.None, callBack, recvBuffer);//开始从连接的Socket中接收数据
                } // try

                catch (System.Exception ex)
                {
                    if (currentUser.socket != null)
                    {
                        SocketHandler.ShutDownConn(currentUser.socket);
                    } // if
                }     /// catch
            }         // try

            catch (System.Exception ex)
            {
                // 封装客户端停止连接时的异常
            } // catch
        }     // recCallBack
Example #3
0
        } // W_Main_Load

        /// <summary>
        /// 遍历消息列表,每20秒查询一下是否有新好友加入
        /// </summary>
        /// <param name="cancellationToken">the sing of cancel task</param>
        private void findAddMsg(CancellationToken cancellationToken)
        {
            while (!cancellationToken.IsCancellationRequested)
            {
                // 发现未读消息
                Thread.Sleep(1000);
                MsgList   msgList   = MsgList.GetInstance();
                ArrayList indexList = new ArrayList();
                int       i         = 0;
                unReadMsg = "";
                lbl_unRdMsg.Invoke(reset, "", lbl_unRdMsg);
                if (msgList.recvMsgList.Count != 0)
                {
                    foreach (RecvdMsg msg in msgList.recvMsgList)
                    {
                        if (msg.type == "addfrd")
                        {
                            pnl_userList.Invoke(iniFrdList, pnl_userList);
                            indexList.Add(i);
                        } // if

                        // 查看是否有未读消息
                        if (msg.type == "client2client")
                        {
                            unReadMsg += msg.userName + ",";
                        }
                        i++;
                    } // foreach
                }     // if

                unReadMsg.TrimEnd(',');
                if (unReadMsg != "")
                {
                    unReadMsg += "的消息未读";
                    lbl_unRdMsg.Invoke(reset, unReadMsg, lbl_unRdMsg);
                } // if

                // 删掉已经读取的消息
                foreach (int index in indexList)
                {
                    RecvdMsg msg = msgList.recvMsgList[index];
                    msgList.recvMsgList.Remove(msg);
                }
            } // while
        }     // findAddMsg
Example #4
0
        } // W_SingleChat_Load

       /// <summary>
        /// 从MsgList中找到有效的数据并且填入窗口
       /// </summary>
       /// <param name="cancellationToken">the symbol whether cancel task</param>
        private void pollMsg(CancellationToken cancellationToken)
        {
            while (!cancellationToken.IsCancellationRequested)
            {
                MsgList msgList = MsgList.GetInstance();
                ArrayList indexList = new ArrayList();
                int i = 0;

                // 未读消息列表不为空时
                if (msgList.recvMsgList.Count != 0)
                {
                    foreach (RecvdMsg msg in msgList.recvMsgList)
                    {
                        if (msg.type == "offline" && msg.userName == userName && msg.destName == destName)
                        {
                            lstBox_recMsg.Invoke(appendString, lstBox_recMsg, msg.content);
                            indexList.Add(i);
                        } // if

                        if (msg.type == "client2client" && msg.userName == destName)
                        {
                            string finalStr = msg.userName + "发来:" + msg.content;
                            lstBox_recMsg.Invoke(appendString, lstBox_recMsg, finalStr);
                            indexList.Add(i);
                        } // if

                        i++;
                    } // foreach
                } // if

                // 删掉已经读取的消息
                foreach (int index in indexList)
                {
                    RecvdMsg msg = msgList.recvMsgList[index];
                    msgList.recvMsgList.Remove(msg);
                } // foreach
            } // while
        } // pollMsg