简体   繁体   中英

Having trouble connecting Unity3d WebGL project build to database, works fine in editor

I used this code to connect my Unity project to my MySql database:

public void SetupSQLConnection()
{
    Debug.Log("Connection Function Started");
    if (connection == null)
    {
        Debug.Log("If connection == null");
        try
        {
            Debug.Log("Try block started");
            MySqlConnectionStringBuilder connBuilder = new MySqlConnectionStringBuilder();

            connBuilder.Server = "localhost";
            connBuilder.UserID = "root";
            connBuilder.Database = "therapygame";
            connBuilder.Password = "";
            connBuilder.OldGuids = true;

            Debug.Log("String set");
            connection = new MySqlConnection(connBuilder.ConnectionString);
            Debug.Log("new MySqlConnection");
            connection.Open();
            Debug.Log("connection");
        }
        catch (MySqlException ex)
        {
            Debug.LogError("MySQL Error: " + ex.ToString());
        }
    }
}

This works fine when running my project in the Unity3d editor. However, when I build the WebGL, it no longer connects to the database.

The error happens between the Debug.Logs of "new MySqlConnection" and "connection". In other words, it is failing at the connection.Open(); statement.

Here is the console log in Google Chrome:

SystemException: Thread creation failed.

Rethrow as TypeInitializationException: The type initializer for 'System.Threading.Timer.Scheduler' threw an exception.

Rethrow as TypeInitializationException: The type initializer for 'System.Threading.Timer' threw an exception.

Rethrow as TypeInitializationException: The type initializer for 'MySql.Data.MySqlClient.MySqlPoolManager' threw an exception.


(Filename: currently not available on il2cpp Line: -1)

And here is the source of that error (it is so large that I had to break it apart into sections)(see following section to know what to look for):

https://0bin.net/paste/-28c3BvLx+Nx+Q+L#ltyOWWb+IembwSAgNKzPCRwvI5MkPOrUfgOAs3i4R+f

https://0bin.net/paste/ZleyLDeOcgr3kj-e#OPVr3oIC8-mcO9mXB1pV/+ONcdnUB+3I1RCy37/NI+p

https://0bin.net/paste/+rbHWsKmXhIXM50A#PaAFLCeDJZzIQTWczYbDepmubFhSaeDWqzcB+ItUTnb

https://0bin.net/paste/VdNv2NA8K0--ZXNM#fomzOVZ9iNOJDHequiSAbuv6I3lxDXXL+E5WK6GPKZv

https://0bin.net/paste/zcDPoycv5lqbjRcA#h0WNQdMSU4VtdIbwzOByW5csu68FERPvEdOKJz9oUeA

Copy this and paste it into ctrl+f find:

function _JS_Log_Dump(ptr, type) {
var str = Pointer_stringify(ptr);
if (typeof dump == "function") dump(str);
switch (type) {
case 0:
case 1:
case 4:
console.error(str);
return;
case 2:
console.warn(str);
return;
case 3:
case 5:
console.log(str);
return;
default:
console.error("Unknown console message type!");
console.error(str);
}
}

the lines that are in question are

case 4:
console.error(str);

I have included System.Data.dll and MySql.Data.dll to my Visual Studio references, and also all the I18N files. I have also put those files in my built project file path.

Please tell me if you need additional information, I tried to include everything I thought was relevant.

Since WebGL builds boil down to javascript, you cannot use standard C# networking APIs in those builds (MySQL connector uses ADO.NET and is therefore, unfortunately, disqualified from WebGL builds). There is some good information on Networking with Unity WebGL Builds but here's the part relevant to your situation:

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.

Keep in mind the "new Unity Networking features" it mentions are part of UNet, which is being deprecated . EDIT: It looks like the entire NetworkTransport API is gone.


Hopefully you can find a solution while digging through those articles. I haven't tried anything with UNet, WebSockets, or WebRTC but personally, to solve this issue in my own WebGL projects, I use UnityWebRequest to send POST requests to a server I'm hosting on AWS that's running a LAMP stack. My php pages process the requests and the MySQL database is sitting there on the same server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM