WebGL + JSLIB Plugin, using functions

I’ve written a .jslib file in my /Assets/Plugins/WebGL folder that I am successfully calling back and forth with my C# code (using [DllImport (“__Internal”)] in C# and SendMessage in JS). The only problem I’m running into is I have not found a way that I can add a function to my Javascript object that I can call from another JS function.

Ex.

var WebInteraction = {

	func1: function()
	{
	    console.log("do func1 stuff");	
            func3();
	},
	
	func2: function(str)
	{
	    console.log("do func2 stuff str=" + str);
            func3();
	},
	
	func3: function()
        {
            console.log("do func3 stuff");
        }
};

mergeInto(LibraryManager.library, WebInteraction);

Code similar to the above example is giving me a func3 doesn’t exist error. I also looked in the .js file that Unity outputs and confirmed func3 was not exported. I do see func1 and func2 as _func1() and _func2().

Is there a way to add functions into the jslib that are not intended to be called from the C# code?

I’ve also tried calling the function like WebInteraction.func3(), as well as moving the function out of the WebInteraction object, but both did not get the compiler to export the func3().

You need to add dependencies declaration so emscripten does not strip out func3:

func1__deps: ['func3'],
func2__deps: ['func3']

There’s a tool that can do this for you automatically:

When i use js plugin run by webgl with https. I got error:
“Uncaught ReferenceError: SocketSend is not defined”

Jslib:
var LibraryWebSockets = {
$webSocketInstances: ,

SocketCreate: function(url)
{
	var str = Pointer_stringify(url);
	var socket = {
		socket: new WebSocket(str),
		buffer: new Uint8Array(0),
		error: null,
		messages: []
	}

	socket.socket.binaryType = 'arraybuffer';

	socket.socket.onmessage = function (e) {
		// Todo: handle other data types?
		if (e.data instanceof Blob)
		{
			var reader = new FileReader();
			reader.addEventListener("loadend", function() {
				var array = new Uint8Array(reader.result);
				socket.messages.push(array);
			});
			reader.readAsArrayBuffer(e.data);
		}
		else if (e.data instanceof ArrayBuffer)
		{
			var array = new Uint8Array(e.data);
			socket.messages.push(array);
		}
	};

	socket.socket.onclose = function (e) {
		if (e.code != 1000)
		{
			if (e.reason != null && e.reason.length > 0)
				socket.error = e.reason;
			else
			{
				switch (e.code)
				{
					case 1001: 
						socket.error = "Endpoint going away.";
						break;
					case 1002: 
						socket.error = "Protocol error.";
						break;
					case 1003: 
						socket.error = "Unsupported message.";
						break;
					case 1005: 
						socket.error = "No status.";
						break;
					case 1006: 
						socket.error = "Abnormal disconnection.";
						break;
					case 1009: 
						socket.error = "Data frame too large.";
						break;
					default:
						socket.error = "Error "+e.code;
				}
			}
		}
	}
	var instance = webSocketInstances.push(socket) - 1;
	
	return instance;
},

SocketState: function (socketInstance)
{
	var socket = webSocketInstances[socketInstance];
	return socket.socket.readyState;
},

SocketError: function (socketInstance, ptr, bufsize)
{
 	var socket = webSocketInstances[socketInstance];
 	if (socket.error == null)
 		return 0;
    var str = socket.error.slice(0, Math.max(0, bufsize - 1));
    writeStringToMemory(str, ptr, false);
	return 1;
},

SocketSend: function (socketInstance, ptr, length)
{
	//alert('start SocketSend');
	//var socket = webSocketInstances[socketInstance];
	//socket.socket.send (HEAPU8.buffer.slice(ptr, ptr+length));
	var socket = webSocketInstances[socketInstance];
	if (socket.socket.readyState === 1) {
        socket.socket.send (HEAPU8.buffer.slice(ptr, ptr+length));
    }else{
		//var m_that = this;
        // optional: implement backoff for interval here
        setTimeout(function () {SocketSend(socketInstance, ptr, length);}, 1000);
	}
},

SocketRecvLength: function(socketInstance)
{
	var socket = webSocketInstances[socketInstance];
	if (socket.messages.length == 0)
		return 0;
	return socket.messages[0].length;
},

SocketRecv: function (socketInstance, ptr, length)
{
	var socket = webSocketInstances[socketInstance];
	if (socket.messages.length == 0)
		return 0;
	if (socket.messages[0].length > length)
		return 0;
	HEAPU8.set(socket.messages[0], ptr);
	socket.messages = socket.messages.slice(1);
},

SocketClose: function (socketInstance)
{
	var socket = webSocketInstances[socketInstance];
	socket.socket.close();
},

SocketAlert: function(msg){
	alert(msg);
}
};

autoAddDeps(LibraryWebSockets, '$webSocketInstances');
mergeInto(LibraryManager.library, LibraryWebSockets);

somebody tell me how to fix this, please…

" I also looked in the .js file that Unity outputs "

What is this file name? Although my functions are working fine. I tried to find my functions in UnityLoader.js but had no success.