Skip to content

Commit

Permalink
fix: WebGL BackgroundWorker implementation & sample packages
Browse files Browse the repository at this point in the history
  • Loading branch information
vaind committed Mar 23, 2022
1 parent ef4eab4 commit fbe97d3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 25 deletions.
3 changes: 2 additions & 1 deletion samples/unity-of-bugs/Packages/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"com.unity.modules.androidjni": "1.0.0",
"com.unity.modules.audio": "1.0.0",
"com.unity.modules.screencapture": "1.0.0",
"com.unity.modules.ui": "1.0.0"
"com.unity.modules.ui": "1.0.0",
"com.unity.modules.unitywebrequest": "1.0.0"
}
}
6 changes: 6 additions & 0 deletions samples/unity-of-bugs/Packages/packages-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@
"depth": 0,
"source": "builtin",
"dependencies": {}
},
"com.unity.modules.unitywebrequest": {
"version": "1.0.0",
"depth": 0,
"source": "builtin",
"dependencies": {}
}
}
}
35 changes: 11 additions & 24 deletions src/Sentry.Unity/WebGL/SentryWebGL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Sentry.Extensibility;
using Sentry.Infrastructure;
using Sentry.Internal;
using Sentry.Internal.Http;
using Sentry.Protocol.Envelopes;
using UnityEngine;
using UnityEngine.Networking;
Expand Down Expand Up @@ -60,45 +61,31 @@ public WebBackgroundWorker(SentryUnityOptions options, SentryMonoBehaviour behav

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

private IEnumerator SendEnvelope(Envelope envelope)
{
var dsn = Dsn.Parse(_options.Dsn!);
var authHeader =
$"Sentry sentry_version={Sentry.Constants.ProtocolVersion}," +
$"sentry_client={UnitySdkInfo.Name}/{UnitySdkInfo.Version}," +
$"sentry_key={dsn.PublicKey}," +
(dsn.SecretKey is { } secretKey ? $"sentry_secret={secretKey}," : null) +
$"sentry_timestamp={_clock.GetUtcNow().ToUnixTimeSeconds()}";

var www = new UnityWebRequest(dsn.GetEnvelopeEndpointUri());
www.method = "POST";
www.SetRequestHeader("X-Sentry-Auth", authHeader);
var builder = new HttpRequestBuilder(_options);
var www = new UnityWebRequest();
www.url = builder.GetEnvelopeEndpointUri().ToString();
www.method = UnityWebRequest.kHttpVerbPOST;
www.SetRequestHeader(builder.AuthHeaderName, builder.AuthHeader(_clock.GetUtcNow()));
// TODO is it OK to call .Wait() here in webGL?
var stream = new MemoryStream();
envelope.SerializeAsync(stream, _options.DiagnosticLogger).Wait(TimeSpan.FromSeconds(2));
stream.Flush();
www.uploadHandler = new UploadHandlerRaw(stream.ToArray());
www.downloadHandler = new DownloadHandlerBuffer();
yield return www.SendWebRequest();

while (!www.isDone)
{
yield return null;
}
if (
www.isNetworkError || www.isHttpError
|| www.responseCode != 200)
if (www.isNetworkError || www.isHttpError || www.responseCode != 200)
{
_options.DiagnosticLogger?.LogWarning("error sending request to sentry: {0}", www.error);
}
{
_options.DiagnosticLogger?.LogDebug("Sentry sent back: {0}", www.downloadHandler.text);
_options.DiagnosticLogger?.LogWarning("error sending request to Sentry: {0}", www.error);
}

_options.DiagnosticLogger?.LogDebug("Sentry sent back: {0}", www.downloadHandler.text);
}

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

0 comments on commit fbe97d3

Please sign in to comment.