示例#1
0
文件: Client.cs 项目: MrCloudFox/Chat
        public static void ReceiveMessage()
        {
            var client = new Client();

            while (true)
            {
                try
                {
                    byte[]        data    = new byte[64];
                    StringBuilder builder = new StringBuilder();
                    int           bytes   = 0;
                    do
                    {
                        bytes = Stream.Read(data, 0, data.Length);
                        builder.Append(Encoding.Unicode.GetString(data, 0, bytes));
                    }while (Stream.DataAvailable);

                    string nickName = (builder.ToString()).Split(' ')[0];
                    string message  = builder.ToString().Replace(nickName, "");

                    if (message == " ~Connect")
                    {
                        client.OnParticipantConnected(new ParticipantEventArgs(nickName.Split(':')[0]));
                    }
                    else if (message == " ~Disconnect")
                    {
                        client.OnParticipantDisonnected(new ParticipantEventArgs(nickName.Split(':')[0]));
                        Disconnect();
                    }
                    else
                    {
                        client.OnMessageReceive(new MessageEventArgs(nickName, message));
                    }
                }
                catch
                {
                    //MessageBox.Show("Connect is lost!");
                    Disconnect();
                }
            }
        }