コード例 #1
0
        public async void ReciveMessage()
        {
            try
            {
                while (true)
                {
                    byte[] buff = new byte[2024];
                    await stream.ReadAsync(buff, 0, buff.Length);

                    using (var s = new MemoryStream(buff))
                    {
                        BinaryFormatter formatter = new BinaryFormatter();
                        Message.Message tmp       = (Message.Message)formatter.Deserialize(s);
                        if (tmp.MessageType == MessageType.UserInfo)
                        {
                            info = tmp.UserData;
                        }
                        else if (tmp.MessageType == MessageType.SearchInfo)
                        {
                            if (tmp.SearchInfo == Search.Start)
                            {
                                server.AddInQueueAsync(this);
                            }
                            else
                            {
                                server.RemoveFromQueueAsync(this);
                            }
                        }
                        if (liveGame != null)
                        {
                            liveGame.GotMessage(this, tmp, buff);
                        }
                        if (tmp.MessageType == MessageType.Quit)
                        {
                            server.RemoveClientAsync(this);
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                client.Close();
            }
        }