Skip to content

Commit 472cb00

Browse files
committed
fix: WebGL BackgroundWorker implementation & sample packages
1 parent ef4eab4 commit 472cb00

File tree

3 files changed

+22
-28
lines changed

3 files changed

+22
-28
lines changed

samples/unity-of-bugs/Packages/manifest.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"com.unity.modules.androidjni": "1.0.0",
1111
"com.unity.modules.audio": "1.0.0",
1212
"com.unity.modules.screencapture": "1.0.0",
13-
"com.unity.modules.ui": "1.0.0"
13+
"com.unity.modules.ui": "1.0.0",
14+
"com.unity.modules.unitywebrequest": "1.0.0"
1415
}
1516
}

samples/unity-of-bugs/Packages/packages-lock.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@
128128
"depth": 0,
129129
"source": "builtin",
130130
"dependencies": {}
131+
},
132+
"com.unity.modules.unitywebrequest": {
133+
"version": "1.0.0",
134+
"depth": 0,
135+
"source": "builtin",
136+
"dependencies": {}
131137
}
132138
}
133139
}

src/Sentry.Unity/WebGL/SentryWebGL.cs

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Sentry.Extensibility;
88
using Sentry.Infrastructure;
99
using Sentry.Internal;
10+
using Sentry.Internal.Http;
1011
using Sentry.Protocol.Envelopes;
1112
using UnityEngine;
1213
using UnityEngine.Networking;
@@ -28,6 +29,9 @@ public static void Configure(SentryUnityOptions options)
2829

2930
// Caching transport relies on a background thread
3031
options.CacheDirectoryPath = null;
32+
// Note: we need to use a custom background worker which actually doesn't work in the background
33+
// because Unity doesn't support async (multithreading) yet. This may change in the future so let's watch
34+
// https://docs.unity3d.com/2019.4/Documentation/ScriptReference/PlayerSettings.WebGL-threadsSupport.html
3135
options.BackgroundWorker = new WebBackgroundWorker(options, SentryMonoBehaviour.Instance);
3236

3337
// Still cant' find out what's using Threads so:
@@ -53,52 +57,35 @@ public WebBackgroundWorker(SentryUnityOptions options, SentryMonoBehaviour behav
5357
{
5458
_options = options;
5559
_behaviour = behaviour;
56-
// var composer = new SdkComposer(options);
57-
// HTTP transport is not compatible. Need to use Unity's one.
58-
// _transport = composer.CreateTransport();
5960
}
6061

6162
public bool EnqueueEnvelope(Envelope envelope)
6263
{
63-
// _transport.SendEnvelopeAsync(envelope, CancellationToken.None)
64-
// .ContinueWith(r => _options.DiagnosticLogger?.LogInfo("Result of envelope capture was: {0}", r.Status));
6564
_ = _behaviour.StartCoroutine(SendEnvelope(envelope));
6665
return true;
6766
}
6867

6968
private IEnumerator SendEnvelope(Envelope envelope)
7069
{
71-
var dsn = Dsn.Parse(_options.Dsn!);
72-
var authHeader =
73-
$"Sentry sentry_version={Sentry.Constants.ProtocolVersion}," +
74-
$"sentry_client={UnitySdkInfo.Name}/{UnitySdkInfo.Version}," +
75-
$"sentry_key={dsn.PublicKey}," +
76-
(dsn.SecretKey is { } secretKey ? $"sentry_secret={secretKey}," : null) +
77-
$"sentry_timestamp={_clock.GetUtcNow().ToUnixTimeSeconds()}";
78-
79-
var www = new UnityWebRequest(dsn.GetEnvelopeEndpointUri());
80-
www.method = "POST";
81-
www.SetRequestHeader("X-Sentry-Auth", authHeader);
70+
var builder = new HttpRequestBuilder(_options);
71+
var www = new UnityWebRequest();
72+
www.url = builder.GetEnvelopeEndpointUri().ToString();
73+
www.method = UnityWebRequest.kHttpVerbPOST;
74+
www.SetRequestHeader(builder.AuthHeaderName, builder.AuthHeader(_clock.GetUtcNow()));
75+
// TODO is it OK to call .Wait() here in webGL?
8276
var stream = new MemoryStream();
8377
envelope.SerializeAsync(stream, _options.DiagnosticLogger).Wait(TimeSpan.FromSeconds(2));
8478
stream.Flush();
8579
www.uploadHandler = new UploadHandlerRaw(stream.ToArray());
8680
www.downloadHandler = new DownloadHandlerBuffer();
8781
yield return www.SendWebRequest();
8882

89-
while (!www.isDone)
90-
{
91-
yield return null;
92-
}
93-
if (
94-
www.isNetworkError || www.isHttpError
95-
|| www.responseCode != 200)
83+
if (www.isNetworkError || www.isHttpError || www.responseCode != 200)
9684
{
97-
_options.DiagnosticLogger?.LogWarning("error sending request to sentry: {0}", www.error);
98-
}
99-
{
100-
_options.DiagnosticLogger?.LogDebug("Sentry sent back: {0}", www.downloadHandler.text);
85+
_options.DiagnosticLogger?.LogWarning("error sending request to Sentry: {0}", www.error);
10186
}
87+
88+
_options.DiagnosticLogger?.LogDebug("Sentry sent back: {0}", www.downloadHandler.text);
10289
}
10390

10491
public Task FlushAsync(TimeSpan timeout) => Task.CompletedTask;

0 commit comments

Comments
 (0)