//We will only call this method on the server and provide it the game name and time //We don't provide the game name in StartConnection because the client won't have it and //both the client and server use StartConnection public void Send(float floatTime, string gameName) { //All our values need to be string to be able to be serialized string stringTime = floatTime.ToString(); //We need to create our destination endpoint //It will be at the provided broadcast IP address and port IPEndPoint sendToEndpoint = new IPEndPoint(IPAddress.Parse(sendToIp), sendOrReceivePort); //We create a new ServerInfoObject which we will use to store our data ServerInfoObject thisServerInfoObject = new ServerInfoObject(); //We populate our ServerInfoObject with data thisServerInfoObject.gameName = gameName; thisServerInfoObject.ipAddress = m_MyIp.ToString(); thisServerInfoObject.timeStamp = stringTime; //Then we turn it into JSON string json = JsonUtility.ToJson(thisServerInfoObject); //Then we create a sendBytes array the size of the bytes of the JSON Byte[] sendBytes = Encoding.UTF8.GetBytes(json); //We then call send method on udpClient to send our byte array udpClient.Send(sendBytes, sendBytes.Length, sendToEndpoint); }
//This is another method the client will call to grab all the messages that have been received by listening public ServerInfoObject[] getMessages() { //We created an array of pending messages string[] pendingMessages = new string[0]; //We create an array where we will store our ServerInfoObjects (how we store the JSON) ServerInfoObject[] pendingServerInfos = new ServerInfoObject[0]; //While we get this done we need to lock the Queue lock (incomingQueue) { //We set our pending messages the length of our queue of byte stream pendingMessages = new string[incomingQueue.Count]; //We set our pending server infos the length of our queue of byte stream pendingServerInfos = new ServerInfoObject[incomingQueue.Count]; //We will go through all our messages and update to get an array of ServerInfoObjects int i = 0; while (incomingQueue.Count != 0) { //We start moving data from the queue to our pending messages pendingMessages[i] = incomingQueue.Dequeue(); //We then take our pending message string jsonObject = pendingMessages[i]; //And use FromJson to create our array of ServerInfoObjects pendingServerInfos[i] = JsonUtility.FromJson <ServerInfoObject>(jsonObject); i++; } } //We return an array of ServerInfoObjects to the client that called this method return(pendingServerInfos); }
public void LoadJoinScreenForSelectedServer(ServerInfoObject localGame) { m_GameName = this.Q <Label>("game-name"); m_GameIp = this.Q <Label>("game-ip"); m_GameName.text = localGame.gameName; m_GameIp.text = localGame.ipAddress; }
void ClickedJoinGame(ServerInfoObject localGame) { //We query our JoinGameScreen cVE and call a new function LoadJoinScreenForSelectedServer and pass our GameObject //This is an example of clicking a list item and passing through data to a new function with that click //You will see in our JoinGameScreen cVE that we use this data to fill labels in the view m_titleScreenManagerClass.Q <JoinGameScreen>("JoinGameScreen").LoadJoinScreenForSelectedServer(localGame); //We then call EnableJoinScreen on our TitleScreenManager cVE (which displays JoinGameScreen) m_titleScreenManagerClass.EnableJoinScreen(); }
void ReceivedServerInfo(ServerInfoObject serverInfo) { //Filter to see if this ServerInfoObject matches with previous broadcasts //We will start by thinking that it does not exist bool ipExists = false; foreach (ServerInfoObject discoveredInfo in discoveredServerInfoObjects) { //Check if this discovered ip address is already known if (serverInfo.ipAddress == discoveredInfo.ipAddress) { ipExists = true; //If a ServerInfoObject with this IP address has been discovered, when did we hear about it? float receivedTime = float.Parse(serverInfo.timeStamp); //What about this broadcast? float storedTime = float.Parse(discoveredInfo.timeStamp); //We will update to the latest information from the IP address that has been broadcast //The host might have quit and started a new game and we want to display the latest info if (receivedTime > storedTime) { //Set the data to the new data discoveredInfo.gameName = serverInfo.gameName; discoveredInfo.timeStamp = serverInfo.timeStamp; //Now we need to update the table m_ListView.Refresh(); } } } //If the ip didn't already exist, add it to the known list if (!ipExists) { //We add it to the list discoveredServerInfoObjects.Add(serverInfo); //We refresh our list to display the new data m_ListView.Refresh(); } }