Search Unity

Connecting two phones without interaction over local Wifi - is it possible?

Discussion in 'Multiplayer' started by eshan-mathur, Dec 23, 2015.

  1. eshan-mathur

    eshan-mathur

    Joined:
    Nov 22, 2011
    Posts:
    118
    We're interested in building a seamless connection between two phones using UNET, but we're not exactly sure how this would work. By "seamless", I mean absolutely minimal button clicking and game-y GUI stuff going on.

    In particular, I'd like to avoid a "Create" and "Join" button (server/client buttons, whatever).

    Our biggest obstacle in UNET right now is figuring out who needs to be the client and who needs to be the server. The NetworkDiscovery class has this problematic sentence at the bottom:

    So the question is, when two phones are present on the same local network, what strategy can we use to accomplish this magic connection?

    It needs to automatically pick who should be server/client (because without this they won't find each other using NetworkDiscovery) and then connect up automagically (seamlessly). Do we have to subvert NetworkDiscovery and write our own LLAPI discovery code (that would really suck :()? We've REALLY enjoyed working with UNET so far and are hoping that the HLAPI can serve all of our needs for Wifi networking.

    Would love everyone's (especially seanr and Unity dev) input on this.

    P.S. We're okay displaying a server list ONLY in the case that there is more than one other device available to connect to (Disneyland, parties, whatever). It's not ideal, but there really isn't a way around it.
     
  2. eshan-mathur

    eshan-mathur

    Joined:
    Nov 22, 2011
    Posts:
    118
    P.S. How do I get that nice green Multiplayer tag on this thing after I've already created it? Whoops.
     
  3. papiot

    papiot

    Joined:
    Jan 27, 2015
    Posts:
    10
    I am trying to do similar thing, but phone to web LAN connection (also seamless to the phone).

    Basically the user logs in to a web browser, and then turns on the app and the app will pair automatically to the web browser session. Similar to how whatsapp web works, but the other way around - where the web app is the server and the phone is the client.

    any idea how to approach this?

    I tried the NetworkDiscovery - and it works fine, but the problem I'm running into is that the server has to be a non-unity component - something that broadcasts UDP packets that will get picked up by the phone (client).
     
  4. aer0ace

    aer0ace

    Joined:
    May 11, 2012
    Posts:
    1,513
    From a strictly technical design with the requirement to omit create/join buttons, the first player to log onto the network can automatically check for an open listen port and try to connect, and if not it creates the server. Subsequent launches of the app would then try listening to the port and auto connect to the existing server. The disadvantage to this is that you can only ever have one massive session and no private sessions (well technically you can have private sessions on the same port but that'dbe extra engineering and would definitely not be as secure as separate ports). So the also, depending on how many connected players, your server may be doing extra unnecessary work to account for the numerous sessions. Ultimately, the main drawback is security. With allowing users to choose port and network, you can run into "why can't I connect to my friend's game?" issues, and your answer would be unfortunately, there's no way to specify which server you can connect to, and I can see that as very frustrating.
     
  5. eshan-mathur

    eshan-mathur

    Joined:
    Nov 22, 2011
    Posts:
    118
    Yeah, the disadvantage you stated to this approach wouldn't work for us - the game needs to be playable anywhere, so just as much on a public wifi network as a private one.

    A game that seems to do this well is Spaceteam - there's no specification by the user of who is the server and who is the client, only one big play button that gets you in to a "lobby". It's very close to what we'd like to do, except that instead of a lobby you just need to connect TWO phones, so in theory you don't need to wait for anyone else to join therefore don't need a lobby.

    I'm not sure how he's achieving that.
     
  6. eshan-mathur

    eshan-mathur

    Joined:
    Nov 22, 2011
    Posts:
    118
  7. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    Click on "Thread Tools", click on "Edit Title", and select the flair.
     
  8. eshan-mathur

    eshan-mathur

    Joined:
    Nov 22, 2011
    Posts:
    118
    Bump again.
     
  9. molx88

    molx88

    Joined:
    Apr 6, 2015
    Posts:
    6
    I've just been going through a conversion of my handwritten network discovery using UDP broadcasting/listening, to now using the NetworkDiscovery component. I've had great success, but wouldn't exactly fit with what you're trying to do.

    You can use the NetworkDiscovery Component, but you have to write a custom version of it that overrides the OnReceivedBroadcast() method. Within this method I would check to see if any servers are found, if so then handle joining that server automatically, but if you do not find a server, then you would want to create a server for others to join. This could all be done in the background without the user even knowing.
     
  10. Jinja

    Jinja

    Joined:
    Nov 28, 2012
    Posts:
    10
    @eshan.mathur Did you find a solution? I haven't tried this myself, but why not alternate being the server and client for either 1, 2 or 3 seconds (chosen randomly). It shouldn't be too long before one device is the server and one is the client.
     
  11. eshan-mathur

    eshan-mathur

    Joined:
    Nov 22, 2011
    Posts:
    118
    @Jinja Yes - it turns out you can run two NetworkDiscoveries at once. You just need to stick the scripts on separate game objects :)
     
  12. Hamidreza1010

    Hamidreza1010

    Joined:
    Jul 20, 2017
    Posts:
    3
    HQHAS HSOASASASSAASSASAASASSASASAASASASASSAASSASASAS
     
    VirtualPierogi likes this.
  13. Hamidreza1010

    Hamidreza1010

    Joined:
    Jul 20, 2017
    Posts:
    3
    Excuse me
     
  14. MadDevil

    MadDevil

    Joined:
    Nov 4, 2014
    Posts:
    4
    @eshan-mathur, any luck with the solution ???

    I tried the thing with networkserver, networkclient and networkdiscovery, it works but then i am not able to spawn players or network objects using that environment.
     
  15. palgony100

    palgony100

    Joined:
    May 11, 2020
    Posts:
    2
    Can Somebody Explain me how you can do it becuse i am lost