AppEvent Antwort nicht möglich

Issue #71 resolved
Enrico Schultz created an issue

Eine App-Instanz kann an einer anderen Instanz, die ein Event verschickt hat, nicht antworten.

var App = new (function() {

    this.onAppEventReceived = function(appInstance, type, data) {

        var ownInstance = KnuddelsServer.getAppAccess().getOwnInstance();
        var isRootInstance = ownInstance.isRootInstance();

        if (type === 'toroot') {
            // only root instance should handle this
            if (! isRootInstance) {
                throw new Error('this event can only get handled by the root instance');
            }

            KnuddelsServer.getDefaultBotUser().sendPublicMessage('Event bei Root angekommen.');

            // reply
            appInstance.sendAppEvent('fromroot', {});

        } else if (type === 'fromroot') {
            // only child instance should handle this
            if (isRootInstance) {
                throw new Error('this event cannot be sent to the root instance');
            }

            KnuddelsServer.getDefaultBotUser().sendPublicMessage('Event bei Sub angekommen.');
        }
    }

    this.chatCommands = {
        'appevent': function(user, params, command) {
            var ownInstance = KnuddelsServer.getAppAccess().getOwnInstance();
            var rootInstance = ownInstance.isRootInstance() ? ownInstance : ownInstance.getRootInstance();

            rootInstance.sendAppEvent('toroot', {});
        }
    }

})();

Event bei Root angekommen.

ERROR: org.mozilla.javascript.JavaScriptException: Error: this event cannot be sent to the root instance.

Dabei sollte appInstance laut Doku doch die Instanz sein, die das App-Event ausgelöst hat.

Comments (6)

  1. Enrico Schultz reporter

    Workaround:

    Bei den App-Daten wird die aktuelle AppUid mit übergeben:

    rootInstance.sendAppEvent('toroot', {'uid': ownInstance.getAppInfo().getAppUid()});
    

    Und im Event selbst neu ausgewertet, da die übergebene AppInstance derzeit IMMER Root ist:

    // temporary fix
    var allInstances = ownInstance.getAllInstances(false);
    for (var i = 0; i < allInstances.length; i++) {
        if (allInstances[i].getAppInfo().getAppUid() === data.uid) {
            appInstance = allInstances[i];
            break;
        }
    }
    
  2. Frederic Leitenberger (privat)

    Hi, ich korrigiere gerade diesen Bug. Bin schon fast fertig.

    Noch ein Hinweis am Rande:

    var rootInstance = ownInstance.isRootInstance() ? ownInstance : ownInstance.getRootInstance();
    

    geht auch so:

    var rootInstance = ownInstance.getRootInstance();
    
  3. Frederic Leitenberger (privat)

    Habe es mehrfach getestet und es funktioniert bei mir: lokal, sowie auf dem DEV-Server einwandfrei.

    Im Sub-Channel:

    1. Im Sub-Channel /appevent eingegeben.
    2. Im Root-Channel erscheint: Event bei Root angekommen.
    3. im Sub-Channel erscheint: Event bei Sub angekommen.

    Im Root-Channel ist es natürlich anders:

    1. Im Root-Channel /appevent eingegeben.
    2. Event bei Root angekommen.
    3. ERROR: org.mozilla.javascript.JavaScriptException: Error: this event cannot be sent to the root instance

    Dass es im Root-Channel so nicht geht ist ja aber klar!

  4. Log in to comment