void HeartBeat() { updateMessage uMsg = new updateMessage(); uMsg.cmd = "heartbeat"; JsonUtility.ToJson(uMsg); Byte[] sendBytes = Encoding.ASCII.GetBytes(JsonUtility.ToJson(uMsg)); udp.Send(sendBytes, sendBytes.Length); }
void Start() { udp = new UdpClient(); // 3.131.90.180 udp.Connect("18.217.121.107", 12345); updateMessage uMsg = new updateMessage(); uMsg.cmd = "connect"; JsonUtility.ToJson(uMsg); Byte[] sendBytes = Encoding.ASCII.GetBytes(JsonUtility.ToJson(uMsg)); udp.Send(sendBytes, sendBytes.Length); udp.BeginReceive(new AsyncCallback(OnReceived), udp); InvokeRepeating("HeartBeat", 1, 1); InvokeRepeating("UpdatePosition", 1, 1 / 30); }
void UpdatePosition() { if (myClientID == null) { return; } if (existingPlayers.ContainsKey(myClientID)) { updateMessage uMsg = new updateMessage(); uMsg.cmd = "updateposition"; uMsg.Y = existingPlayers[myClientID].transform.position.y; uMsg.X = existingPlayers[myClientID].transform.position.x; uMsg.Z = existingPlayers[myClientID].transform.position.z; JsonUtility.ToJson(uMsg); Byte[] sendBytes = Encoding.ASCII.GetBytes(JsonUtility.ToJson(uMsg)); udp.Send(sendBytes, sendBytes.Length); existingPlayers[myClientID].GetComponent <NetworkID>().setBool(true); } else { existingPlayers[myClientID].GetComponent <NetworkID>().setBool(false); } }