Search code examples
unity-game-enginewebsocketunity-webgl

Unity WebGL throws Error: "ReferenceError: Runtime is not defined"


I wanted to export my Unity project (Unity Version 2021.2) for WebGL, but I get this Error:

An error occurred running the Unity content on this page. See your browser JavaScript console for more info. The error was: ReferenceError: Runtime is not defined unityFramework/_WebSocketConnect/instance.ws.onopen@http://localhost:55444/Build/WEbGL.framework.js:3:67866

I am using this Websocket package (https://github.com/endel/NativeWebSocket) an everything is working fine in Unity or in a Windows Build. When i run the WebGL build it does connect with the websocket but then i get the Error. The Error message says more info is in my console but the console on F12 only repeats the error:

Uncaught ReferenceError: Runtime is not defined at WebSocket.instance.ws.onmessage (WEbGL.framework.js:3) instance.ws.onmessage @ WEbGL.framework.js:3


To give a minimal reproducable example i just created a empty 3D Core project with Unity 2021.2 and imported the package NativeWebSocket (I downloaded the File from GitHub and installed it manally:

Copy the sources from NativeWebSocket/Assets/WebSocket into your Assets directory

Then you have to make the fixes postet by kentakang on https://github.com/endel/NativeWebSocket/pull/54 otherwise the build will fail.

Then i made a new C# script with the code below (also from the Github page) and put it on the Camera in the Scene. I exported it for WebGL and got the mentioned Error.

This happens when one of the Methods websocket.OnOpen/OnError/OnClose/OnMessage is called, so you don´t even need a running websocket because then websocket.OnError is called and the WebGL Build throws the "Runtime is not defined" Error. Or if you have also the running Websocket server which is also included in the package you get the Error when websocket.OnOpen is called.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using NativeWebSocket;

public class Connection : MonoBehaviour
{
  WebSocket websocket;

  // Start is called before the first frame update
  async void Start()
  {
    websocket = new WebSocket("ws://localhost:2567");

    websocket.OnOpen += () =>
    {
      Debug.Log("Connection open!");
    };

    websocket.OnError += (e) =>
    {
      Debug.Log("Error! " + e);
    };

    websocket.OnClose += (e) =>
    {
      Debug.Log("Connection closed!");
    };

    websocket.OnMessage += (bytes) =>
    {
      Debug.Log("OnMessage!");
      Debug.Log(bytes);

      // getting the message as a string
      // var message = System.Text.Encoding.UTF8.GetString(bytes);
      // Debug.Log("OnMessage! " + message);
    };

    // Keep sending messages at every 0.3s
    InvokeRepeating("SendWebSocketMessage", 0.0f, 0.3f);

    // waiting for messages
    await websocket.Connect();
  }

  void Update()
  {
    #if !UNITY_WEBGL || UNITY_EDITOR
      websocket.DispatchMessageQueue();
    #endif
  }

  async void SendWebSocketMessage()
  {
    if (websocket.State == WebSocketState.Open)
    {
      // Sending bytes
      await websocket.Send(new byte[] { 10, 20, 30 });

      // Sending plain text
      await websocket.SendText("plain text message");
    }
  }

  private async void OnApplicationQuit()
  {
    await websocket.Close();
  }

}

Does someone know how to fix this Error? Help would be appreciated:)


Solution

  • It seams that in unity 2021.2 variable Runtime doesn't exist and can be replaced with Module['dynCall_*'].

    In webSocket.jslib change all Runtime.dynCall('*1', *2, [*3, *4]) for Module['dynCall_*1'](*2, *3, *4)

    Example instance.ws.onopen function in WebSocket.jslib:

    change Runtime.dynCall('vi', webSocketState.onOpen, [ instanceId ]);
    for
    Module['dynCall_vi'](webSocketState.onOpen, instanceId);