void ReceiveAsync(IAsyncResult ar) { Debug.Log("Received Something ..."); try { IPEndPoint ipEndpoint = null;// = new IPEndPoint(IPAddress.Any, 0); //sender ip byte[] data = connection2.EndReceive(ar, ref ipEndpoint); // string datastr = System.Text.Encoding.UTF8.GetString(data); // Debug.Log("Received message: " + datastr); lastMessage = MyMessageSerializer.DeserializeFromBytes <MyMessage>(data); currentPhase = lastMessage.Phase; //Debug.Log("lastMessage phase: " + lastMessage.Phase); Debug.Log("current phase: " + currentPhase.ToString()); ////https://www.dotnetperls.com/enum-parse if (isServer) { AddClient(ipEndpoint); //do not remove this, or else the server cannot write back!! } // Debug.Log("\nlastMessage SteeringInput: " + lastMessage.SteeringInput); //Application.Quit(); } catch (SocketException e) { // This happens when a client disconnects, as we fail to send to that port. Console.WriteLine(e.ToString()); } connection2.BeginReceive(new AsyncCallback(ReceiveAsync), null); }
/* * public void Send(string message) * { * //Debug.Log ("Send()"); * //Debug.Log("Targets to send to: " + countConnections()); * byte[] data = System.Text.Encoding.UTF8.GetBytes(message); * BroadcastChatMessage(data); * } */ public void Send(MyMessage mymessage) { // Debug.Log("Send()"); byte[] data = MyMessageSerializer.SerializeToBytes <MyMessage>(mymessage); BroadcastChatMessage(data); }