Search Unity

Showcase Mirror - Open Source Networking for Unity

Discussion in 'Multiplayer' started by mischa2k, Aug 11, 2016.

  1. goldbug

    goldbug

    Joined:
    Oct 12, 2011
    Posts:
    767
    Shiny new feature: The 2018 branch now has a really fast and modern async websocket transport. It can also listen to multiple transports at the same time! Your facebook users (websockets) and android users (tcp or whatever transport you want) can connect to the same server and interact with each other.



    This is the default setup, both TCP and Websockets are on, listening on different ports. Out of the box, your project will run fine if you build it as standalone or webgl.
     
  2. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Yes

    Thanks mate, we are glad that you like it!
     
  3. Player7

    Player7

    Joined:
    Oct 21, 2015
    Posts:
    1,533
    Nice, can one be added for UDP support, so TCP and UDP maybe with Websocket aswel?
     
  4. goldbug

    goldbug

    Joined:
    Oct 12, 2011
    Posts:
    767
    It is doable, but the only UDP based transport we have built in is LLAPI. We can't turn LLAPI on by default because it kills FPS.

    You can easily do this yourself. Something along these lines:


    Code (CSharp):
    1. public override void InitTransport()
    2. {
    3.     TransportLayer tcpTransport = new TcpTransport();
    4.     // configure port and whatnot
    5.  
    6.     TransportLayer ignoranceTransport = new IgnoranceTransport();
    7.     // configure port and whatnot
    8.  
    9.     TransportLayer websocketTransport = new WebsocketTransport();
    10.     // configure your websocket transport
    11.  
    12.     // create a transport that multiplexes between all 3
    13.     NetworkManager.transport = new MultiplexTransport(tcpTransport, ignoranceTransport, websocketTransport);
    14. }
     
  5. Roamer79

    Roamer79

    Joined:
    Oct 25, 2016
    Posts:
    45

    Whoooo!!! I've been waiting for this to be fixed!! Good job.

    Sorry to be greedy by any chance we can incorporate networkchild objects into this or fix the NetworkTransformChild as well?? Would be nice just to have it all in one component!

    Good job and thank-you!!
     
  6. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Already did. Still called NetworkTransformChild.
     
  7. Roamer79

    Roamer79

    Joined:
    Oct 25, 2016
    Posts:
    45
    Awesome! Double thanks!!! Hey the interpolation user setting are gone! Is it all optimized?
     
  8. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Which settings to do you mean? It didn't have interpolation before.
     
  9. Happycamper8

    Happycamper8

    Joined:
    Aug 31, 2017
    Posts:
    7
    A new problem for me! May be just the localhost problem, not sure.

    I've got things working again with Mirror, and the multiplayer training simulation is working great. I created it as a proof of concept so I added only 4 "players" connecting to the "game". Things have been working great, however in the end I need a total of 12 connected.

    Just got the thing setup so I could add 12 total "players". When testing it out I get the following:

    I launch the Host using unity.
    I launch 4 instances of the program by double clicking the exe. These four connect in using localhost.
    I now try a 5th instance making 6 total players and the message showing up in the unity console is the following:
    Server too full, disconnected a client

    It seems to only allow connecting in a total of 5 players. When adding the 6th, that message shows up.

    Is this due to using localhost? Will this error happen if I use 12 different computers and not localhost? I may be screwed if I can't use more than 5 total players. Also, I may be stating terms incorrectly such as localhost as I'm not really a programmer. Talk to me like I'm stupid :)

    Thanks
     
  10. Player7

    Player7

    Joined:
    Oct 21, 2015
    Posts:
    1,533

    "We can't turn LLAPI on by default because it kills FPS." ...why does it kill fps? Would be great to see a working example of this with TCP and UDP support. Personally think uSurvival should be split up to use UDP for the movement/shooting while TCP for chat/inventory...? will it happen?
     
  11. Happycamper8

    Happycamper8

    Joined:
    Aug 31, 2017
    Posts:
    7
    I apologize everybody. Just noticed there is a max connections setting and it was set to 4 by default...
    Thanks
     
  12. goldbug

    goldbug

    Joined:
    Oct 12, 2011
    Posts:
    767
    Ideally I agree with you: movement via UDP/unreliable, chat, inventory, etc via TCP/reliable.
    That is not done yet, and that is different from what I posted before.

    LLAPI is a black box, there is no telling why it does anything. What I can tell you, is that a lot of people experienced higher FPS when we switched from LLAPI to Telepathy. It looks like LLAPI sleeps on every frame or something, who knows.

    LLAPI is on it's way out, I believe they removed it on 2019.1 already, so it is probably not smart to rely on it for your game. There is another transport available with UDP: Ignorance, which is based on ENET. The problem is that this is a native library written in C, so it is not portable to all platforms.

    Personally, I think the way to go is to have a small, simple unreliable only UDP transport with System.Net classes. Have the unreliable channel use that transport and the reliable channel use TCP. Someone has to do that work. If you care about unreliable, and you have the skills, you are welcome to write such transport. HMU in discord if you have questions or want to brainstorm about it.
     
  13. Player7

    Player7

    Joined:
    Oct 21, 2015
    Posts:
    1,533
    Yeah I wouldn't want to use LLAPI it is as you say a black box, and worst just awful for headless terminal servers as the black box nature of compiled source code means you can't change shhhit about it.. like it spitting out errors in the console output.. :mad: sucks if you wanted to have a console based gui.. I know I tried.. and gave up when they wouldn't be able to do anything about that issue..because of multiple platforms doing things differently.. still there are a bunch of other open sourced UDP based frameworks that could be supported.. like https://github.com/RevenantX/LiteNetLib ?

    It's not me, really not in my skillset at all, I just want something that is already proven to work with some examples I can build on from done in this way really...

    "HMU in discord if you have questions or want to brainstorm about it."

    might do just to see where things are going with it and updates in this area.
     
  14. goldbug

    goldbug

    Joined:
    Oct 12, 2011
    Posts:
    767
    I will be happy to answer questions and offer guidance if someone wanted to take on this project.

    LiteNetLib is essentially an RUDP transport, that supports unreliable. Just like Ignorance and LLAPI. If we don't need reliable (because we have TCP for that), then it is overkill and we would be using maybe 10% of their code. A class that just sends and receives udp packages would be a lot simpler.
     
    Last edited: Jan 11, 2019
  15. Roamer79

    Roamer79

    Joined:
    Oct 25, 2016
    Posts:
    45
    Well its been ages since I last looked at Network Transform but last time I did (before mirror) it used to look like this: (having threshold and interpolation variables)

     
  16. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    There is an interpolate movement factor, yes. But it did not interpolate transform movement before. It does now :)
     
  17. Roamer79

    Roamer79

    Joined:
    Oct 25, 2016
    Posts:
    45
    Maybe something is missing in my update but when I add Network Transform now all I get is below. I have no interpolation controls or thresholds just a compress rotation box?? What have I done?!!!!
    upload_2019-1-13_10-4-55.png
     
  18. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    You did nothing wrong, that's just the new NetworkTransform. Takes care of everything internally for you.
     
    hopeful likes this.
  19. goldbug

    goldbug

    Joined:
    Oct 12, 2011
    Posts:
    767
    Great news everyone. Mirror is now moving to a source based asset. No more compiling mirror with visual studio or importing mirror DLL’s, no more messing with editor/standalone dlls. Instead, you import mirror sources in your project and it just works.

    This is already released in https://github.com/vis2k/Mirror/releases/tag/master-1094 Just download Mirror.zip and decompress in your Asset folder.

    Note, for those of you upgrading from previous versions of Mirror, you will need to replace your NetworkIdentities, NetworkProximityCheckers and any other mirror component you might be using.
     
    nirvanajie, mischa2k and hopeful like this.
  20. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Easiest way to upgrade:
    1. drag all networked prefabs into the scene
    2. select them all
    3. in Inspector debug mode: drag in the new NetworkIdentity/NetworkProximityChecker scripts into the components that are now missing them.
    4. Apply each prefab and delete from scene again.

    Shouldn't take longer than a minute or two.
     
  21. Roamer79

    Roamer79

    Joined:
    Oct 25, 2016
    Posts:
    45
    Cool that's great and thanks for clarifying. Will test this week!
     
  22. Serinx

    Serinx

    Joined:
    Mar 31, 2014
    Posts:
    788
    Does Mirror allow you to save persistent data to a database?
    Would I need to get uSurvival or something for an example of that?

    Thanks
     
  23. goldbug

    goldbug

    Joined:
    Oct 12, 2011
    Posts:
    767
    It allows it in the sense of that it does not get in the way :p. Mirror is a networking library for getting your server and client to talk to each other. Databases are out of scope. You can use any database you want in your server, and just use mirror to communicate with the clients.
     
  24. antsonthetree

    antsonthetree

    Joined:
    May 15, 2015
    Posts:
    102
    Lots of really cool changes for 2019! Thanks guys!
     
  25. goldbug

    goldbug

    Joined:
    Oct 12, 2011
    Posts:
    767
    Mirror (master) now works in unity 2018.3+. You no longer need the 2018 branch.
    If you are using 2018 branch, you can keep using it, it is not going anywhere.
    Hopefully we can merge some of the stuff in there to master.
     
    Zelek likes this.
  26. kingbaggot

    kingbaggot

    Joined:
    Jun 6, 2013
    Posts:
    51
    Hi - I'm using the Pong example to try learn how this works - running between two PCs on a Lan. It's working, which is great buuut.

    I'm trying to send a message from the client to the server (in the script on the player when a key is pressed) and I've read up on UNet how it's achieved there but I'm foxed.

    this is what I've got in the player script. The client.Send part is where it's erroring as it's not defined - but It doesn't seem to be defined in any of the examples I've looked at.

    Code (CSharp):
    1.  
    2.     NetworkClient client;
    3.  
    4.     public void OnGUI()
    5.     {
    6.         if (!isLocalPlayer) return;
    7.         if(Input.GetKeyDown(KeyCode.Space))
    8.         {
    9.             MyScoreMessage msg = new MyScoreMessage();
    10.             msg.score = 100;
    11.  
    12.             client.Send(MyMessageTypes.MSG_SCORE, msg);
    13.  
    14.         }
    15.        
    16.     }
    17.  
    18.     public class MyMessageTypes
    19.     {
    20.         public static short MSG_SCORE = 1005;
    21.     };
    22.  
    23.     public class MyScoreMessage : MessageBase
    24.     {
    25.         public int score;
    26.     }
    27.  
    28.  
    and this is what I've got on a script in the scene which extends NetworkBehaviour.

    Code (CSharp):
    1.  [ServerCallback] // only call this on server
    2.     public void ReceiveSomethingOnServer(NetworkMessage netMsg)
    3.     {
    4.         //server does stuff here
    5.     }
    But as it's erroring in the player script, nothing is received here. I'd looking into using the [command] functionality - but I can't find an example it it that I can get working either. Any help would be mucho appreciated.
     
  27. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Read the manual - the part about Cmds.
    Also read the old UNET manual.
    We'll also add more examples later to make this more obvious.
     
    kingbaggot likes this.
  28. kingbaggot

    kingbaggot

    Joined:
    Jun 6, 2013
    Posts:
    51
    this this the manual right ? Just checking !
    https://vis2k.github.io/Mirror/

    I'll go through the UNet Manual & Some examples too. Thanks for putting all this together :)
     
  29. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Yes
     
    kingbaggot likes this.
  30. goldbug

    goldbug

    Joined:
    Oct 12, 2011
    Posts:
    767
    @kingbaggot it is a lot simpler than that:

    Code (CSharp):
    1.  
    2.     public void OnGUI()
    3.     {
    4.         if (!isLocalPlayer) return;
    5.         if(Input.GetKeyDown(KeyCode.Space))
    6.         {
    7.             // this sends a message to the server.   It does not wait for the server to execute it
    8.             CmdSendScore(100);
    9.         }
    10.     }
    11.  
    12.     [Command]
    13.     public CmdSendScore(int score)
    14.     {
    15.           // this gets executed in the server,  with the score the client passed us
    16.     }
    17. }
    18.  
     
    kingbaggot likes this.
  31. Arganth

    Arganth

    Joined:
    Jul 31, 2015
    Posts:
    277
    Just to clarify:
    I would like to use Mirror potential Coop-Play with Peer-to-Peer Networking (not concerned with cheating at all)
    So would it be possible that
    - one person hosts the game using the "serverportion" of mirror
    - the other are able to find joinable games and connect?

    Or i am wrong?
     
  32. antsonthetree

    antsonthetree

    Joined:
    May 15, 2015
    Posts:
    102
    I just updated from a previous Mirror to the latest source asset version and I'm running into a problem. I've reassigned all my network identities and transforms, and when I run my app in host mode my little tank runs around fine and is replicated to the clients. However none of my clients can move at all. Their transforms are not getting updated locally or on the server. The main difference between my code and your example in the project is that I am spawning my tanks using NetworkServer.SpawnWithClientAuthority() - which I have to do because players can switch tanks at runtime etc. This function is coming back with a true return so it seems to be successful. Any thoughts on this?

    EDIT- This only seems to be affecting the transform updates. The clients can still fire their bullets and that is replicated to the server and other clients. They just cannot move.

    Also - my tanks have a Rigidbody component on them and I think that may be related to the issue. I noticed in your NetworkTransformBase you mention that FixedUpdate handles the rigidbody movement and rotation - however there is no FixedUpdate implemented in NetworkTransformBase or NetworkTransform. Maybe somethings missing?
     
    Last edited: Jan 15, 2019
  33. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Peer-to-peer is a dead end. Just let people host the server, they can do that on their own computer too (port forwarding).
     
    Arganth likes this.
  34. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Take a look at the 'move' example from the current version on github. It uses local authority too, and it works there.
    If you think you found a bug then please open an issue on github and tell us how to reproduce it.
     
  35. antsonthetree

    antsonthetree

    Joined:
    May 15, 2015
    Posts:
    102
    You can repro this with the Move example that is included. Just modify it to create a new controllable object via the SpawnWithClientAuthority function. Doesn't really matter what it is. You will notice that this works perfectly on the host, but the clients are unable to move. The problem seems to be with the SpawnWithClientAuthority function.
     
  36. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,980
    Does Mirror work on the HoloLens platform?
     
  37. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Try it!
     
    MadeFromPolygons likes this.
  38. antsonthetree

    antsonthetree

    Joined:
    May 15, 2015
    Posts:
    102
    Attached is an updated version of your Movement example that demonstrates the defect. Basically the new NetworkTransform does not work when it is created via the SpawnWithClientAuthority. Run the example and attach a client. Note the client's pawn does not update, but the server pawn does.
     

    Attached Files:

  39. antsonthetree

    antsonthetree

    Joined:
    May 15, 2015
    Posts:
    102
    I've debugged this and I have it working now for both player and non-player objects. Note that SpawnWithClientAuthority creates non-player objects.

    The lines you need to change are in NetworkTransformBase.cs.
    Line 328 should be
    if (!isServer && hasAuthority)
    The additional check you have for connectionToServer will always fail for non player objects - see unet doc.

    Line 348 should be
    if (!(isLocalPlayer || hasAuthority))
    Note the || and not && - because non player objects with authority need love too.

    Thanks
     
    Last edited: Sep 14, 2021
  40. shiena

    shiena

    Joined:
    May 19, 2017
    Posts:
    44
    Line 323-328 in NetworkTransformBase.cs
    Code (CSharp):
    1. // no 'else if' since host mode would be both
    2. if (isClient)
    3. {
    4.     // send to server if we have local authority (and aren't the server)
    5.     // -> only if connectionToServer has been initialized yet too
    6.     if (!isServer && hasAuthority && connectionToServer != null)
    The second if statement is isClient and !isServer so it will not work in host mode.
    Why do we need !isServer here?

    I think that together with the plan of @antsonthetree should be the following code.
    Line 328
    Code (CSharp):
    1.     if (hasAuthority)
     
  41. MrG

    MrG

    Joined:
    Oct 6, 2012
    Posts:
    368
    Either of you can make an edit on GitHub and submit a PR with a good description of the change.
     
  42. Player7

    Player7

    Joined:
    Oct 21, 2015
    Posts:
    1,533
    Would be good to get Mirror working on 2019 as Unity have thrown out UNET support and left an aftermath of broken assets everywhere, while Mirror doesn't even support it as it has a ton of console errors... Also I know 2019.1a is still in alpha, but it's got less bugs in it than 2018.3 final so yeah please look into soon.

    And Unity can't be bothered to backport a lot fixes or package updates to 2018.2, so basically 2019.1a14 is a less broken version of 2018.3 that has more fixes so I have no interest in using that, and 2018.2 won't get the fixes and package updates that only work in 2019.1
     
  43. antsonthetree

    antsonthetree

    Joined:
    May 15, 2015
    Posts:
    102
    Looks like this issue is already being tracked on github here:
    https://github.com/vis2k/Mirror/issues/273
     
  44. VrTechEx

    VrTechEx

    Joined:
    Aug 4, 2013
    Posts:
    40
    How can you make an RPC Buffered in Mirror? I know it's UNET but any ideas to make it work? Thank you!
     
  45. BiosElement

    BiosElement

    Joined:
    Apr 29, 2013
    Posts:
    10
    I've spent three hours on this so I figure it's time to admit my shame and ask for help.

    I'm testing Mirror for possible replacement of Photon2 in a game where I need players to have access to numerous spawned items from different players, basically as a board game. With Photon2 I'm using a RequestOwnership system which isn't exactly ideal since ownership doesn't matter much but it seems to work. I attempted to hack in an ownership request system or just assign ownership via commands but ran into the usual UNET permissions preventing interacting with non-owned GameObjects. Note my token movement and interactions are handled by the tokens themselves, thus the need for permissions.

    I'd appreciate just a kick in the right direction and thanks for the awesome work with Mirror.
     
  46. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Stress Test Weekend


    Why do you want to make it buffered?

    Join our discord. Someone talked about assigning ownership yesterday, maybe this person can share the code. Also check out our issue tracker, there is a demo about ownership somewhere in an open bug report about networktransform.
     
    BiosElement likes this.
  47. Serinx

    Serinx

    Joined:
    Mar 31, 2014
    Posts:
    788
    Is the authoritative/server-only code in mirror completely hidden from the user? I want to call a backend API from the server-only code with functions such as "Add Item", "Record Win", "Record Loss" etc but I'm worried if the user can decompile the code and work out the functions they might be able to give themselves items and wins.

    Might be a silly question as I'm new to authoritative servers but I'd just like to work out the best way to keep it secure.

    Also, would Mirror be a good option for a smaller scale game with roughly 10 players in a match?
    Or is there another option which would be more fitting.

    Cheers
     
    Last edited: Jan 23, 2019
  48. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    If you use IL2CPP then user will only see assembly code. But it's not exactly hidden. Unity 2018.3 has a #if UNITY_SERVER flag that you can use to hide server code from the client.

    Yes, Mirror works for MMO scale projects, so larger ones are no problem either.
     
    Serinx likes this.
  49. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    News:
    The Road to 1000 CCU
    - 500 CCU WORST CASE @ 8 CPUs, 32GB RAM


    It's done. Finally, after 3 years of networking hell.

    1000 CCU in a real MMO with players spread across a huge map should be no problem for the server now.

    Big thanks to everyone who contributed to Mirror. It's been a long, difficult journey to get where we are from UNET's initial 20-50 CCU full of errors. Every small contribution, every bug fix and every discussion was needed to get us here. I also apologize for giving everyone such a hard time with pull requests, but I hope that it all makes sense now.

    An open 1000 CCU community test will follow after a few more optimizations and Mirror bug fixes. The new Telepathy version will be released after that test.

    Thanks everyone
     
  50. VrTechEx

    VrTechEx

    Joined:
    Aug 4, 2013
    Posts:
    40
    I want all commands and clientrpcs to be buffered when a new player connected into a game. Any ideas ?

    Thank you !