1. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice
  2. We're looking for feedback on Unity Starter Kits! Let us know what you’d like.
    Dismiss Notice
  3. We’re giving 2017.1 beta testers a chance to win t-shirts and a Nintendo Switch. Read more on the blog.
    Dismiss Notice
  4. We want to know how you learned Unity! Help us by taking this quick survey and have a chance at a $25 gift card
    Dismiss Notice
  5. Are you an artist or level designer going to Unite Europe? Join our roundtables there to discuss artist features.
    Dismiss Notice
  6. Unity 5.6 is now released.
    Dismiss Notice
  7. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice

Unity Multiplayer Master Server sample project

Discussion in 'Multiplayer Networking' started by seanr, Jun 9, 2015.

  1. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    MasterServer Sample

    This is a "MasterServer" similar to the MasterServer that shipped with the old Raknet based networking system. The project is implemented using the HLAPI messaging system, not using [Command]s and [ClientRpc] calls, so it uses the MessageBase class to define messages which are registered with handler functions. The file Master.cs contains the network protocol built with these messages.

    There are two components, a MasterServer and a MasterClient. The MasterClient has an API similar to the old network system. There is a simple UI for controlling each component.

    The MasterServer uses the NetworkServer class and listens for connections from MasterClients - which use the NetworkClient class. It allows clients to register as hosts with a name and a gameType, and to request a list of hosts by gameType. The list of hosts that is returned includes IP address and port.

    So this could be used for local matchmaking - not over the internet unless all the hosts have public IPs.
     

    Attached Files:

    XxSimBaaxX, Wobes, laurachen and 7 others like this.
  2. yinjc

    yinjc

    Joined:
    Jun 10, 2015
    Posts:
    1
    how can i do if i want deploy a master server on a linux server?
     
  3. dbulatov

    dbulatov

    Joined:
    Apr 28, 2014
    Posts:
    2
    In all the documentation pages and in this example it uses the localhost as the IP (127.0.0.1). How do I actually find the IP of the server from the client perspective?
     
  4. MSylvia

    MSylvia

    Joined:
    May 19, 2009
    Posts:
    42
    This is something that would need to be known in advance. Your client would ship knowing that address or of a way to find it from some external service you host.
     
    StarGamess likes this.
  5. ObliviousHarmony

    ObliviousHarmony

    Joined:
    Jul 3, 2014
    Posts:
    79
    I'm currently evaluating whether I want to create my own master server or utilize Unity's multiplayer services, and I had a quick question about your example here.

    You stated that this cannot be used over the internet, but it would still be possible to use the connection information on the master server to perform a primitive NAT punchthrough, correct? I recognize that this wouldn't work on stricter NAT setups that require the packets to come from the IP it initially connected to.

    For bonus points, I've actually got a few questions about the multiplayer services that I haven't found any answers to:

    1. Do you know when any pricing and restriction information will be announced? I'd like to have all of the facts before I commit to using the services as the underlying tech for my game. I read a thread that had limits based on messages and message size that placed limits at 30msg/s and 150 bytes per message. Is this incoming, outgoing, or both?

    1a. In addition, the networking profiler doesn't seem to give any information about message sizes?

    1b. How good at buffering messages is the SyncVar system, and will the compression/buffering work to keep the message < 150 bytes?

    1c. Is it possible to use the matchmaker without the relay server, and simply use NAT punchthrough in the method above?

    2. Do you have any information about relay server geographical locations, and any sort of performance metrics for how having the relay server affects ping?
     
    EMGPY and smkymcpimsht like this.
  6. dbulatov

    dbulatov

    Joined:
    Apr 28, 2014
    Posts:
    2
    Yes, in a case of an online multiplayer game. But, if you are making a local multiplayer game over WiFi, there is no way to find out the IP using the HL API.

    The only way you can do it is by using the Transport Layer with Broadcast functionality, which currently doesn't work on mobile... (pc tests were successful).

    So, is there something planned for HL API that mimics the Broadcasting functionality of LL API?
     
  7. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    The Broadcast functionality only works on a LAN, not over the internet.

    And, there will be broadcast support in the HLAPI.
     
  8. clever

    clever

    Joined:
    Oct 11, 2012
    Posts:
    34
    Quick question, does this sample or Init'ing a server in HLAPI open both UDP and TCP hosts to accommodate WebGL? I've only seen examples for WebGL hosts on LLAPI unless I missed a page somewhere.

    Also, would I be able to design an Authoritative server (internet) without any usage of the Unity service (ie, no Unity-based limits)?
     
  9. bgprocks

    bgprocks

    Joined:
    Oct 28, 2012
    Posts:
    4
    As a former network engineer, my advice is: when you ship a game, don't ever hard code the IP. use a name like mygame.domain.com. This way if your IP change it won't kill your game. Use one of the free DNS service on the Internet to setup a name that points to your IP. For testing locally on your machine, you can put the name in your HOST file, and have it point to your loopback (127.0.0.1).. Example, while working on your game you can edit your "hosts" file in your "windows\system32\drivers\etc" to add the following

    127.0.0.1 yourgame.freedns.org

    or

    192.168.1.10 yourgame.freedns.org

    and on the Internet the same name could point to the public IP of your game server.
     
    Last edited: Jun 30, 2015
  10. shame

    shame

    Joined:
    Jun 25, 2012
    Posts:
    130
    So when the uNet phase 2 will start, so we will be able to use it outside Unity3D engine ?

    I like Unity3D for physics, navigation and stuff. Unity engine instance is perfect for hosting a game server, but for things like master server it's different.
    Old mono version doesn't allow us to use the new async programming introduced in C# 4.
    Right now I can't find any way to keep MSSQL server calls away from blocking threads on unity's mono, and the overhead of Unity engine for simple things like this is gigantic.

    UPD: Nevermind, found the answer myself in here http://unity3d.com/unity/roadmap
    Standalone simulation server should be available in 16 mar of 2016
     
    Last edited: Jul 3, 2015
    EMGPY likes this.
  11. shame

    shame

    Joined:
    Jun 25, 2012
    Posts:
    130
    @ObliviousHarmony you can use this thing to avoid NAT issues via uPnP or PMP, but it's not very reliable. You can use it if you don't need 100% success on hosting a game from every client. But manual port forwarding will always do the trick.
    https://github.com/nterry/Mono.Nat
     
    EMGPY likes this.
  12. hMark

    hMark

    Joined:
    Oct 16, 2013
    Posts:
    14
    is it possible to setup more than 8 connections somehow?

    this code throws error UNet Client Disconnect Error: CRC Mismatch.
    ConnectionConfig config = new ConnectionConfig();
    NetworkServer.Configure(config, 8);
     
  13. henry_jvx

    henry_jvx

    Joined:
    May 18, 2015
    Posts:
    1
    Nicee! :eek: that was solved my life
     
  14. tinman

    tinman

    Joined:
    Jan 8, 2011
    Posts:
    229
    Can this master server be used to register both clients (players) and servers? And can players still be connected to the master server while connected to a game server?
     
  15. RyuMaster

    RyuMaster

    Joined:
    Sep 13, 2010
    Posts:
    467
    I am using this as template and works pefect for PC builds, but on Android WiFi I get random disconnects every few seconds. Increasing timeout or spamming packets to keep connection alive did not help. It happens if I sent data or do nothing - client simply disconnects. Any idea what I am missing here? I am on the Unity 5.3.?p5
     
  16. IAMBATMAN

    IAMBATMAN

    Joined:
    Aug 14, 2015
    Posts:
    258
    Is it just me or when you open the masterserver in unity it's just an empty project?
     
  17. nsmith1024

    nsmith1024

    Joined:
    Mar 18, 2014
    Posts:
    289
    Hello,

    I used your MasterServer code above, and did CLIENT ONLY build for WebGL. Basically i disabled the server gameobject and left the client only enabled.

    The server was running in the Unity editor.

    When i run the WebGL client code in FireFox browser and try to connect to the server running in the unity editor, i get this message:

    Firefox can't establish a connection to the server at ws://127.0.0.1:45555/.
    Error: undefined socket will be closed
    Client Disconnected from Master
    OnFailedToConnectToMasterServer

    Anybody knows how i can fix it? Im trying to make a server that can host multiple rooms to WebGL browser clients, but it dont work using your MasterServer code that i downloaded.

    thanks
     
  18. TheStarboxTR

    TheStarboxTR

    Joined:
    Mar 22, 2014
    Posts:
    7
    @seanr is there any other ways for increasing max "Master Client Connect" limit? max limit is now 8.
    can we increase?
     
  19. jesusluvsyooh

    jesusluvsyooh

    Joined:
    Jan 10, 2012
    Posts:
    332
    I got it to work, locally, and uploading to external server, could connect two players to it and it displayed their created game.

    Do we have a Facilitator? Or connection tester update for Unity 5 unet?

    I have all my own servers so theres no need for Unity to host them, however i do need the programmes XD
    Thanks for the work so far @seanr

    My previous multiplayer games were using the old legacy system, i have a few million downloads in total and the removal of legacy networking is starting to screw me over XD
     
  20. IAMBATMAN

    IAMBATMAN

    Joined:
    Aug 14, 2015
    Posts:
    258
  21. jesusluvsyooh

    jesusluvsyooh

    Joined:
    Jan 10, 2012
    Posts:
    332
  22. MonkeyDKid

    MonkeyDKid

    Joined:
    Dec 18, 2015
    Posts:
    1
    how to know player connected on each room with this??
     
  23. XxSimBaaxX

    XxSimBaaxX

    Joined:
    Oct 23, 2016
    Posts:
    17
    how to run this with a public ip? and make it monitor the players count in each room?