APE_JSF Events

Louis Charette edited this page Apr 19, 2014 · 1 revision

In APE JSF there are three differents kinds of events :

  • Interval events.
  • Raw events.
  • Command events.

Internal events

Internal events are fired by APE Core, they are used for special case. Example :

  • When APE Core is loaded
  • When APE Core is disconnected (or reconnected) from APE Server
  • When a new pipe is created
  • ...

Internal events can be catched with addevent :

var client = new APE.Client();
client.load();
 
// Intercept 'load' event. This event is fired when the Core is loaded and ready to connect to APE Server
client.addEvent('load', function() {
    // Call core start function to connect to APE Server
    client.core.start({'name': 'Pooridge'});
});
 
//Listen to the ready event to know when your client is connected
client.addEvent('ready', function() {
    console.log('Your client is now connected');
});

Raw events

Every time APE JSF receives a raw from APE server, an event with the name of the raw is fired. Raw events can be catched with onRaw. The core passes as callback function's argument the data received from APE Server.

//Stuff to connect to APE server
var client = new APE.Client();
client.load();
client.addEvent('load', function() {
    client.core.start({'name': 'Pooridge'});
});
 
//Intercept login raw
client.onRaw('login', function(data) {
    //output data received from APE Server
    console.log(data);
});

Commands events

Every time APE JSF sends a command to server an event with the name of the command is fired. Commands events can be catached with onCmd. The core passes as callback function's argument the data sent to APE Server.

//Stuff to connect to APE server
var client = new APE.Client();
client.load();
client.addEvent('load', function() {
    client.core.start({"name":"Pooridge"});
});
 
//Intercept login raw
client.onCmd('connect', function(data) {
    //output data sent to APE Server
    console.log(data);
});

Pipe events

Pipe have there own events stack, This is very useful to listen some events only on a specific pipe.

//Stuff to connect to APE server
var client = new APE.Client();
client.load();
client.addEvent('load', function() {
    client.core.start({'name': 'Pooridge'});
});
client.onCmd('ready', function(data) {
    //Join two channels
    client.core.join(['testChannel', 'testChannel2']);
});
 
//multiPipeCreate event is fired when you join a channel
client.addEvent('multiPipeCreate', function(pipe, options) {
    //test if pipe is testChannel
    if (pipe.name == 'testChannel') {
        //Attach userJoin event only to pipe testChannel
        pipe.addeEvent('userJoin', function(user, pipe) {
            console.log('New user on pipe testChannel');
        });
       //Fire event 'samplePipeEvent' only on this pipe
       pipe.fireEvent('samplePipeEvent');
       //Fire event 'globalEvent' both on this pipe and Core
       pipe.fireGlobalEvent('sampleGlobalEvent');
    }
});

How does APE JSF detects the pipe where it have to fire the event?

When APE Core receives raws from APE Server it checks for presence of a key named pubid if the value of the pubid key corresponds to a known pipe's pubid on the core, the event is fired first on Core, then on the corresponding pipe. It works in the same way for commands. Being notified about all raws and commands

From version 1.0 it is now possible to get notified about all raws and commands using respectively 'onRaw' and 'onCmd' with addEvent(). Think of it as a onRaw('', callback) and onCmd('', callback), although the writing is different:

//React to all raw and cmd events
client.addEvent('onRaw', function(e) { console.log("Raw received") });
client.addEvent('onCmd', function(e) { console.log("Command sent") });

Predefined events

APE_JSF has several predefied events: *PipeDelete *apeDisconnect *clearSession *cmd_ *error_.. *init *load *multiPipeCreate *onCmd *onRaw *progress *proxyPipeCreate *raw_.. *ready *restoreEnd *restoreStart *uniPipeCreate