// Start is called before the first frame update void Start() { string msgJson; FlagToServer flag = new FlagToServer(); Byte[] sendBytes; if (bDebug) { Debug.Log("[Notice] Setting up client..."); } udp = new UdpClient(); playerReferences = new Dictionary <string, GameObject>(); dataQueue = new Queue <string>(); if (bDebug) { Debug.Log("[Notice] Client connecting to Server..."); } udp.Connect("3.19.185.97", 12345); Debug.Log("udp.Client.LocalEndPoint: " + udp.Client.LocalEndPoint); //Send flag 'connect' to server FlagToServer connectFlag = new FlagToServer(); connectFlag.flag = NetworkMan.flag.CONNECT; msgJson = JsonUtility.ToJson(connectFlag); //Byte[] sendBytes = Encoding.ASCII.GetBytes("connect"); sendBytes = Encoding.ASCII.GetBytes(msgJson); udp.Send(sendBytes, sendBytes.Length); udp.BeginReceive(new AsyncCallback(OnReceived), udp); if (bDebug) { Debug.Log("[Notice] Client-server connection established from " + udp.Client.LocalEndPoint + " to " + udp.Client.RemoteEndPoint.ToString()); } if (bDebug) { Debug.Log("[Notice] Routinely sending Heartbeat."); } InvokeRepeating("RoutinePing", 1, 1); // Sends 1 heartbeat message to server every 1 second. if (bDebug) { Debug.Log("[Notice] Routinely sending Coordinates."); } InvokeRepeating("UploadLocalClientData", 1, 0.033f); //Send 1 coordinate message to server every 0.033 second. Essentially 30 times per second. }
void RoutinePing() { if (bDebug && bVerboseDebug) { Debug.Log("[Routine] Pinging server..."); } string msgJson; FlagToServer newFlag = new FlagToServer(); Byte[] sendBytes; newFlag.flag = flag.PING; msgJson = JsonUtility.ToJson(newFlag); sendBytes = Encoding.ASCII.GetBytes(msgJson); udp.Send(sendBytes, sendBytes.Length); }
void HeartBeat() { if (bDebug && bVerboseDebug) { Debug.Log("[Routine] Sending message to server: heartbeat"); } string msgJson; FlagToServer flag = new FlagToServer(); Byte[] sendBytes; flag.flag = NetworkMan.flag.HEARTBEAT; msgJson = JsonUtility.ToJson(flag); //Byte[] sendBytes = Encoding.ASCII.GetBytes("heartbeat"); sendBytes = Encoding.ASCII.GetBytes(msgJson); udp.Send(sendBytes, sendBytes.Length); }