Esempio n. 1
0
        } // btn_sendMsg_Click

        /// <summary>
        /// 群聊通信
        /// </summary>
        private void sendData()
        {
            User currentUser = User.GetInstance();

            try
            {
                string finalStr = BuildMsg.buildSendMsg("group", userName, currentUser.socket.LocalEndPoint.ToString(), destName, str_Send);
                byte[] msg      = Encoding.UTF8.GetBytes(finalStr);

                //始化需要传入的参数
                SendCallBackParam sendCallBackParam = new SendCallBackParam(
                    str_Send,
                    lstBox_recMsg,
                    appendString);
                // 定制并实例化回调方法
                AsyncCallback callBack = new AsyncCallback(CallBackFunc.sendCallBack);

                // 开始异步发送数据
                currentUser.socket.BeginSend(msg, 0, msg.Length, SocketFlags.None, callBack, sendCallBackParam);
            } // try

            catch (System.Exception ex)
            {
                SocketHandler.ShutDownConn(currentUser.socket);
                lstBox_recMsg.Invoke(appendString, lstBox_recMsg, "服务器断开了TCP连接");
            } // catch
        }     // sendData
Esempio n. 2
0
        } // NoChatSendCb

        /// <summary>
        /// 发送非聊天信息
        /// </summary>
        /// <param name="userName">current user name</param>
        public static void noChatSendData(string type, string userName, string destNamem, Socket soc_Client)
        {
            string str_Send = BuildMsg.buildSendMsg(type, userName, soc_Client.LocalEndPoint.ToString(), destNamem, "");

            byte[] msg = Encoding.UTF8.GetBytes(str_Send);

            // 定制并实例化回调方法
            AsyncCallback callBack = new AsyncCallback(NoChatSendCb);

            // 开始异步发送数据
            soc_Client.BeginSend(msg, 0, msg.Length, SocketFlags.None, callBack, soc_Client);
        } // noChatSendData
Esempio n. 3
0
        }     // acceptCallBack

        /// <summary>
        /// 接受消息
        /// </summary>
        /// <param name="ar"></param>
        public static void recvCallBack(IAsyncResult ar)
        {
            try
            {
                // 初始化关键参数
                RecvCallBackParam          recvCallBackParam = (RecvCallBackParam)ar.AsyncState;
                ListBox                    lstBox_statu      = recvCallBackParam.lstBox_statu;
                MyFriend                   frd          = recvCallBackParam.frd;
                DelegateMem.AppendDelegate appendString = recvCallBackParam.appendString;
                bool    IsStart = recvCallBackParam.IsStart;
                Friends friends = Friends.GetInstance();

                try
                {
                    int i = frd.socket.EndReceive(ar);
                    if (i == 0)
                    {
                        lock (friends)
                        {
                            friends.friendList.Remove(frd);
                        }
                        frd.Dispose();
                        return;
                    } // if

                    else
                    {
                        string strData = Encoding.UTF8.GetString(frd.recvBuffer, 0, i);

                        // 解析收到的数据
                        C2CAnayMsg c2cAnayMsg = new C2CAnayMsg(strData);
                        #region 登录
                        if (c2cAnayMsg.type == "login")
                        {
                            frd.userName = c2cAnayMsg.srcUser;
                        } // if-login
                        #endregion
                        #region 群聊
                        if (c2cAnayMsg.type == "group")
                        {
                            string finalData = string.Format("收到来自:{0},发送给:{1} 的消息: {2}", c2cAnayMsg.srcUser, c2cAnayMsg.destName, c2cAnayMsg.content);
                            lstBox_statu.Invoke(appendString, finalData, lstBox_statu);
                            foreach (MyFriend destFrd in friends.friendList)
                            {
                                if (destFrd.userName != c2cAnayMsg.srcUser)
                                {
                                    string groupMsg = BuildMsg.buildSendMsg("group", c2cAnayMsg.srcUser, c2cAnayMsg.srcIP, c2cAnayMsg.destName, c2cAnayMsg.content);
                                    sendData(destFrd, groupMsg);
                                }
                            }
                        }
                        #endregion
                        #region 点对点聊天
                        if (c2cAnayMsg.type == "client2client")
                        {
                            string finalData = string.Format("收到来自:{0},发送给:{1} 的消息: {2}", c2cAnayMsg.srcUser, c2cAnayMsg.destName, c2cAnayMsg.content);

                            lstBox_statu.Invoke(appendString, finalData, lstBox_statu);

                            // 寻找目的frd
                            int destUserIndex = 0;
                            for (destUserIndex = 0; destUserIndex < friends.friendList.Count; destUserIndex++)
                            {
                                MyFriend destFrd = (MyFriend)friends.friendList[destUserIndex];
                                if (destFrd.userName == c2cAnayMsg.destName)
                                {
                                    break;
                                }
                            } // for

                            if (destUserIndex == friends.friendList.Count)
                            {
                                // TODO 通知发送方,对方不在线
                                int srcUserIndex = 0;
                                for (srcUserIndex = 0; srcUserIndex < friends.friendList.Count; srcUserIndex++)
                                {
                                    MyFriend destFrd = (MyFriend)friends.friendList[srcUserIndex];
                                    if (destFrd.userName == c2cAnayMsg.srcUser)
                                    {
                                        break;
                                    }
                                } // for

                                // 这种情况,客户端收到后,srcUser是自己的名字,而destName不是自己的名字
                                string offlineMsg = BuildMsg.buildSendMsg("offline", c2cAnayMsg.srcUser, c2cAnayMsg.srcIP, c2cAnayMsg.destName, "对方未在线");
                                sendData((MyFriend)friends.friendList[srcUserIndex], offlineMsg);
                            } // if

                            else
                            {
                                // 转发出去 这种情况,转发给另外一个用户,src是发送者的名字,destName是自己的名字
                                string onlineMsg = BuildMsg.buildSendMsg("client2client", c2cAnayMsg.srcUser, c2cAnayMsg.srcIP, c2cAnayMsg.destName, c2cAnayMsg.content);
                                sendData((MyFriend)friends.friendList[destUserIndex], onlineMsg);
                            } // else
                        }     // if-lient2client
                        #endregion
                        #region 退出
                        if (c2cAnayMsg.type == "logout")
                        {
                            //  TDO 已处理
                            string quitMsg = frd.userName + "(" + frd.socket.LocalEndPoint + ")断开了连接";
                            lstBox_statu.Invoke(appendString, quitMsg, lstBox_statu);
                            lock (friends)
                            {
                                friends.friendList.Remove(frd);
                            }
                            frd.Dispose();
                        } // if
                        #endregion
                        #region 添加好友
                        if (c2cAnayMsg.type == "addfrd")
                        {
                            int destUserIndex = 0;
                            for (destUserIndex = 0; destUserIndex < friends.friendList.Count; destUserIndex++)
                            {
                                MyFriend destFrd = (MyFriend)friends.friendList[destUserIndex];
                                if (destFrd.userName == c2cAnayMsg.destName)
                                {
                                    break;
                                } // if
                            }     // for

                            // 被添加的用户肯定存在
                            string onlineMsg = BuildMsg.buildSendMsg("addfrd", c2cAnayMsg.srcUser, c2cAnayMsg.srcIP, c2cAnayMsg.destName, c2cAnayMsg.content);
                            sendData((MyFriend)friends.friendList[destUserIndex], onlineMsg);
                        }
                        #endregion
                        frd.clearBuffer();
                        AsyncCallback callBack = new AsyncCallback(recvCallBack);
                        frd.socket.BeginReceive(frd.recvBuffer, 0, frd.recvBuffer.Length, SocketFlags.None, callBack, recvCallBackParam);
                    } // else
                }     // try

                catch (System.Exception ex)
                {
                    lock (friends)
                    {
                        friends.friendList.Remove(frd);
                    }
                    frd.Dispose();
                } // catch
            }     // try

            catch (System.Exception ex)
            {
            } // cath
        }     // recvCallBack