2
\$\begingroup\$

There are plenty of good Websocket APIs for C#, but I run into trouble when targeting Unity WebGL due to this restriction described in the Unity documentation:

No direct socket access

Due to security implications, JavaScript code does not have direct access to IP Sockets to implement network connectivity. As a result, the .NET networking classes (ie, everything in the System.Net namespace, particularly System.Net.Sockets) are non-functional in WebGL. The same applies to Unity’s old UnityEngine.Network* classes, which are not available when building for WebGL.

If you need to use Networking in WebGL, you currently have the options to use the WWW or UnityWebRequest classes in Unity or the new Unity Networking features which support WebGL, or to implement your own networking using WebSockets or WebRTC in JavaScript.

How can I work around this restriction for networking in my Unity WebGL game?

\$\endgroup\$
3
  • 1
    \$\begingroup\$ How have you tried implementing the work-arounds described in the documentation page you're citing? \$\endgroup\$
    – DMGregory
    Nov 5, 2019 at 0:05
  • \$\begingroup\$ I didn't want to use the workaround things because they are based on requests and not the Websocket Protocoll im currently using on my Server. And creating the Websocket Protocoll with all its functions would be too hard. \$\endgroup\$
    – Jonas Re
    Nov 5, 2019 at 11:19
  • 1
    \$\begingroup\$ The answer you've accepted is one of the workarounds described in the documentation. See the section "Using Web Sockets or WebRTC from JavaScript" \$\endgroup\$
    – DMGregory
    Nov 5, 2019 at 18:03

1 Answer 1

5
\$\begingroup\$

As the documentation says, The Unity scripting API does not expose direct WebSocket access itself. But you can access the native web browser API by calling JavaScript from Unity C# scripts.

  1. Create a JavaScript file which defines the functions you need and implements them by calling native browser functionality.
  2. Give it the file extension .jslib
  3. Put it into the directiory Assets/Plugins of your Unity project
  4. Import them into a C# script by defining a class-level method with the prefix [DllImport("__Internal")] private static extern.

Example for a websocket.jslib:

mergeInto(LibraryManager.library, {

  WebSocketInit: function(url, protocol) {
         this.socket = new WebSocket(url, protocol);
  },
  WebSocketSend: function(message) {
         this.socket.send(message);
  }

});

Example of a C# script using those methods:

using UnityEngine;
using System.Runtime.InteropServices;

public class WebSocketSender : MonoBehaviour {

    [DllImport("__Internal")]
    private static extern void WebSocketInit(string url, string protocol);

    [DllImport("__Internal")]
    private static extern void WebSocketSend(string message);

    void Start() {
        WebSocketInit("wss://example.com/gameserver", "mygameprotocol");
    }

    void Update() {
        if (Input.GetKeyDown(KeyCode.Space)) {
                WebSocketSend("Pew!Pew!Pew!");
        }
    }
}

But please keep in mind that WebSockets are not native TCP/IP sockets. The protocol adds some framing and masking to the messages which makes them unable to implement any raw TCP/IP protocols. So you won't be able to use WebSockets to interface with any server which doesn't implement the WebService protocol.

\$\endgroup\$
1
  • \$\begingroup\$ Thanky you for your very detailed Help. That's excatly what is was looking for. I'm migrating from a Javascript Html FrontEbd to Unity so this method is perfect cause i can copy the Websocket Stuff to this attempt. \$\endgroup\$
    – Jonas Re
    Nov 5, 2019 at 11:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .