Search Unity

Showcase WebRTC Networking for WebGL Clients with a Standalone Server

Discussion in 'Multiplayer' started by Risky_Crash, May 24, 2022.

  1. Risky_Crash

    Risky_Crash

    Joined:
    May 26, 2020
    Posts:
    3
    Hey everyone, I recently got WebRTC networking working for a browser based game I made.

    I achieved this using the WebRTC PeerConnection's DataChannel to send bit-packed strings representing client inputs and player states between the Unity WebGL client and the standalone server. The client uses Javascript WebRTC objects accessed through a .jslib plugin while the server uses the preview Unity WebRTC package.

    This solution gives access to UDP-like networking for WebGL games. This means messages can be sent continuously without checking for order or if they arrived. My work was largely based on this tutorial: https://www.marksort.com/udp-like-networking-in-the-browser/

    It was a challenging process as not everything worked out-of-the-box. Notably the IceCandidate generated by the Unity WebRTC package had to be manually reserialized to match the form the JavaScript side was expecting.

    My code is publicly available here: https://github.com/RussellRuffolo/VoxelZombieWebGL

    With the most relevant files being the .jslib in the client:

    https://github.com/RussellRuffolo/V...ZombieClient/Assets/Plugins/UnityWebRtc.jslib

    And the connection manager in the server:

    https://github.com/RussellRuffolo/V...ombieServer/Assets/ClientConnectionManager.cs
    (The connection manager is using a simple HTTPListener in my code because it is behind an NGINX reverse-proxy that is enforcing HTTPS)

    I'm planning to do a more in depth write up of my methods and I'd love to hear what people are curious about or want explained.
     
  2. unity_3451455826C011708283

    unity_3451455826C011708283

    Joined:
    Mar 12, 2022
    Posts:
    1
    Awesome, works on internet? or only localhost?
     
  3. scareface124

    scareface124

    Joined:
    Aug 19, 2017
    Posts:
    5
    Hi would be great if you do a simple tutorial for this and keep that as a separate project.