Corpus
Corpus

Reputation: 31

After WebGL build a websocket client doesn't work and throws a JS exception

When I use WebGL build to build my project, websocket client doesn't work at all. I use WebSocketSharp library and Unity 6. I encounter a JS exception:

null function or function signature mismatch.

What does it mean and how to solve the problem?

The exception:

The JS exception

And the same error from the console:

what.framework.js:9 RuntimeError: null function or function signature mismatch
    at what.wasm:0x35d628
    at what.wasm:0x35dc8c
    at what.wasm:0x20168b
    at what.wasm:0x26e72c5
    at invoke_viiii (what.framework.js:9:434715)
    at what.wasm:0x35071e
    at what.wasm:0x351b86
    at what.wasm:0x26e724f
    at invoke_viii (what.framework.js:9:434082)
    at what.wasm:0x3541d7
    at what.wasm:0x35a1cf
    at what.wasm:0x1adc1f3
    at what.wasm:0x1c2ee24
    at what.wasm:0x1c88ecd
    at what.wasm:0x26e7214
    at invoke_iiii (what.framework.js:9:433932)
    at what.wasm:0x1c87ea7
    at what.wasm:0x132c4c
    at what.wasm:0x2648ad9
    at what.wasm:0x25fc955
    at what.wasm:0x25fc96a
    at what.wasm:0x236636b
    at what.wasm:0x24f9285
    at what.wasm:0x246d1b3
    at what.wasm:0x246d228
    at what.wasm:0x26bb9ea
    at what.wasm:0x26e7271
    at what.framework.js:9:24986
    at _JS_CallAsLongAsNoExceptionsSeen (what.framework.js:9:25000)
    at what.wasm:0x26b61de
    at what.wasm:0x26e7271
    at browserIterationFunc (what.framework.js:9:116203)
    at callUserCallback (what.framework.js:9:98565)
    at Object.runIter (what.framework.js:9:99932)
    at Browser_mainLoop_runner (what.framework.js:9:115729)

I've already tried to use different libraries but all the libraries I used throw the same exception. This library works correctly in Unity Editor at least.

Code:

using System;
using UnityEngine;
using WebSocketSharp;

public class ClientLogic : MonoBehaviour
{
    [SerializeField] private string _address = "ws://localhost:4649/app";

    private WebSocket client;
    private bool reconnect = false;

    private void Start()
    {
        client = new WebSocket(_address);
        client.OnOpen += OnConnect;
        client.OnClose += OnDisconnect;
        client.OnMessage += OnMessage;
        GlobalValues.OnApplicationClosed.AddListener(StopClient);
    }

    public bool IsConnected { get => client.IsAlive; }

    public void StartClient()
    {
        reconnect = true;
    }

    private void Update()
    {
        if (client == null)
            return;

        if (reconnect && !IsConnected)
            client.Connect();
    }

    private void OnConnect(object sender, EventArgs e)
    {
        Debug.Log($"Connected to Server");
    }

    private void OnDisconnect(object sender, EventArgs e)
    {
        Debug.Log($"Disconnected from Server");
    }

    public void Send(string message)
    {
        client.Send(message);
    }

    private void OnMessage(object sender, MessageEventArgs e)
    {
        /*my logic*/
    }

    private void StopClient()
    {
        reconnect = false;
        client?.Close();
    }
}

Upvotes: 0

Views: 30

Answers (0)

Related Questions