Hello again. Thanks for all your help on this. Weâre still struggling to figure out what we might be doing wrong. The code works perfectly in the Unity Editor with the build target set to PC / Mac / Linux stand-alone, but not with WebGL as the build target (both as an actual build and in the editor).
What we see when we run with WebGL as the build target is the following:
- Two new records are created in the DB but are not updated or deleted.
- There is a NullReferenceException thrown on line 108 of UniversalWebClient.cs
- None of the Debug.Log($"***** âŚ") statements appear in the console (although we do see several WebRequest entries in the log).
Hereâs the final test code based on what you posted with just a few minor adjustments to the WebGL portions. (Also, to make the compiler happy, we had to put the MoralisLiveQueryController.cs and MoralisSubscriptionQuery.cs in the WebGL namespace):
Thank you!
using Assets.Scripts;
using MoralisWeb3ApiSdk;
using System;
using UnityEngine;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
#if UNITY_WEBGL
using Cysharp.Threading.Tasks;
using Moralis.WebGL;
using Moralis.WebGL.Platform.Objects;
using Moralis.WebGL.Platform.Queries;
using Moralis.WebGL.Platform.Queries.Live;
#else
using Moralis;
using Moralis.Platform.Objects;
using Moralis.Platform.Queries;
using Moralis.Platform.Queries.Live;
#endif
public class LiveQueryTest
{
#if UNITY_WEBGL
public async void TestLiveQuery()
{
var moralisQueryPlayerData = await MoralisInterface.GetClient().Query<PlayerData>();
// Setup subscription
setupLiveQuerySubscription(moralisQueryPlayerData);
Thread.Sleep(2000);
#else
public async void TestLiveQuery()
{
var moralisQueryPlayerData = MoralisInterface.GetClient().Query<PlayerData>();
// Setup subscription
setupLiveQuerySubscription(moralisQueryPlayerData);
Thread.Sleep(2000);
#endif
System.Random rand = new System.Random((int)DateTime.Now.Ticks);
int x = rand.Next(25) + 3;
PlayerData p1 = MoralisInterface.GetClient().Create<PlayerData>();
p1.Name = GetTestName();
p1.TokenCount = x;
await p1.SaveAsync();
x = rand.Next(25) + 3;
PlayerData p2 = MoralisInterface.GetClient().Create<PlayerData>();
p2.Name = GetTestName();
p2.TokenCount = x;
await p2.SaveAsync();
// Get the records created
IEnumerable<PlayerData> recs = await moralisQueryPlayerData.FindAsync();
// Update data
foreach (PlayerData pd in recs)
{
x = rand.Next(25) + 3;
pd.TokenCount = x;
await pd.SaveAsync();
}
// Delete data
foreach (PlayerData pd in recs)
{
await pd.DeleteAsync();
}
}
private void setupLiveQuerySubscription(MoralisQuery<PlayerData> playerData)
{
MoralisLiveQueryCallbacks<PlayerData> moralisLiveQueryCallbacks = new MoralisLiveQueryCallbacks<PlayerData>();
moralisLiveQueryCallbacks.OnConnectedEvent += (() => { Debug.Log("Connection Established."); });
moralisLiveQueryCallbacks.OnSubscribedEvent += ((requestId) => { Debug.Log($"Subscription {requestId} created."); });
moralisLiveQueryCallbacks.OnUnsubscribedEvent += ((requestId) => { Debug.Log($"Unsubscribed from {requestId}."); });
moralisLiveQueryCallbacks.OnErrorEvent += ((ErrorMessage em) =>
{
Debug.Log($"***** ERROR: code: {em.code}, msg: {em.error}, requestId: {em.requestId}");
});
moralisLiveQueryCallbacks.OnCreateEvent += ((item, requestId) =>
{
Debug.Log($"***** Created ");
});
moralisLiveQueryCallbacks.OnUpdateEvent += ((item, requestId) =>
{
Debug.Log($"***** Updated ");
});
moralisLiveQueryCallbacks.OnDeleteEvent += ((item, requestId) =>
{
Debug.Log($"***** Deleted");
});
moralisLiveQueryCallbacks.OnGeneralMessageEvent += ((text) =>
{
Debug.Log($"***** Websocket message: {text}");
});
MoralisLiveQueryController.AddSubscription<PlayerData>("PlayerData", playerData, moralisLiveQueryCallbacks);
}
private static string GetTestName()
{
string[] names = { "Clem the Great", "Sion the Bold", "Bob", "D@ve", "Oogmar the Deft", "Alesdair the Blessed", "Seviel the Mighty", "Master Adept Xactant", "Semaphore the Beautiful", "Gamemaster Nexnang" };
System.Random rand = new System.Random((int)DateTime.Now.Ticks);
int x = rand.Next(names.Length);
x = rand.Next(names.Length);
return names[x];
}
}
** Software Versions Used
Unity 2020.3.24f1
Moralis Ethereum Unity Boilerplate v1.0.8