WebRTC Networking for WebGL Clients with a Standalone Server

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: GitHub - RussellRuffolo/VoxelZombieWebGL

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

And the connection manager in the server:

(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.

8 Likes

Awesome, works on internet? or only localhost?

Hi would be great if you do a simple tutorial for this and keep that as a separate project.