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

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:
    2
    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?