[Solved-ish] Disconnecting Gracefully in WebGL

Good day all :slight_smile: I’ve been trying to narrow down the cause of some errors that occur in my WebGL build when a player navigates away from the game. Everything works fine up until that point, and I believe I’ve narrowed the cause down to a specific root.

When the user navigates away from the page, an “End Session Request” is sent to the game’s backend (Gamesparks), but the request is interrupted before it can be completed, presumably because the user has navigated away from the program before the operation has been finished. (Note: I’m only experiencing this issue with WebGL and receive no errors from the Unity Web Player)

I’m inquiring with the folks who run my BaaS of choice, but thought it might be good to inquire here as well. Do any of you have any suggestions as to how I’d go about ensuring that the game / browser disconnect gracefully, without causing a ton of “unexpected abort” and/or “uncaught exception” errors? Any advice would most certainly be appreciated.

Hmm, perhaps if I fire off some code in OnApplicationQuit() I can coordinate things with the BaaS end of things and stop the Unexpected Abort errors, etc. I’ll do some testing…

While it isn’t “graceful” per se, I managed to use the following code snippet to get rid of the numerous Unexpected Abort &/or Uncaught Exception errors that were being thrown when a player navigated away from the game’s canvas:

    void Start ()
    {
        string eval = "";
        eval += "function OnApplicationQuit()";
        eval += "{";
        eval += "   GetUnity().SendMessage('" + gameObject.name + "', 'OnApplicationQuit', '');";
        eval += "   return true;";
        eval += "}";
        eval += "window.onbeforeunload = OnApplicationQuit;";
        Application.ExternalEval(eval);
    }

Sharing in case it does someone else some good one day. FYI, I do still get a few errors in the browser’s JS debugging Console, but at least they’re no longer being popped up in an annoying window that the user has to click on to escape from, haha. I can live with that & will see what, if anything, can be done about the few errors that remain. Good times! :stuck_out_tongue:

1 Like

You are a genius! This just solved an issue I was having with using mail to code for email sending. It would generate errors and crash after opening outlook. Thanks a bunch for this fix!

1 Like