Javascript onunload function or meteor blaze destroy


#1

Hi there. The title is not very good for this question, but I will do my best to explain better.

I read a lot on this, but I still can’t figure it out. I have to do some jobs when the user leaves my page.
I tried to use Template.myTemplate.onDestroyed(function(){} and others similar ways to do that, but all the methods i tried is really catch how I want !

So I used the window.onbeforeunload to warn him that he will quit the page and the window.onunload to launch my job. This seems to works really better. The problem is, when I’m doing the onunload function, it doesn’t finish correctly before my page is already closed.

I tried this then :

    window.onunload = function() {
        //freezeScreen(5000);    // That delay retard well the browser UI closing, but this is still not working
        console.log("destroy finished 1/8");
        Blaze._globalHelpers.terminalHS6Telnet_destroy();
        console.log("destroy finished 8/8");
    }

    Template.registerHelper('terminalHS6Telnet_destroy', function () {
        console.log("destroy finished 2/8");
        $.each(_tabClassObj, function(index) {
            _tabClassObj[index].destroy();
        });
        console.log("destroy finished 3/8");
        _tabClassObj = {};
        console.log("destroy finished 4/8");
        _terminalTabCounter = 0;
        console.log("destroy finished 5/8");
        window.onbeforeunload = null;
        console.log("destroy finished 6/8");
        window.onunload = null;
        console.log("destroy finished 7/8");
    }

But Here is my console log :

destroy finished 1/8
destroy finished 2/8
WebSocket is already in CLOSING or CLOSED state.
WebSocket is already in CLOSING or CLOSED state.
WebSocket is already in CLOSING or CLOSED state.
WebSocket is already in CLOSING or CLOSED state.
WebSocket is already in CLOSING or CLOSED state.
WebSocket is already in CLOSING or CLOSED state.

I tried to delay the browser UI from closing with the freeze function that delays my browser shutdown by 5 sec… but it’s still not working. I also tried a setTimeout here… but got the same result. What is the problem ? Is this the good way to do it ? Thanks for advice.

Edit : some codes like resetting variable etc is useless here… but this function is also used by the unload of iron:router when I’m changing page. So I let it in this example to show you all the code.


#2

I couldn’t done what i would like with javascript event.
To mention my meteor server my client has closed his browser, I developped a ping system like that :

// Both side
TelnetUserConnections = new Meteor.Collection('TelnetUserConnections');

// Client side
Meteor.startup(function(){
    // Timer who refresh the heartbeat on server
    Meteor.setInterval(function () {
        Meteor.call('keepAlive', Blaze._globalHelpers.currentUserId());
    }, 5000);
});

// Server side
Meteor.methods({
    "keepAlive": function (user_id) {
        if (!TelnetUserConnections.findOne({user_id: user_id})) {
           TelnetUserConnections.insert({user_id: user_id});
        }
        TelnetUserConnections.update({user_id: user_id}, {$set: {last_seen: (new Date()).getTime()}});
    }
});

Meteor.setInterval(function () {
    // do the job when user hasn't pinged since 20 secondes
    var now = (new Date()).getTime();
    TelnetUserConnections.find({last_seen: {$lt: (now - 20 * 1000)}}).forEach(function (user) {
        
        /* here is my destroy stuff */

        TelnetUserConnections.remove({_id : user._id});
    });
});

Maybe it can helps