Bayou
Bayou copied to clipboard
Javascript module fails to connect with Unity 6 and "Target WebAssembly 2023" enabled
When enabling Target WebAssembly 2023 and WebAssembly.Table + Enable BigInt player options, it seems to stop Unity from creating the (deprecated) dynCall function within the JS environment which causes Bayuo to become inoperable in the browser.
I have replaced my SimpleWeb.jslib with the following and it seems to fix things:
// this will create a global object
const SimpleWeb = {
webSockets: [],
next: 1,
GetWebSocket: function (index) {
return SimpleWeb.webSockets[index];
},
AddNextSocket: function (webSocket) {
var index = SimpleWeb.next;
SimpleWeb.next++;
SimpleWeb.webSockets[index] = webSocket;
return index;
},
RemoveSocket: function (index) {
SimpleWeb.webSockets[index] = undefined;
},
};
function IsConnected(index) {
var webSocket = SimpleWeb.GetWebSocket(index);
if (webSocket) {
return webSocket.readyState === webSocket.OPEN;
} else {
return false;
}
}
function Connect(addressPtr, openCallbackPtr, closeCallBackPtr, messageCallbackPtr, errorCallbackPtr) {
const address = UTF8ToString(addressPtr);
console.log("Connecting to " + address);
// Prepare JS call wrappers for the native function pointers
const openCb = {{{ makeDynCall('vi', 'openCallbackPtr') }}};
const closeCb = {{{ makeDynCall('vi', 'closeCallBackPtr') }}};
const msgCb = {{{ makeDynCall('viii', 'messageCallbackPtr') }}};
const errCb = {{{ makeDynCall('vi', 'errorCallbackPtr') }}};
// Create webSocket connection.
const webSocket = new WebSocket(address);
webSocket.binaryType = 'arraybuffer';
const index = SimpleWeb.AddNextSocket(webSocket);
// Connection opened
webSocket.addEventListener('open', function () {
console.log("Connected to " + address);
openCb(index);
});
// Connection closed
webSocket.addEventListener('close', function () {
console.log("Disconnected from " + address);
closeCb(index);
});
// Listen for messages
webSocket.addEventListener('message', function (event) {
if (event.data instanceof ArrayBuffer) {
var array = new Uint8Array(event.data);
var arrayLength = array.length;
var bufferPtr = _malloc(arrayLength);
var dataBuffer = new Uint8Array(HEAPU8.buffer, bufferPtr, arrayLength);
dataBuffer.set(array);
msgCb(index, bufferPtr, arrayLength);
_free(bufferPtr);
} else {
console.error("message type not supported");
}
});
// Error handling
webSocket.addEventListener('error', function (event) {
console.error('Socket Error', event);
errCb(index);
});
return index;
}
function Disconnect(index) {
var webSocket = SimpleWeb.GetWebSocket(index);
if (webSocket) {
webSocket.close(1000, "Disconnect Called by Mirror");
}
SimpleWeb.RemoveSocket(index);
}
function Send(index, arrayPtr, offset, length) {
var webSocket = SimpleWeb.GetWebSocket(index);
if (webSocket) {
const start = arrayPtr + offset;
const end = start + length;
const data = HEAPU8.buffer.slice(start, end);
webSocket.send(data);
return true;
}
return false;
}
const SimpleWebLib = {
$SimpleWeb: SimpleWeb,
IsConnected,
Connect,
Disconnect,
Send
};
autoAddDeps(SimpleWebLib, '$SimpleWeb');
mergeInto(LibraryManager.library, SimpleWebLib);
Is this still an issue in 4.1.5?