Hello .
I am currently trying to understand how NetworkDiscovery works, because the documentation on it is a quite sparse. And I am a bit puzzled by the behaviour of that component.
I started using the default NetworkDiscovery component, everything works just fine, my two computers on LAN can broadcast to each other, no problem.
The next thing I tried was having just four buttons, respectively calling NetworkDiscovery’s Initialize(),StartAsServer(), StartAsClient(), StopBroadcast().
I manage to crash the editor by doing the following :
One computer is put on broadcast mode.
On the other computer I followed this sequence : Initialize>StartAsClient (Here I am receiving the broadcast) >StopBroadcast>StartAsClient> and then it crashes.
( I have reported that one )
So what I got from that is that it is needed to re Initialize after stopping, and before trying to re start as client or server. Which I find a bit weird, and am not sure if it’s a bug or the correct behaviour. I would have thought that you only need to initialize once.
The thing I am currently trying to do is using my own class, inheriting from NetworkDiscovery, to attempt to switch between client and server. The end goal is to make some sort of matchmaking over LAN, but I quickly got rid of any NetworkManager related code to focus on attempting to make NetworkDiscovery works reliably.
public class LANMatchmaker : NetworkDiscovery
{
void Start()
{
if (this.Initialize())
{
this.StartAsClient();
}
}
public void StartHosting()
{
this.StopBroadcast();
if (!running)
{
if (this.Initialize())
{
this.StartAsServer();
print("Starting as an Host on hostId : " + this.hostId);
}
}
}
public void BackToListening()
{
this.StopBroadcast();
if (!running)
{
if (this.Initialize())
{
this.StartAsClient();
print(" Back to listening , hostId : " + this.hostId);
}
}
}
}
Two buttons in the scene, one for calling StartBroadcasting, the other for BackToListening. It properly start as client, receiving messages if the other computer is broadcasting. If I switch to broadcasting, it also works, and the other computer is receiving correctly. However if I switch back to client, it’s no longer receiving anything. The booleans “running”, “isServer” and “isClient” are displaying the correct value for each state. But once switch off it’s initial state as a client, my component inheriting from NetworkDiscovery doesn’t seem to be able to correctly switch back to it. Broadcasting is fine though.
Now, someone helped me “improve” on this problem, by changing my two methods to the following :
public void StartHosting()
{
this.StopBroadcast();
if (!running)
{
Debug.Log("Start hosting"); //magie! ne pas enlever
if (this.Initialize())
{
this.StartAsServer();
print("Starting as an Host on hostId : " + this.hostId);
}
}
}
public void BackToListening()
{
this.StopBroadcast();
if (!running)
{
Debug.Log("Start listening"); //magie! ne pas enlever
if (this.Initialize())
{
this.StartAsClient();
print(" Back to listening , hostId : " + this.hostId);
}
}
}
Spot the differences.
Now this doesn’t actually solve the problem. It just inconsistently allows to switch back and forth between server and client a few times, before the client definitely stops receiving any messages at all.
So I guess this all boils down to the following questions :
What am I doing wrong ?
What is the correct way of using NetworkDiscovery ?
What is exactly going on inside Initialize(), StartAsServer(), StartAsClient(), StopBroadcast() ? Are those methods asynchronous in some way ?
Edit > Corrected the code in the first part, it was badly copy pasted
Thanks, this seems to work. It would be nice if this was addressed somewhere officially, because the lack of information on networking in unity is starting to give me a headache. Now it's the NetworkManager that I am trying to had on top of the NetworkDiscovery that is being unreliable and is throwing errors out of nowhere. But that's for another topic I suppose.
– Charles-Dexter-WardSame problem for me. Perfect for your solution !! Thanks
– Twil75Thank you! This helped us with a different problem: Our GearVR was heating up and draining battery in standby mode. We have a multiplayer component with unet implemented - so ultimately stopping NetworkTransport onApplicationPause and Initializing it on Resume again solved the heating issue!
– ExtrawrigleyI tried your solution but it does not fix the issue for me
– MaxFrax96Thanks! It's help me!
– Dm-Smirnov