private void HandleGetStateMessage(GetStateMessage getStateMessage) { // generate State Message of all playerpositions and blockpositions and send it back! Dictionary <string, string> playersPositions = new Dictionary <string, string>(); playersPositions.Add(myClientName, Message.GetStringFromVector3(playersPosition)); foreach (KeyValuePair <string, MutliplayerObject> entry in players) { string name; Vector3 position; (position, name) = entry.Value.GetPlayer(); playersPositions.Add(name, Message.GetStringFromVector3(position)); } Dictionary <string, int> blockPositions = new Dictionary <string, int>(); foreach (KeyValuePair <Vector3, int> entry in blockList) { blockPositions.Add(Message.GetStringFromVector3(entry.Key), entry.Value); } StateMessage messageToSend = new StateMessage(playersPositions, blockPositions); Debug.Log("P2PConnection: Sending my State of the World with " + blockPositions.Count + " blocks and " + playersPositions.Count + " players"); sockets[getStateMessage.senderName].CreateAndSendMessage(messageToSend); }
private void HandleGetStateMessage(string message) { GetStateMessage getStateMessage = JsonUtility.FromJson <GetStateMessage>(message); peerToPeerManager.AddIncommingMessage(getStateMessage); }
// Send message to server using socket connection. public new bool SendMessage(string message) { string[] tokens = message.Split(' '); if (!tokens[0].StartsWith("/")) { return(false); } switch (tokens[0]) { case "/connect": if (tokens.Length < 3) { return(false); } try { int result = Int32.Parse(tokens[2]); ConnectToTcpServer(tokens[1], result); return(true); } catch (FormatException) { Console.WriteLine($"Unable to parse '{tokens[2]}'"); } return(false); case "/all": if (tokens.Length < 2) { return(false); } ChatMessageOut worldMsg; worldMsg = new ChatMessageOut("world", string.Join(" ", tokens.Skip(1))); if (SendData(JsonUtility.ToJson(worldMsg))) { chat.addMessage(string.Format("[Ich an alle]: {0}", worldMsg.content)); } break; case "/hello": if (tokens.Length < 2) { return(false); } HelloMsg helloMsg = new HelloMsg(tokens[1]); if (SendData(JsonUtility.ToJson(helloMsg))) { chat.addMessage("Hallo Server, ich heiße " + tokens[1]); gameController.playerName = tokens[1]; } break; case "/to": ChatMessageOut chatMsg; if (tokens.Length > 2) { chatMsg = new ChatMessageOut(tokens[1], string.Join(" ", tokens.Skip(2))); } else { chatMsg = new ChatMessageOut(tokens[1], ""); } if (SendData(JsonUtility.ToJson(chatMsg))) { chat.addMessage(string.Format("[Ich an {0}]: {1}", chatMsg.receiver, chatMsg.content)); } break; case "/clear": chat.clearMessages(); break; case "/getstate": GetStateMessage getStateMessage = new GetStateMessage(); SendData(JsonUtility.ToJson(getStateMessage)); break; } return(false); }