Server script base class. This class provides the functionality to create server scripts that can connect to database, send requests to cache or slave servers and make HTTP requests.

The scripts based on that class will load "script.cfg" file from the same directory that the script is located in. You can find the list of supported configuration variables in Server-side Scripts article.

Note: The script should run the init() method first to load the configuration file and set up the database and server connections (if needed).

Constructor

new ()

Fields

config:Config

Script configuration variables.

Methods

connectDatabase ():Connection

Connects to database specified in the configuration variables and returns the connection. This method is needed only if you do not intend to use query() call. Note that in that case you will have to provide the thread safety yourself.

Returns:

New database connection. Throws exception if the connection could not be established.

init ():Void

Initializes script state. Loads "script.cfg" configuration file, establishes database and server connections (if enabled). This method should be the first one called in the script.

log (s:String):Void

Adds a given message to the logfile ("log.txt" in case of simple log, "logs/log---

.txt" in case of extended). This method is thread-safe.

If the "log.extended" configuration variable is set, the logger will be in a separate thread and use "logs/" directory to store hourly logs instead of "log.txt".

Parameters:

s

Log message.

nextID (t:String):Int

Returns next available row ID from sequence for given database table.

Parameters:

t

Database table name.

Returns:

Next available row ID.

inline query (q:String):ResultSet

Runs SQL query through the database connection. This method is thread-safe. This method will throw an exception if the connection to the database was not established.

Parameters:

q

SQL query string.

Returns:

The result of the query.

inline quote (s:String):String

Escapes a given string for using in SQL query as a row field.

Parameters:

s

String.

Returns:

Escaped string.

request (isCacheServer:Bool, msg:Dynamic):Dynamic

Sends request to the game or cache server and returns the response. This method will throw an exception if the connection to the server was not established.

Parameters:

isCacheServer

Set to true, if the request is going to the cache server.

msg

Request contents.

Returns:

Server response object. Returns null in case of an error.

inline urlRequest (url:String, method:String, params:Dynamic):String

Sends HTTP request with a given URL and returns the result.

Note: To enable HTTPS support with Neko 2.0, you will need to install hxssl library with "haxelib git hxssl https://github.com/tong/hxssl" command and insert the "-lib hxssl" line into the compilation HXML file.

Parameters:

url

HTTP URL.

method

Request method, "get" or "post".

params

Request parameters object. If the object contains field "_postData", it will be used to set POST request data. If the object contains field "_headers" (key-value map), it will be used to set request headers.

Returns:

Webserver response. If the connection could not be established, returns "#CONNECT" string. If the request failed due to an exception, returns "#ERROR" string.

inline urlRequestExt (url:String, method:String, params:Dynamic, retries:Int, pause:Float):String

Extended version of HTTP request. Sends HTTP request with a given URL and returns the result. Supports automatic request retry on error.

Note: To enable HTTPS support with Neko 2.0, you will need to install hxssl library with "haxelib git hxssl https://github.com/tong/hxssl" command and insert the "-lib hxssl" line into the compilation HXML file.

Parameters:

url

HTTP URL.

method

Request method, "get" or "post".

params

Request parameters object. If the object contains field "_postData", it will be used to set POST request data. If the object contains field "_headers" (key-value map), it will be used to set request headers.

retries

Number of attempts to send the request. If it equals 0, will try indefinitely.

pause

Time in seconds to pause between attempts.

Returns:

Webserver response. If all attempts to make the request failed, returns "#ERROR" string.