コード例 #1
0
        private void ListenForData()
        {
            try
            {
                Byte[] bytes = new Byte[1024];
                while (true)
                {
                    using (NetworkStream stream = socketConnection.GetStream())
                    {
                        int length;
                        while ((length = stream.Read(bytes, 0, bytes.Length)) != 0)
                        {
                            stream.WriteTimeout = 500;
                            var incommingData = new byte[length];
                            Array.Copy(bytes, 0, incommingData, 0, length);
                            string serverMessage = Encoding.ASCII.GetString(incommingData);
                            Console.WriteLine("Kapott üzenet a szervertől: " + serverMessage);

                            SingletonGameState.GetInstance().SetGameState(JsonConvert.DeserializeObject <GameState>(serverMessage));
                            SendMessage(JsonConvert.SerializeObject(SingletonGameState.GetInstance().GetUserCommand()));
                            SingletonGameState.GetInstance().GetUserCommand().deleteCommands();
                            stream.Flush();
                        }
                    }
                }
            }
            catch (SocketException socketException)
            {
                Console.WriteLine("Socket hiba: " + socketException);
            }
        }
コード例 #2
0
ファイル: Game.cs プロジェクト: postam95/FootballGameClient
        private void timer1_Tick(object sender, EventArgs e)
        {
            gameState = SingletonGameState.GetInstance().GetGameState();
            if (gameState.GameActualState == 1)
            {
                label2.Text           = "Várakozás a második játékosra...";
                label2.ForeColor      = Color.White;
                textBoxName.Enabled   = false;
                buttonConnect.Enabled = false;
                clientId             = 1;
                userCommand.ClientId = 1;
            }

            if (gameState.GameActualState == 2)
            {
                panel1.Visible = false;
                this.KeyDown  += new KeyEventHandler(Form1_KeyDown);
                this.KeyUp    += new KeyEventHandler(Form1_KeyUp);
                if (clientId == 0)
                {
                    clientId             = 2;
                    userCommand.ClientId = 2;
                }
            }

            player1Name.Text                = gameState.Player1Name;
            player2Name.Text                = gameState.Player2Name;
            player1Value.Text               = gameState.Player1Value.ToString();
            player2Value.Text               = gameState.Player2value.ToString();
            pictureHomePlayer1.Location     = new Point(gameState.PictureHomePlayer1X, gameState.PictureHomePlayer1Y);
            pcictureHomeGoalKeeper.Location = new Point(gameState.PcictureHomeGoalKeeperX, gameState.PcictureHomeGoalKeeperY);
            pictureAwayPlayer1.Location     = new Point(gameState.PictureAwayPlayer1X, gameState.PictureAwayPlayer1Y);
            pictureAwayGoalKeeper.Location  = new Point(gameState.PictureAwayGoalKeeperX, gameState.PictureAwayGoalKeeperY);
            pictureBall.Location            = new Point(gameState.PictureBallX, gameState.PictureBallY);

            canSend = true;
        }