onCmd

(since )



Intercept a command

Description


string onCmd ( string type, string fn )

Execute a function when a command is sent and pass to the function the arguments sent to APE server.

If the sent raw is related to a pipe (e.g : you sent data to a channel) the first arguments will be a pipe object.

Parameters


type
Name of the command (e.g : 'connect', 'send');

fn
Callback function to execute
  • args - (object, optional) An object containing all data sent to the server.
  • pipe - (object, optional) If the request is made on a pipe (e.g : you sent data to a channel) the second argument will be a pipe object.

Examples


//client var is a reference to APE.Client instance
 
//start(); method is used to connect to APE.
client.core.start();
 
//Intercept connect command (connect command is used to initiate connection to APE server)
client.onCmd('connect', function() {
    console.log('Connect command sent');
});
 

//client var is a reference to APE.Client instance
 
//Intercept send command (send command is used to send data to a pipe)
client.onCmd('send', function(pipe, data) {
    console.log('You sent ' + data.msg + ' to pipe with pubid' + pipe.getPubid());
});
 
//Intercept pipeCreate event (when a new pipe is created)
client.addEvent('pipeCreate', function(type, pipe, options) {
    //send a message on the pipe ("complex" way)
    pipe.request.send('SEND', 'Hello');
 
    //Sending a message could also be done more easily with this code
    pipe.send('Hello again!');
});

See also


  • Commands » [[Category:Protocol]] The [[APE_Server | APE Server]] is controllable via '''Commands''' sent by a... (Wiki)

User notes


# No comment

Post a note


User notes may be edited or deleted, and usually a note is deleted because of the following reasons:
  • Bugs: Instead report a bug
  • Missing documentation: report that as a bug.
  • Support questions: See the community for available options.

In other words, do not ask questions within the user notes.

Note <b>,<u>,<i> HTML tags are allowed in the posts and the note formatting is preserved. URLs will be turned into clickable links, JavaScipt and C code blocks enclosed in the <source="javascript">JS Code</source> and <source="c">C Code</source> tags will be source highlighted automatically.