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 Broadcast(MessageGame messageGameToClients) { var messageToClientsJson = ""; foreach (var clientHandler in clients) { NetworkStream nTemp = clientHandler.tcpclient.GetStream(); BinaryWriter wTemp = new BinaryWriter(nTemp); messageToClientsJson = messageGameToClients.ToJson(); wTemp.Write(messageToClientsJson); wTemp.Flush(); } Console.WriteLine("GAMEoutput:" + messageToClientsJson); }
public void Send(MessageGame messageGame) { string message = ""; messageGame.Version = 1; message = messageGame.ToJson(); try { NetworkStream n = client.GetStream(); BinaryWriter w = new BinaryWriter(n); w.Write(message); w.Flush(); //client.Close(); } catch (Exception ex) { Console.WriteLine(ex.Message); } }