public void mainThread() { if (mainThreadMessageQueue.Count > 0) { string nextMessage = mainThreadMessageQueue.Dequeue(); Message messageObject = JsonUtility.FromJson <Message>(nextMessage); switch (messageObject.message) { case (int)Message.messageTypes.ConnectionSuccessful: ConnectMessage connectMessage = JsonUtility.FromJson <ConnectMessage>(nextMessage); if (connectMessage.isInProgress) { sceneManager.spawnLocalPlayer(); } else { sceneManager.LobbyMenu(); } break; case (int)Message.messageTypes.PositionUpdate: PositionUpdateMessage positionUpdateMessage = JsonUtility.FromJson <PositionUpdateMessage>(nextMessage); foreach (RemoteClients remote in remoteClients) { if (remote.clientID == positionUpdateMessage.clientID) { remote.clientTransform = positionUpdateMessage.position; remote.clientRotation = positionUpdateMessage.rotation; } } break; case (int)Message.messageTypes.RaycastMessage: sceneManager.takeDamage(nextMessage); break; case (int)Message.messageTypes.InstantiateObject: InstantiateObject instantiateObject = JsonUtility.FromJson <InstantiateObject>(nextMessage); if (instantiateObject.prefabName == NetworkInstantiate.prefabNames.ragdoll) { sceneManager.instantiateRagdoll(instantiateObject); } else { networkInst.recieveInstance(nextMessage); } break; } } }
// sends the updated position of a player to other clients private void updatePosition(string message) { PositionUpdateMessage positionUpdate = JsonUtility.FromJson <PositionUpdateMessage>(message); foreach (RemoteClients remote in remoteClients) { if (remote.clientID != positionUpdate.clientID) { sendToClient(remote.clientEndPoint, positionUpdate.constructMessage()); } } }
void sendPositionUpdateToServer() { if (chatClient != null) { try { if (this.ownPlayer.transform.position != this.lastPosition) { this.lastPosition = this.ownPlayer.transform.position; PositionUpdateMessage message = new PositionUpdateMessage(); message.type = "positionUpdate"; message.name = this.ownName; message.position = this.ownPlayer.transform.position.ToString().Replace("(", "").Replace(")", ""); string json = UnityEngine.JsonUtility.ToJson(message); sendStringToServer(json); } } catch (System.Exception e) { b.AppendLine(e.ToString()); this.chatText.text = b.ToString(); this.TimeLeftUntilFade = fadeOutTime; } } }
// Updates the location of position on the server and communicates it to all connected clients public void sendPositionUpdate(Vector3 position, Quaternion rotation) { PositionUpdateMessage positionUpdateMessage = new PositionUpdateMessage(localClientID, position, rotation); sendToServer(positionUpdateMessage.constructMessage()); }
void handleServerResponse(string response) { string[] singleJSONStrings = response.Split(new char[] { '\n' }, System.StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < singleJSONStrings.Length; i++) { if (System.Text.RegularExpressions.Regex.IsMatch(singleJSONStrings[i], "\"type\"\\s*:\\s*\"end\"")) { b.AppendLine("Server terminated connection"); chatClient.Close(); chatClient = null; continue; } if (System.Text.RegularExpressions.Regex.IsMatch(singleJSONStrings[i], "\"type\"\\s*:\\s*\"welcome\"")) { b.AppendLine("Received welcome from server!\nFollowing coordinates have been assigned to you:"); WelcomeMessage message = UnityEngine.JsonUtility.FromJson <WelcomeMessage>(singleJSONStrings[i]); b.AppendLine(message.position); this.teleportAvatar(message.position); continue; } if (System.Text.RegularExpressions.Regex.IsMatch(singleJSONStrings[i], "\"type\"\\s*:\\s*\"newcomer\"")) { b.Append("New player connected! Name: "); ServerNewcomerMessage message = UnityEngine.JsonUtility.FromJson <ServerNewcomerMessage>(singleJSONStrings[i]); b.Append(message.name); b.Append(" Position: "); b.Append(message.position); b.Append("\n"); try { string[] coords = message.position.Split(','); float x = float.Parse(coords[0], System.Globalization.CultureInfo.InvariantCulture); float y = float.Parse(coords[1], System.Globalization.CultureInfo.InvariantCulture); float z = float.Parse(coords[2], System.Globalization.CultureInfo.InvariantCulture); otherPlayerList.Add(new OtherPlayer(message.name, x, y, z, otherPlayerPrefab)); } catch (System.Exception e) { b.AppendLine(e.ToString()); } continue; } if (System.Text.RegularExpressions.Regex.IsMatch(singleJSONStrings[i], "\"type\"\\s*:\\s*\"disconnect\"")) { ServerDisconnectMessage message = UnityEngine.JsonUtility.FromJson <ServerDisconnectMessage>(singleJSONStrings[i]); b.Append("Player "); b.Append(message.name); b.Append(" disconnected!\n"); OtherPlayer disconnected = otherPlayerList.Find(x => x.getName().Equals(message.name)); otherPlayerList.Remove(disconnected); disconnected.destroy(); continue; } if (System.Text.RegularExpressions.Regex.IsMatch(singleJSONStrings[i], "\"type\"\\s*:\\s*\"blockAction\"")) { try { BlockActionMessage message = UnityEngine.JsonUtility.FromJson <BlockActionMessage>(singleJSONStrings[i]); string[] positionRaw = message.position.Split(','); float x = float.Parse(positionRaw[0], System.Globalization.CultureInfo.InvariantCulture); float y = float.Parse(positionRaw[1], System.Globalization.CultureInfo.InvariantCulture); float z = float.Parse(positionRaw[2], System.Globalization.CultureInfo.InvariantCulture); Vector3 position = new Vector3(x, y, z); GameObject result = this.blockPlacementInstance.blockPlacementList.Find(block => block.transform.position == position); if (result == null) { int blockID = int.Parse(message.blockID); GameObject newBlock = GameObject.Instantiate(this.blockPlacementInstance.blockSelectionList[blockID], position, Quaternion.identity); this.blockPlacementInstance.blockPlacementList.Add(newBlock); } else { this.blockPlacementInstance.blockPlacementList.Remove(result); GameObject.Destroy(result); } } catch (System.Exception e) { b.AppendLine(e.ToString()); } continue; } if (System.Text.RegularExpressions.Regex.IsMatch(singleJSONStrings[i], "\"type\"\\s*:\\s*\"positionUpdate\"")) { try { PositionUpdateMessage message = UnityEngine.JsonUtility.FromJson <PositionUpdateMessage>(singleJSONStrings[i]); string[] positionRaw = message.position.Split(','); float x = float.Parse(positionRaw[0], System.Globalization.CultureInfo.InvariantCulture); float y = float.Parse(positionRaw[1], System.Globalization.CultureInfo.InvariantCulture); float z = float.Parse(positionRaw[2], System.Globalization.CultureInfo.InvariantCulture); Vector3 position = new Vector3(x, y, z); OtherPlayer moved = otherPlayerList.Find(player => player.getName().Equals(message.name)); moved.move(position); } catch (System.Exception e) { } } if (System.Text.RegularExpressions.Regex.IsMatch(singleJSONStrings[i], "\"type\"\\s*:\\s*\"chat\"")) { ServerMessage message = UnityEngine.JsonUtility.FromJson <ServerMessage>(singleJSONStrings[i]); b.Append(message.sender); b.Append(message.world ? " (world): " : " (you): "); b.Append(message.content); b.Append('\n'); continue; } if (System.Text.RegularExpressions.Regex.IsMatch(singleJSONStrings[i], "\"type\"\\s*:\\s*\"ping\"")) { int latency = System.Environment.TickCount - this.pingTimer; b.Append("Ping: "); b.Append(latency); b.Append(" ms\n"); } if (System.Text.RegularExpressions.Regex.IsMatch(singleJSONStrings[i], "\"type\"\\s*:\\s*\"state\"")) { foreach (OtherPlayer player in otherPlayerList) { player.destroy(); } otherPlayerList.Clear(); ServerStateMessage message = UnityEngine.JsonUtility.FromJson <ServerStateMessage>(singleJSONStrings[i]); string parsedPositions = message.positions; parsedPositions = parsedPositions.Replace("{", ""); parsedPositions = parsedPositions.Replace("}", ""); parsedPositions = parsedPositions.Replace("\\", ""); parsedPositions = parsedPositions.Replace("\"", ""); parsedPositions = parsedPositions.Replace(":", ","); string[] positionData = parsedPositions.Split(new char[] { ',' }); b.AppendLine("Generating list of players and their global positions:"); for (int j = 0; j < positionData.Length; j += 4) { b.AppendLine("Name: " + positionData[j] + ", X:" + positionData[j + 1] + ", Y:" + positionData[j + 2] + ", Z:" + positionData[j + 3]); if (positionData[j].Equals(ownName)) { continue; } float x = float.Parse(positionData[j + 1], System.Globalization.CultureInfo.InvariantCulture); float y = float.Parse(positionData[j + 2], System.Globalization.CultureInfo.InvariantCulture); float z = float.Parse(positionData[j + 3], System.Globalization.CultureInfo.InvariantCulture); otherPlayerList.Add(new OtherPlayer(positionData[j], x, y, z, otherPlayerPrefab)); } continue; } } if (!this.chatText.text.Equals(b.ToString())) { this.chatText.text = b.ToString(); this.TimeLeftUntilFade = fadeOutTime; } }
public void SendPosition(Vector3 position) { var message = new PositionUpdateMessage(position); _networkManager.SendDataMessage(PositionUpdateMessage.OpCode, message); }