Search Unity

Unity multiplayer in WebGL

Discussion in 'Multiplayer' started by maurod_unity, Aug 30, 2022.

  1. maurod_unity

    maurod_unity

    Joined:
    Aug 30, 2022
    Posts:
    3
    Hi!
    Sorry for the thread, I'm sure it's something basic, but I can't understand the topic well. I have found some information and they say different things, it is not clear to me.

    I'm making a fight game, only two players. It has to be in WebGL. One player could invite another to play a match through a link. The idea is that the player who invites is the host of the match and acts as a server.

    For example, the documentation of netcode says:
    Sin título.png

    So, I would appreciate some information on the subject, what could be my options to approach the problem, a point where to start, frameworks that can host in WebGL, etc.

    Thanks in advance!
     
  2. r31o

    r31o

    Joined:
    Jul 29, 2021
    Posts:
    460
    Netcode cant support WebGL because WebGL uses WebSockets.

    Transports are normally some kind of "wrapper" for something that can be used to connect over the internet (like the socket class or Steam Relay), so the networking solution can use them.
    The advantage of doing it like this is that you can make all transports you want , and the code outside the transport doesnt have to change.

    I guess netcode will use the Socket Class by default, which doesnt work on WebGL, so you will have to make a WebSockets transport or download one.

    As for the hosting, the server itself cant be a WebGL build, so you will need a Azure, AWS or Google Cloud server, and when you want to host a game the player will send a rpc saying "hey, I want to host a lobby with this name", and the server will handle it. Doing it that way has a lot of advantages:

    -No port forwarding.
    -Doesnt need the hosts computer ip.
    -The server is full authoritative (You can give some authority to the host to do kick players), so the host cant cheat.
    -Since the real game is not running on any client computer, but in a powerfull server, the preformance wont be a problem.
    -Clients dont depeend on the host, and no host migration is needed, since the real host is a server on the cloud.
    -Clients dont depend on how good is the internet of the host.

    But all of this has a problem:
    -Costs money.
     
  3. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Not sure which library 'Netcode' is, I assume mlapi?
    As far as I remember, they use Transports as well.
    And there are a few WebGL transports available, e.g. this one from james:
    https://github.com/James-Frowen/SimpleWebTransport

    You could probably convert this to any library which uses the Transport concept rather easily.
    It's usually just Connect/Disconnect/Send/etc.
     
  4. maurod_unity

    maurod_unity

    Joined:
    Aug 30, 2022
    Posts:
    3
    Thanks for your answers!

    Now, I'm using netcode for gameobjects (https://docs-multiplayer.unity3d.com/netcode/current/about/index.html), with a transport that supports WebGL (https://github.com/Unity-Technologi...rts/com.community.netcode.transport.websocket).

    I'm trying to connect a WebGL build as a client to a windows build as a server, I can't figure out how to do it.
    I have no problem doing the tutorials of the netcode for gameobjects. However, when i change to the WebSocket transport and a build webgl client, I can't replicate the tutorials.

    Any help would be really appreciated!
     
  5. thamrick

    thamrick

    Joined:
    Sep 19, 2022
    Posts:
    6
    @maurod_unity any luck? I'm attempting the same thing. I can build native with that websockets transport but not webgl
     
  6. r31o

    r31o

    Joined:
    Jul 29, 2021
    Posts:
    460
    WebGL is a graphics library, the websockets transport is what you want.
     
  7. thamrick

    thamrick

    Joined:
    Sep 19, 2022
    Posts:
    6
    sorry, I wasn't very clear. The NetworkManager (netcode for gameobjects) with a websocket transport doesn't work in a webGL client build, even though the same setup works in a windows build
     
  8. CreativeChris

    CreativeChris

    Unity Technologies

    Joined:
    Jun 7, 2010
    Posts:
    457
    Are you using your WebGL build client as a Host or Server? If so, don't, try using the Windows or Mobile builds as your server or host.
     
  9. thamrick

    thamrick

    Joined:
    Sep 19, 2022
    Posts:
    6
    I'm only using the webgl build as a client.
     
  10. maurod_unity

    maurod_unity

    Joined:
    Aug 30, 2022
    Posts:
    3
    @thamrick No luck :(
    Same problem, couldn't solve it. So, I moved to Mirror (https://mirror-networking.com/) and it worked!
    I could even setup a dedicated server (windows) and connect WebGL clients to it!
     
  11. thamrick

    thamrick

    Joined:
    Sep 19, 2022
    Posts:
    6
    Awesome! Thank you for the recommendation! That's looks like it's just what I needed!