Attached is a simple project that uses the NetworkDiscovery component that is new in Unity 5.2.
2294959–154376–Broadcast.zip (30.8 KB)
Attached is a simple project that uses the NetworkDiscovery component that is new in Unity 5.2.
2294959–154376–Broadcast.zip (30.8 KB)
Thank you very much!
Never mind it’s so simple now
M.
Just to help out new comers. If you don’t want to use the default gui, you’ll have to override OnReceivedBroadcast() and parse the data string. You then can join a game directly or add the game to a server browser.
@seanr Is it always NetworkManager:adr:port
?
Yes, it is that when using NetworkDiscovery with the NetworkManager.
Will there be any support for multiple clients?
I’d like to have my android phone act as a client and my desktop machine act as server but I’ve been unable to get this example working for this scenario.
If I run this example on my desktop (Initilalise Broadcast > Start Broadcasting) and laptop (Initialize Broadcast > Listen for Broadcast) the laptop successfully reports the desktop IP.
But if i export the scene and run it on my android phone (Initialize Broadcast > Listen for Broadcast) it fails to find the server (doesn’t report game IP).
All machines are connected to the same wifi network.
Does anyone have an idea of what I may be doing wrong or what I need to look at to get the android client to see the server? (I can provide additional information about my setup if necessary of course)
Hi, seanr,
I came across some problem when trying to change broadcastData in NetworkDiscovery, here is my post:http://forum.unity3d.com/threads/networkdiscovery-broadcastdata-can-not-change.368308/
can you help me?
Hi seanr ( @seanr )
When I initialize the broadcast as server, the other device(client) is connected to the server (connectivity successful) IP. But the packets are not being sent from the game port.
I keep getting this error message : " UNet Client Disconnect Error: Timeout UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate() site:forum.unity3d.com".
But works completely fine when I run the server and client on the same system.
@seanr
Hi. Tested this sample in editor (5.2.3f1). On 3rd host restart (Lan host, stop, lan host, stop, lan host) I get several errors:
Broadcast discovery has been already running. Stop discovery first before repeat this call
UnityEngine.Networking.NetworkDiscovery:StartAsServer()
MyNetManager:OnStartHost() (at Assets/MyNetManager.cs:13)
UnityEngine.Networking.NetworkManagerHUD:OnGUI()
NetworkDiscovery StartBroadcast failed err: 8
UnityEngine.Networking.NetworkDiscovery:StartAsServer()
MyNetManager:OnStartHost() (at Assets/MyNetManager.cs:13)
UnityEngine.Networking.NetworkManagerHUD:OnGUI()
Cannot open socket on ip {*} and port {7777}; check please your network, most probably port has been already occupied
UnityEngine.Networking.NetworkManagerHUD:OnGUI()
StartServer listen failed.
UnityEngine.Networking.NetworkManagerHUD:OnGUI()
Hi @seanr
Sorry for my bad English,
I am trying to run your example of Network Discovery. If I run server and client both on my PC its working fine. If I run server on other PC within same network and I run client on different PC. Its is finding server(IP) after click on ‘Listen for Broadcast’ but after click on IP button, my Player does not create clone. I am working with Unity 5.3.
Hey - did you manage to get this resolved? I am trying to do exactly the same thing - have the PC as server and android phone as client
Can this class be used to have two phones discover each other without knowing which one will be server/client? The only way to know that is to have the user select an option, but we’d like a more seamless, no-interaction type experience for our casual audience.
I’ve posted more detail about my question in the link below, but I thought I’d mention it here since it’s very related to the capabilities of NetworkDiscovery.
not yet
Oh weird, i thought i posted a reply to this already but it looks like it got eaten somewhere.
Short version: Not all android phones accept multicast messages. The ones that don’t (e.g. Motorola Moto G and moto E) won’t ‘auto discover’ the server with the approach used in this script. Others will (e.g. Nexus 5).
I ended up exposing an input field for non-auto-discovery phone users so they could manually enter the IP of the server if auto-connect wasn’t happening.
Hi - I’m a beginner with the whole multiplayer side of things and just trying to get my head around NetworkDiscovery. When I run this example the sphere gets spawned when I use the LAN Host and client. But when I use broadcast and and connect to a game nothing gets spawned, how can I spawn from the NetworkManager once connected through the Broadcast? Many Thanks!
I am trying to get this working without the GUIs, both the Discovery GUI and the Network Manager GUI, but I can’t figure out how.
What I want to achieve is similar to @bitbutter 's question: connect my android phone to my desktop PC.
So in high level code what I want is:
#if UNITY_EDITOR || UNITY_STANDALONE
Initialize()
StartAsServer()
#elif UNITY_ANDROID
Initialize()
StartAsClient()
JoinGame()
#endif
That last JoinGame obviosuly does not exist. For that I would need to override OnReceivedBroadcast() as @HugoZandel mentioned. So I tried to make my own NetworkDiscovery class, see below, using the example OnBroadcastReceived from the Unity documentation. I then replaced @seanr 's NetworkManager with the default NetworkManager, but when I start the app first in the editor and then on my phone I don’t see a playerSphere appear. I do get the debug message “Started as server” in the editor.
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class MyNetDiscovery : NetworkDiscovery
{
void Start ()
{
#if UNITY_EDITOR
Initialize();
StartAsServer();
Debug.Log("Started as server");
#elif UNITY_ANDROID
Initialize();
StartAsClient();
#endif
}
public override void OnReceivedBroadcast(string fromAddress, string data)
{
NetworkManager.singleton.networkAddress = fromAddress;
NetworkManager.singleton.StartClient();
}
}
Thanks!
Edit 2016-01-20: I did some more debugging and added a second scene to start when the connection is established (added MyOne as offline scene and two as online scene in the NetworkManager). I found that I do get to the end of the OnReceivedBroadcast, but the online scene never loads, neither on the server nor on the client.
The same happens if I use the example’s scene one. Also in that case I do not get to scene two. Although I am not completely sure I did everything right because it is very, very hard to see the GUI on my mobile phone, it is incredibly small. But I think I saw a button with connect to and an IP-address. And when I tapped on it the discovery buttons disappeared, although the network manager GUI was still on screen.
I then tried to use a standalone version as client and the editor as server. Now I could read the button which said Game on ffff: and my PC’s IP-address, but after clicking on it the same result. The broadcast GUI disappears but the Network Manager GUI remains on screen and still no second scene.
Do I need to call something like NetworkManager.OnClientReady?? I would expect that this is handled by the Discovery component.
After some more searching, cursing and retrying I found that I needed to start the NetworkManager on the server side as well. I added NetworkManager.singleton.StartServer() in the #if UNITY_EDITOR section after StartAsServer().
Oops there seems some bug with NetworkDiscovery.
When NetworkDiscovery.StartAsServer is invoked.
The Object will become don’t destroy on load
public bool StartAsServer()
{
if (this.m_HostId != -1 || this.m_Running)
{
if (LogFilter.logWarn)
{
Debug.LogWarning("NetworkDiscovery StartAsServer already started");
}
return false;
}
this.m_HostId = NetworkTransport.AddHost(this.m_DefaultTopology, 0);
if (this.m_HostId == -1)
{
if (LogFilter.logError)
{
Debug.LogError("NetworkDiscovery StartAsServer - addHost failed");
}
return false;
}
byte b;
if (!NetworkTransport.StartBroadcastDiscovery(this.m_HostId, this.m_BroadcastPort, this.m_BroadcastKey, this.m_BroadcastVersion, this.m_BroadcastSubVersion, this.m_MsgOutBuffer, this.m_MsgOutBuffer.Length, this.m_BroadcastInterval, out b))
{
if (LogFilter.logError)
{
Debug.LogError("NetworkDiscovery StartBroadcast failed err: " + b);
}
return false;
}
this.m_Running = true;
this.m_IsServer = true;
if (LogFilter.logDebug)
{
Debug.Log("StartAsServer Discovery broadcasting");
}
Object.DontDestroyOnLoad(base.gameObject);
return true;
}
I know that’s necessary when enter game scene, we need a component to control lan game.
But If I have two Scenes : LanGame, Online
If I go back from Online scene to LanGame scene, NetworkDicovery object will become duplicated.
My current workAround is write another script inherit NetworkDiscovery, and make it as singleton.
When I enter LanGame scene, stop NetworkDiscovery if it’s running.
Does official expect we manually destroy the old NetworkDiscoery?
Hi,the following question "Cannot open socket on ip {*} and port {7777}; check please your network, most probably port has been already occupied " ,do you have to solve the question?