From Ape Wiki

Jump to: navigation, search

Here will look at a problem often encountered, how to send a hash rather than a string.

Contents

[edit] The problem

The problem is that core.send() only accepts string. If you try to send core.send({month: 'july'}); you'll get this error:

[{"time":"1257418208","raw":"ERR","data":{"chl":5,"code":"001","value":"BAD_PARAMS"}}]

One solution to send a hash would be to serialize (for example through JSON.stringify()).

However here we will look at a second approach, writing a new command. It will be defined on the server, don't worry it's easy as it can be.

[edit] How to send (usage)

It's very simple to send:

pipe.request.send('SEND_HASH', {month: 'july'});

or, to send along user information:

pipe.request.send('SEND_HASH', {month: 'july'}, {"from": infos.user.pipe});

[edit] How to receive (usage)

client.core.onRaw('HASH_DATA', function(raw, pipe) {
        alert(raw.data.month);   // raw.data is the hash we sent previously!
});

[edit] How to add the command on the server

It's easy to add a command on the server thanks to the embedded Javascript engine.

vi APE_Server/scripts/commands/SEND_HASH.js

Don't forget to include SEND_HASH.js in main.ape.js

Here's the new file SEND_HASH.js on the server:

Ape.registerCmd("SEND_HASH", true, function(params, infos) {
 Ape.log("Received SEND_HASH");
 
 var pipe = Ape.getPipe(params.pipe);
 if (!$defined(pipe)) return ["4400", "WHERESTHEPIPE"];
 
 pipe.sendRaw('HASH_DATA', {
 	type:params.type, 
 	id:params.id,
	operation:params.operation,
	month:params.month
	});
 return 1;
})

For more details on writing a server-side module, please refer to How_to_build_a_serverside_JS_module.

[edit] How to choose command names

The APE team doesn't emit any convention for the moment. However those principles can be observed:

  • For clarity reasons it's better to use different names for raw and commands (for example SEND_HASH and HASH_DATA rather than HASH)
  • The raws and commands are case-insensitive, however they always have been written upper cased by the APE team

That is it!