public void Run() { var messageGame = new MessageGame(); try { while (true) { NetworkStream n = tcpclient.GetStream(); var message = new BinaryReader(n).ReadString(); messageGame = messageGame.FromJson(message); Console.WriteLine("INPUT: " + message); myServer.Broadcast(this, message); } } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { myServer.DisconnectClient(this); tcpclient.Close(); } }
public void Broadcast(ClientHandler client, string messageJson) { MessageGame messageGame = new MessageGame(); messageGame = messageGame.FromJson(messageJson); NetworkStream n = client.tcpclient.GetStream(); BinaryWriter w = new BinaryWriter(n); switch (messageGame.MessageType) { case "PlayerMove": game.Movehandler(messageGame); break; case "Auth": messageGame.MessageType = "AuthResponse"; if (client == clients[0]) { messageGame.PlayerName = "Player1"; messageJson = messageGame.ToJson(); w.Write(messageJson); w.Flush(); } else if (client == clients[1]) { messageGame.PlayerName = "Player2"; messageJson = messageGame.ToJson(); w.Write(messageJson); w.Flush(); game = new GameServer(this); game.StartGame(); } Console.WriteLine("AUTHoutput:" + messageJson); break; case "ResetRequest": //game.GameBoard = new int[7, 6]; //MessageGame messageReset = new MessageGame //{ // BoardState = game.GameBoard //}; //Broadcast(messageReset); game.InitiateBoard(); break; default: throw new ArgumentException(); } }
public void Listen() { MessageGame messageGame = new MessageGame { Version = 1 }; string message = ""; try { while (true) { NetworkStream n = client.GetStream(); message = new BinaryReader(n).ReadString(); messageGame = messageGame.FromJson(message); switch (messageGame.MessageType) { case "AuthResponse": _game.AuthResponse(messageGame); break; //case "GameReset": // _mainWindow.Dispatcher.Invoke(_mainWindow.ResetGame); //break; case "StartGame": _game.StartGame(messageGame); break; case "GameBoardUpdate": _game.UpdateGameBoard(messageGame); break; case "GameOver": _game.GameOver(messageGame); break; default: break; } } } catch (Exception ex) { Console.WriteLine(ex.Message); } }