webhaxetabbedkha

Haxe inactive browser tab stops completly


I'm developing a browser based game with haxe. As long as I have the browser tab as my active tab, everything works just as intended and smoothly, but as soon as i switch the tab (regardless if i'm using chrome or firefox) it stops working completly and doesn't send any heartbeat at all to my server.

I'm aware, that inactive tabs are slowed down in perfomance and i'm also aware, that inactive tabs are not allowed to load ressources. (My game does not do that).

I want my game to continue while it is inactive and more importantly, I want it to send a heartbeat to the server.

Is there any way I can enforce this?

I'm using Kha and Haxe, i tried the haxe native scheduler for the heartbeat and the kha scheduler, both of them don't work while beeing inactive.


Solution

  • Okay, I found the solution. The SystemImpl from kha is requesting the requestAnimationFrame and checks if it is null every timestamp.

    var window: Dynamic = Browser.window;    
    if (requestAnimationFrame == null) window.setTimeout(animate, 1000.0 / 60.0);
    else requestAnimationFrame(animate);
    

    During inactive tab, the requestAnimationFrame isn't null, but doesn't call the animate function. I've added a static bool value isFocused, which is changed everytime the browser closes or opens this window.

    To fix this i temporarely modified this small snippet into:

    var window = Browser.window;
    window.onfocus = function(){isFocused = true;}
    window.onblur = function()
    {
        window.setTimeout(animate, 1000.0 / 60.0); 
        isFocused = false; 
    }           
    if (requestAnimationFrame == null || !isFocused) window.setTimeout(animate, 1000.0 / 60.0);
    else requestAnimationFrame(animate);
    

    This way I'm checking if the current window is the active window and use the much smoother requestAnimationFrame and if not I'm using setTimeout. The callback methods are setting the is focused value. The Blur Callback value also makes sure, that the animate function is always called while blurred.

    I'm going to present this solution in the upcoming 1 or 2 weeks to rob, maybe he considers the implementation