onRaw

(since )



Intercept a raw received on a pipe.

Description


string onRaw ( string type, string fn )

Execute a function when a raw is received and pass to the function the data received from the server.

If the received raw is related to a pipe (e.g : you receive data from a channel) the second arguments will be a pipe object.

If you send custom raw and you want to intercept those on the pipe you need to add a pipe arguments to data your are sending. (see examples for more information)

Parameters


type
Name of the raw (e.g : 'login', 'data');

fn
Callback function to execute
  • args - (object, optional) An object containing all data sent by 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


//ape var is a reference to APE instance
 
//Add an event when a new pipe is created
ape.addEvent('pipeCreate', function(type, pipe, options) {
    //Test if the pipe is a "multi pipe" with name test1
    if (type == 'multi' && pipe.name == 'test1') {
        //Add an event when a message is received on the pipe test1
        pipe.onRaw('data', function(data, pipe) {
            console.log('data received on pipe', pipe.name, ' message : ', data.msg);
        });
    }
 
    //When a new pipe is created, send hello world
    pipe.send('Hello world');
});
 
//Join channel test1 and test2
ape.join(['test1', 'test2']);

//Server side code
 
var chan = Ape.getChannelByName('demoChannel');
chan.pipe.sendRaw('testRaw', {'foo': 'bar', 'pipe': chan.pipe.toObject()});
 
//Client side code (pipe is a pipe object)
pipe.onRaw('testRaw', function(data, pipe) {
    console.log('testRaw received on pipe');
});

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.