Esempio n. 1
0
        /// <summary>
        /// 应客户端要求建立连接后的处理线程
        /// </summary>
        /// <param name="ia"></param>
        private void AcceptCallBack(IAsyncResult ar)
        {
            try
            {
                //定义一个新的客户端对象
                Session state = (Session)ar.AsyncState;
                Socket client = state._workSocket;

                #region === 消息处理 ===

                //登录成功,则进入消息等待中
                int RevNumber = 0;

                if (client.Connected)
                {
                    #region === 正常或非正常的断线处理 ===

                    try { RevNumber = client.EndReceive(ar); }
                    catch {
                        if (ChatEvent != null)
                        {
                            ChatEvent(this, new ChatEventArgs() { CmdId = "OUT", From = this._id, To = 0, Content = this._name });
                        }
                        return;
                    }

                    if (RevNumber == 0)
                    {
                        if (ChatEvent != null)
                        {
                            ChatEvent(this, new ChatEventArgs() { CmdId = "OUT", From = this._id, To = 0, Content = this._name });
                        }
                        return;
                    }

                    #endregion

                    DataFrame dr = new DataFrame(RecvDataBuffer);
                    string content = dr.Text;

                    string[] msgs = content.Split(',');
                    if (msgs[0] == "LIN")
                    {
                        this._name = content.Substring(8);
                        //通知其他客户端有人上线了
                        if (ChatEvent != null)
                        {
                            ChatEventArgs args = new ChatEventArgs();
                            args.CmdId = "LIN";
                            args.From = this._id;
                            args.To = 0;
                            args.Content = this._name;
                            ChatEvent(this, new ChatEventArgs() { CmdId = "LIN", From = this._id, To = 0, Content = this._name });
                        }
                    }
                    else if (msgs[0] == "MSG")
                    {
                        //聊天
                        if (ChatEvent != null)
                        {
                            ChatEvent(this, new ChatEventArgs() { CmdId = "MSG", From = Convert.ToInt32(msgs[1]), To = Convert.ToInt32(msgs[2]), Content = content.Substring(msgs[0].Length + msgs[1].Length + msgs[2].Length + 3) });
                        }
                    }
                    else
                    {
                        //下线
                        if (ChatEvent != null)
                        {
                            ChatEvent(this, new ChatEventArgs() { CmdId = "OUT", From = this._id, To = 0, Content = this._name });
                        }
                    }

                    //继续接收来自来客户端的数据
                    client.BeginReceive(RecvDataBuffer, 0, RecvDataBuffer.Length, SocketFlags.None,
                        new AsyncCallback(AcceptCallBack), state);
                }
                #endregion

            }
            catch { }
        }
        void state_ChatEvent(object sender, ChatEventArgs e)
        {
            if (e.CmdId == "LIN")
            {
                Session ss = (Session)sender;
                DataFrame dr = new DataFrame(OnlineClients);
                ss.Send("OLN,0," + e.From + "," + dr.Text);
                SendToAll(e.From, e.ToString());
            }
            else if (e.CmdId == "MSG")
            {

                if (e.To == 0)
                {
                    SendToAll(e.From, e.ToString());
                }
                else
                {
                    SendToOne(e.To, e.ToString());
                }

            }
            else
            {
                m_clients.Remove(e.From);
                SendToAll(e.From, e.ToString());
            }
        }