Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Question Engine for Browser Multiplayer Game using WebGl

Discussion in 'Multiplayer' started by richardbendler, Nov 10, 2020.

  1. richardbendler

    richardbendler

    Joined:
    Nov 10, 2020
    Posts:
    1
    Hello everyone, im currently working on a multiplayer browser game, which i want to host on my linux server. After looking how to implement the multiplayer, i decided on the mirror API, but all the tutorials i could find show that the game will be hosted by a player himself. Is it possible with mirror to have the serverlogic run on my server after i export to WebGL? Otherwise what are some good free alternatives to develop a browser multiplayer game, which i can host on my server? I already decided against Photon bc of the 20 player limit and against UNet because its deprecated soon
     
  2. BetaMark

    BetaMark

    Joined:
    Sep 27, 2014
    Posts:
    208
    I am working on similar setup for my current project.

    Long story, short -- yes, you can totally run a "headless" Unity process as a Mirror "HOST" on your Linux box. Once you don that, you can can build WebGL game "clients" which will access your Linux server / Mirror HOST.

    For your host / server you would just go into to your Unity build settings and do the following:
    * set target platform to "Linux" (assuming you have that installed on your build machine)
    * select "server build" (this removes the GUI from trying to pop up on your server)
    * click the "Build" button
    * create a folder for your Linux build
    * pick a name for your build binary
    * After the build completes, move that entire folder over to your Linux box
    * set up the binary to execute with
    chmod 755 ./your_game_name
    for the binary
    * run the build via a command which looks something like
    ./your_game_name


    Assuming that your game is setup like mine is and the game requires the HUD GUI to start the hosting process, then the next step is to write some code wrapped checks for
    #ifdef UNITY_SERVER
    or
    UNITY_CLIENT
    on your host's first "scene" to recognize that it is a server build and to automagically setup and start listening for connections as only a HOST.

    Full disclaimer, as of writing this, I haven't done the final step in this process for my project yet, so I might update later if I learn that it doesn't work as I expected. Also, I am not an expert on this process yet, but let me know if you have any other questions and I'll try to help where I can.

    (edit: replaced backtick code comments with ICODE blocks)
     
    Last edited: Nov 10, 2020
    Cranom and luke-unity like this.
  3. luke-unity

    luke-unity

    Unity Technologies

    Joined:
    Sep 30, 2020
    Posts:
    306
    Yuju_dev, Joe-Censored and BetaMark like this.
  4. BetaMark

    BetaMark

    Joined:
    Sep 27, 2014
    Posts:
    208
    Verified that the
    #ifdef UNITY_SERVER
    works like I expected it to -- that code only fires if you have compiled with the "Server Build" checkbox (assuming you are in Unity 2020 -- I think it was called Headless or something similar in previous versions).

    So my new flow for my project will be:
    * create a new empty startup scene
    * place a new startup scene as scene 0 in the "scenes to build"
    * on a startup scene, have one game object with a single script which will check for UNITY_SERVER and fire up my Mirror HOST behaviors without waiting for user input
    * if this is not a UNITY_SERVER, then move onto the "main menu" scene (or whatever your game client would normally start with)

    This way I can compile for webgl clients and the dedicated server just by switching the target platform and check the "server build" in build settings. If I discover any other optimizations for this process, I'll send them your way.
     
    Yuju_dev and Joe-Censored like this.
  5. luke-unity

    luke-unity

    Unity Technologies

    Joined:
    Sep 30, 2020
    Posts:
    306
    I like to write small tools to create the builds. Makes it very easy to build the server for a different platform and run a different scene in each of them. Something like this:
    Code (CSharp):
    1. #if UNITY_EDITOR
    2. public static class BuildTool
    3. {
    4.     [MenuItem("Tools/Build/Build Client")]
    5.     public static void BuildClient()
    6.     {
    7.         var bpo = new BuildPlayerOptions
    8.         {
    9.             scenes = new[]{"Assets/Scenes/MainClient.unity"},
    10.             locationPathName = "Build/Client/",
    11.             options = BuildOptions.None,
    12.             target = BuildTarget.WebGL,
    13.         };
    14.         BuildPipeline.BuildPlayer(bpo);
    15.     }
    16.  
    17.     [MenuItem("Tools/Build/Build Server")]
    18.     public static void BuildServer()
    19.     {
    20.         var bpo = new BuildPlayerOptions
    21.         {
    22.             scenes = new[] { "Assets/Scenes/MainServer.unity" },
    23.             locationPathName = "Build/Server/MyServer.x86_64",
    24.             options = BuildOptions.EnableHeadlessMode,
    25.             target = BuildTarget.StandaloneLinux64,
    26.         };
    27.         BuildPipeline.BuildPlayer(bpo);
    28.     }
    29. }
    30. #endif
    This creates a Tools menu item which you can use to create builds. You could also add an option to build both with just one click by calling both BuildServer and BuildClient. (BuildOptions.EnableHeadlessMode is the same thing as ticking the Server Build box in the build settings)
     
    Last edited: Nov 11, 2020
    Andreas36, Joe-Censored and BetaMark like this.
  6. Noamxrx

    Noamxrx

    Joined:
    Jul 25, 2018
    Posts:
    39
    Mirror is now finally supports SSL for WebGL builds, highly recommending using it.
     
    Brodus, BetaMark and luke-unity like this.
  7. atomikm

    atomikm

    Joined:
    Feb 8, 2020
    Posts:
    12
    I've set up a dedicated Mirror server in an AWS EC2 instance and have it working with many WebGL clients connecting to it. All the players are using NetworkTransform so that their movement is synced. However, I am running into MAJOR issues with latency. There's a ton of lag when connecting even 2 players to the server. I'm trying to figure out if this is an issue related to the SimpleWebTransport websocket protocol that Mirror uses to work with WebGL. Is it just a very slow transport compared to Telepathy/KCP? The latency is so terrible I'm going to have to re-write all of my code to Photon PUN instead. I really wanted to use Mirror because I wanted a server-authoritative game, but the lag is non-playable.
     
  8. Petter_H

    Petter_H

    Joined:
    Jul 25, 2020
    Posts:
    12
    First: Websockets are built on TCP, and thus are "reliable". What this means is that all data will be delivered, in order, whether it is old (newer information is available) or not. That in turn can be terrible for latency sensitive games, and what's worse is there's no way around it (on WebGL). Now, how various networking solutions' higher layers handle the fact they're running on a reliable protocol differ to various extents. For instance, obviously the more data transmitted the greater chance of running into issues. However, even a very streamlined networking solution running on websockets cannot break out of the constraints TCP places.

    Second: Not all types of AWS EC2 instances are equal - you should probably describe your setup. There are some pretty terrible cheap AWS EC2 instances completely unsuitable for games. Either because of poor network capacity or simply that CPU is optimized for short "bursty" tasks, rather than the long running tasks game servers constitute.
     
    BetaMark likes this.
  9. BetaMark

    BetaMark

    Joined:
    Sep 27, 2014
    Posts:
    208
    Profiling a multiplayer game is super tricky -- so I'd recommend you get it working really smooth in editor FIRST, and then switch over to dealing with the headaches of running it in WebGL.

    Try using something like ParrelSync to setup and run two copies of your game in editor with the profiling window up. Connect both of your editor instances to your dedicated server and post some screenshots of their profilers -- as well as screenshots from your ec2 instances "Monitoring" tab for all the metrics covering the same period as your testing.

    Then we can help you pinpoint where the problem is. After you know where the problem is, then you can begin to work to resolve it.
     
  10. ab15

    ab15

    Joined:
    Apr 6, 2015
    Posts:
    5
    @atomikm Any update on your project,
    Also what is the max amount of players you got connected per client?
     
  11. atomikm

    atomikm

    Joined:
    Feb 8, 2020
    Posts:
    12
    Yes, so the issue had to do with the SimpleWebTransport protocol that Mirror uses for WebGL. It is very slow when using a website that requires SSL.

    Two solutions:

    1. Use the Playflow cloud server (recommended) - https://playflowcloud.com/
    You can upload your Mirror server builds super easy to this cloud solution. The developer behind this solution has setup a reverse proxy server and it handles the SSL signing for you. Very low latency with SimpleWebTransport. And it also handles server scaling/AWS EC2 management etc. Highly recommend - this is what I'm using. I've connected 8 players with low latency

    2. Use AsioTransport - I didn't get too far into doing this so I'm not sure if it works fully. But it is an alternative to SimpleWebTransport and is much more performant. You can find the developer of it in the Mirror discord
     
    ab15 likes this.
  12. tushar_unity775

    tushar_unity775

    Joined:
    Jun 17, 2022
    Posts:
    20
    How can i create a simple 3d metaverse webiste in unity and host it on web using mirror
     
  13. tushar_unity775

    tushar_unity775

    Joined:
    Jun 17, 2022
    Posts:
    20
    CyborgGames likes this.
  14. Cranom

    Cranom

    Joined:
    Jan 31, 2018
    Posts:
    26
    There is nothing simple about creating an online scam with corporate mumbojumbo and technobabble buzzwords !
     
    Migmac likes this.
  15. Noamxrx

    Noamxrx

    Joined:
    Jul 25, 2018
    Posts:
    39
    3D metaverse website?
    You can build a world that will act as your "Website".
    The main domain will redirect to your metaverse game...

    But it's not really effective for main websites since Webgl is very slow at startup.
    You better use threeJS which is a much better tool for 3D websites and also supports mobile.