Reputation: 51
I want to implement a service discovery module to fetch the IP address of a service broadcasting device, similar to the bonjour browser. Is it possible to implement it in Unity3D(Android/iOS). Can anyone guide me on how can I achieve it. A small example would be really helpful. Thanks
Edit 1: An IOT device is broadcasting a DNS service, and I want to fetch this service in the Unity application to find the IP address of the IOT device. I don't have any access to the IOT device software or source code.
Upvotes: 3
Views: 2488
Reputation: 816
The new solution native to unity is the Netcode Network Discovery
It's not actually a supported solution anymore. There is just sample code.
Edit: They removed that discovery code. Which makes sense because it didn't seem to be functional. I still used UNet, but supposedly there's reference of a discovery system here
Upvotes: 0
Reputation: 51
Here is an iOS plugin that does DNS Service discovery: https://docs.unity3d.com/Manual/PluginsForIOS.html
Upvotes: 0
Reputation: 125275
You don't have to do this from scratch. There is a new Unity API called NetworkDiscovery
which is designed to simplify this.
To find which IP Address to connect to, call NetworkDiscovery.StartAsServer()
on the server side once in the Start()
function.
On the client side, call NetworkDiscovery.StartAsClient()
then implement the OnReceivedBroadcast(string fromAddress, string data);
function. When server is found, the OnReceivedBroadcast(string fromAddress, string data);
function will be called and you can then use the returned fromAddress
value to connect to your server.
When client connects, you can stop broadcasting by calling NetworkDiscovery.StopBroadcast()
.
If you are just doing this for learning purposes, you just need to Broadcast with UDP protocol. For more information, you can read this and this.
Upvotes: 2