Skip to content
This repository has been archived by the owner on Oct 19, 2020. It is now read-only.

API reference : server

William Mcmurray edited this page Oct 20, 2013 · 5 revisions

Events listeners

You can declare theses methods in your extended class, theses will be executed when the event is fired on the server.

Method Parameters Description
on_client_connect()
  • client - SocketClient()
Executed when a client is trying to connect to the server (before handshake is done).
on_client_handshake()
  • client - SocketClient()
  • headers - array()
  • vars - array()
Executed when a client is sucessfully handshaked. You got access to an array containing headers sent along with the client request and an array containing variables passed in URL.
on_client_disconnect()
  • client - SocketClient()
Executed when a client socket is closed.
on_client_kick()
  • client - SocketClient()
Executed when a client is kicked from the server.
on_server_tick()
  • tickCounter - int
Executed when a server tick occurs.
on_server_shutdown() Executed when the server is shuting down.
on_server_reboot() Executed just before the server reboots.

Incoming data handling

The main protocol for data transmissions is a JSON object containing two properties : "action" and "content". The action is used to dispatch data more easily and content is used to transmit data. The format for system transmissions is almost the same, the only difference is the "action" property which is renamed "sys". Beside of that, you can send raw data which doesn't use these protocol. More details here.

Method Parameters Description
handle_[ACTION_NAME_HERE]()
  • client - SocketClient()
  • data - *
This method dosen't exist, you create it for each action you want to handle. Data parameter will be whatever you sent to the server.
sys_handle_[COMMAND_NAME_HERE]()
  • client - SocketClient()
  • data - *
This method dosen't exist, you create it every time you want to add a new command in the system data transmission layer. This could be useful if you want to extend remote admin functionality without mixing it with your app data transmissions. In this case, you'll need to use the "register_admin_command()" method too.
raw_handle()
  • client - SocketClient()
  • data - *
This method is used to handle raw data that has been sent without using the main JSON format. You'll have to dispatch what to do with the data yourself. It's useful for those who want to optimize bandwidth use if JSON format is too heavy.

Methods

Global settings

Use theses to set some global variables that affect behavior of server. (call them preferably in the init() method.)

Method Parameters Description
set_admin_password()
  • newPassword - string
Used to set the password that will be required to be granted by admin priviledges via a JavaScript client. Default value is "root".
set_server_name()
  • serverName - string
Set the name of the server. Used almost only for display in command prompt. Default value is "SERVER".
set_max_clients()
  • maxClient - int
Set the limit of accepted clients connections on the server. If this limit is reached, new connections will be rejected until connected clients disconnects. Default value is 1000.
set_tick_interval()
  • nb - int
Set the number of seconds between each server tick events.
set_socket_select_timeout()
  • nb - int
Set the number of seconds before timing out the socket_select function.
set_socket_option()
  • optname - const
  • optval - string || int ||...
Set the value of an option on the master socket (it uses the socket_set_option function).
set_socket_recv_len()
  • nb - int
Set the amount of bytes read by the socket_recv function.
register_admin_command()
  • command - string || array()
Add commands that will require admin privileges to be executed. Use this method if you decide to extend remote admin functionality by adding system commands handlers.

Configuration

Use theses to set configs options specific to your server. You could just set properties in your class but these methods provide a more centralized environement that is used, by exemple, to apply same config to a new server instance when it's rebooted.

Method Parameters Description
set_config()
  • properties - array(key=>value)
Set config properties on server by passing an array of key and values pairs.
get_config()
  • property - string
Retreive the value of a config property by passing its name.

Filtering clients

Use theses to retrieve clients lists or infos about connected clients.

Method Parameters Description
list_clients()
  • properties - array()
  • clients - array()
Return an array of clients each containing an array of desired properties.
get_clients_count() Return connected clients count (handshaked or not).
get_clients()
  • exceptions - array() || SocketClient()
Return an array of all clients except those passed in parameter.
get_clients_from_group()
  • group - SocketClient()->get_group()
  • exceptions - array() || SocketClient()
Return an array of all clients in the same group as the group passed in parameter.
get_npcs() Return an array of all clients that dosen't have a socket (when created manually).

Sending data to clients

Use theses to send data to all clients or specific clients. Normal data transmissions are done in JSON and are separated in two parts : the "action" and the "content". You can send raw data if you want by not passing an "action" to these methods bellow. By exemple, instead of :

$this->send($client, "action_name", "content_to_be_sent");

you would do :

$this->send($client, "content_to_be_sent");

This principle is applicable to every methods bellow.

Method Parameters Description
send()
  • client - array() || SocketClient()
  • action - string
  • content - array() || string
Send data to a client or an array of clients.
send_to_all()
  • action - string
  • content - array() || string
Send data to all clients.
send_to_others()
  • client - array() || SocketClient()
  • action - string
  • content - array() || string
Send data to all clients except those passed in parameter.
send_to_others_in_group()
  • client - SocketClient()
  • action - string
  • content - array() || string
Send data to other clients in the same group as the group on the client passed in parameter.
send_to_group()
  • group - SocketClient()->get_group()
  • action - string
  • content - array() || string
Send data to every clients of the same group.

Other methods

Method Parameters Description
create_client()
  • socket - socket
Creates a new client in the server. You may ommit to pass a socket if you want, this will create a user considered as a NPC.
disconnect()
  • client - SocketClient()
Close the connection between a client and the server.

Command line interface

You can add options when starting the server from command prompt which will affect server behavior. Here is a list of available options :

Option Description
-admin Enable remote admin access. If not specified, all admin actions will be disabled.
-verbose Server will output what's happening. (clients connections, handshake, etc.)
-debug Server will output some additional infos that may help you debuging. PHP errors will also be displayed only when this option is set.
-warn Server will output warnings when something suspicious happens, like a client trying to access admin commands without being admin.

A typical development server setting may look like this :

php ../../core/wrapper.php -server:MyServer -admin -verbose -debug

and a production server setting may look like this :

php ../../core/wrapper.php -server:MyServer -admin -warn