- Home /
Broadcast data not updating.
Hello!
I have a simple interface for a LAN Game: 1 textbox (to input host name), 1 host button (which modifies in a "STOP" button after "HOST" is clicked), 1 scroll list with all servers available and 1 join button. If i put "AAAA" name in textbox, press "HOST", after that press "STOP", the other devices will still see the server as available altough it is not (you can't connect to it).
public void Host() {
if (!isAlreadyHost) {
if (hostName.text.Length != 0) {
isAlreadyHost = true;
discovery.StopBroadcast ();
discovery.Initialize ();
discovery.broadcastData = hostName.text;
discovery.StartAsServer ();
manager.StartHost ();
GameObject.Find ("Host").transform.GetChild (0).GetComponent<Text> ().text = "Stop";
} else if (isAlreadyHost) {
isAlreadyHost = false;
GameObject.Find ("Host").transform.GetChild (0).GetComponent<Text> ().text = "Host";
manager.StopHost ();
discovery.StopBroadcast ();
discovery.Initialize ();
discovery.StartAsClient ();
}
}
void Start() {
discovery.Initialize();
discovery.StartAsClient();
}
To update the list:
public void updateListValues() {
GameObject[] oldOptions = GameObject.FindGameObjectsWithTag ("option");
foreach (GameObject oldOpt in oldOptions) {
Destroy (oldOpt);
}
nr = discovery.broadcastsReceived.Count;
if (nr > 0) {
serverIps = new string[nr];
serverNames = new string[nr];
serverResults = new NetworkBroadcastResult[nr];
discovery.broadcastsReceived.Keys.CopyTo (serverIps, 0);
discovery.broadcastsReceived.Values.CopyTo (serverResults, 0);
for (int i = 0; i < nr; i++) {
serverNames [i] = BytesToString (serverResults [i].broadcastData);
Button btnOption = Instantiate (option);
btnOption.transform.SetParent (gOparent.transform);
Vector3 optionPosition = new Vector3 (0.0f, -28.0f * i, 0.0f);
btnOption.transform.localPosition = optionPosition;
btnOption.transform.localScale = Vector3.one;
btnOption.GetComponent<RectTransform> ().sizeDelta = new Vector2 (0.0f, 30.0f);
GameObject optChild = btnOption.transform.GetChild(0).gameObject;
serverIps[i] = returnIP(serverIps[i]);
optChild.GetComponent<Text> ().text = serverNames [i];
btnOption.name = serverIps [i];
}
}
Answer by DiegoSLTS · Aug 28, 2016 at 12:36 AM
I'm reading the docs for NetworkDiscovery.broadcastsReceived and it never says how and when it's updated. I could be that it just stores every broadcast message received since Initialize runs.
I don't think that's correct. If it would be like you say, than after I press "HOST" one more time with the name "BBBB", for example, I should have 2 items on the list, but ins$$anonymous$$d I've got only the BBBB, and the "AAAA" is gone.
Thanks anyway!
Yes, what I said is probably incorrect. What I mean was that since the docs don't explain when and how that dictionary is updated, your assumption that it would contain only valid broadcasts could be wrong, and that you'll have to test different scenarios to see what's actually happening.
Also, maybe what I said isn't entirely wrong, the key to the dictionary is the address of the server, so when hosting a game with "AAAA" and then another with "BBBB" from the same server both broadcast messages use the same key, and the second one overrides the first one.
Broadcast messages are recevied in an async and probably not constant frequency, so you can't get something like a struct with all broadcast messages received in the current frame.
You might want to add some extra info in another dictionary of a similar struct with a timestamp, and update that timestamp every time you receive a message from the same address. When listing the current servers, check that timestamp against a threshold that you defined, so you can filter old messages received from servers that probably are not hosting.
Or since broadcastsReceived is not readonly you might be able to empty that every few seconds, so it's populated with recent broadcast messages only.