From Ape Wiki
Contents |
[edit] APE JSF events
In APE JSF there are three differents kinds of events :
[edit] Internal events
Internal events are fired by APE Core and are used for special cases. Examples:
- 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 caught 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'); });
For a list of all internal events, check the events doc
[edit] 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 caught with onRaw. The data received from the ape server is passed within an object in the first argument to the event handler.
//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); });
[edit] Command events
Every time APE JSF sends a command to server an event with the name of the command is fired. Commands events can be caught with onCmd. The data sent to the APE server is passed to the callback function as the first argument.
//Stuff to connect to APE server var client = new APE.Client(); client.load(); client.addEvent('load', function() { client.core.start({"name":"Pooridge"}); }); //Intercept connect comand client.onCmd('connect', function(data) { //the data sent to APE Server console.log(data); });
[edit] Pipe events
Pipe have their own events stack. This is very useful if you want to listen for events 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.addEvent('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'); } });
[edit] How does APE JSF detect the pipe where it has 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.
[edit] Being notified about all raws and commands
From version 1.0 and up an internal event will fire whenever any raw or cmd type event is fired; 'onRaw' and 'onCmd' respectively. Think of it as onRaw('*', callback) and onCmd('*', callback), although the written like any other internal event. addEvent('onRaw', callback)
//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") });


