Home » Docs » Server »
 

registerCmd

(since 1.0)



Register a new server command.

Description


string registerCmd ( string name, boolean require_sessid, function fn )

Used to register a new command on the server, this command can then be used by clients.

Parameters


name
The command name

require_sessid
Does this command requires a connected user (is sessid required) ?

fn
This function will be called when an user uses the created command.
  • params - (object) The list of parameters sent by the client.
  • info - (object) Contains information about the client:
    • host (string): Host sent on HTTP headers.
    • client (socket): The client socket's object.
    • chl (number): The challenge number.
    • ip (string): The client's IP.
    • user (user) User object (if logged in).
    • subuser (subuser) User object (if logged in).
    • http (array): contains user HTTP headers.

Examples


Ape.registerCmd("foocmd", true, function(params, info) {
	Ape.log("The user ip : ("+info.ip+"), foo : " + params.foo);
});

/*
 * You can return error in two ways:
 *  By returning 0 to return a "BAD_PARAMS"
 *  By returning an array [code, error] for custom errors.
 */
Ape.registerCmd("foocmd", true, function(params, info) {
	if (!$defined(params.john)) return 0; // send a "BAD_PARAMS" RAW to the user
	if (params.john != "doe") return ["209", "NOT_A_JOHN_DOE"];
	return 1;
});

//Return an object to send a raw as response.
Ape.registerCmd( "gettime", true, function(params, info) ) {
	return {
		name: "TIME",//The raw's name
		data: { time: new Date().getTime() }//The raw's data (must be an object)
	}
}

See also


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.