Skip to content

KBEngine Module

Member Functions

def addTimer( initialOffset, repeatOffset=0, callbackObj=None ):

def delTimer( id ):

def urlopen( url, callback, postData, headers ):

Callback Functions

def onLoginAppReady( ):

def onLoginAppShutDown( ):

def onRequestLogin( loginName, password, clientType, datas ):

def onLoginCallbackFromDB( loginName, accountName, errorno, datas ):

def onRequestCreateAccount( accountName, password, datas ):

def onCreateAccountCallbackFromDB( accountName, errorno, datas ):

Member Function Documentation


def addTimer( initialOffset, repeatOffset=0, callbackObj=None ):

Description:

Registers a timer, which is triggered by the callback function callbackObj. The callback function will be executed for the first time after "initialOffset" seconds, and then every "repeatOffset" seconds.

Example:

py
# Example of using addTimer
import KBEngine

# Add a timer, execute for the first time after 5 seconds, then every 1 second, user parameter is 9
KBEngine.addTimer( 5, 1, onTimer_Callbackfun )

# Add a timer, execute after 1 second, user parameter defaults to 0
KBEngine.addTimer( 1, onTimer_Callbackfun )

def onTimer_Callbackfun( id ):
    print("onTimer_Callbackfun called: id %i" % ( id ))
    # if this is a repeating timer, when it is no longer needed, call the following function to remove it:
    #     KBEngine.delTimer( id )

Parameters:

initialOffset: float, the interval (seconds) from registration to the first callback.

repeatOffset: float, the interval (seconds) between each execution after the first callback. Must be removed with delTimer, otherwise it will repeat indefinitely. Values less than or equal to 0 will be ignored.

callbackObj: function, the specified callback function object.

Returns:

integer, returns the internal id of the timer, which can be used to remove the timer with delTimer.


def delTimer( id ):

Description:

The delTimer function is used to remove a registered timer. The removed timer will no longer execute. Timers that only execute once are automatically removed after the callback, so there is no need to remove them with delTimer. If delTimer is called with an invalid id (e.g., already removed), an error will occur.

See KBEngine.addTimer for an example of using timers.

Parameters:

id: integer, specifies the timer id to remove.


def urlopen( url, callback, postData, headers ):

Description:

This script function provides asynchronous HTTP/HTTPS requests.

Parameters:

url: A valid HTTP/HTTPS URL, string type.

callback: Optional parameter, a callback object (such as a function) with the result of the request. This callback takes 5 parameters: HTTP response code (e.g., 200), response content, HTTP headers, success status, and the request URL.

Example Declaration

py
def onHttpCallback(httpcode, data, headers, success, url):
    print(httpcode, data, headers, success, url)

As shown above:

httpcode: The HTTP response code, an integer value.

data: The response content, a string.

headers: The HTTP headers returned by the server, e.g., {"Content-Type": "application/x-www-form-urlencoded"}, a dictionary.

success: Whether the request was successful. If there was an error, this is False. You can use httpcode to further determine the error.

url: The URL used for the request.

postData: Optional parameter. By default, a GET request is made to the server. If you need a POST request, provide the content to POST, and the engine will automatically use POST. It is bytes.

headers: Optional parameter. The HTTP headers to use for the request, e.g., {"Content-Type": "application/x-www-form-urlencoded"}, a dictionary.

Callback Function Documentation


def onLoginAppReady( ):

Description:

This function is called when the current process is ready.

Note

This callback must be implemented in the entry module (kbengine_defaults.xml->entryScriptFile).


def onLoginAppShutDown( ):

Description:

This function is called when the process is shutting down.

Note

This callback must be implemented in the entry module (kbengine_defaults.xml->entryScriptFile).


def onRequestLogin( loginName, password, clientType, datas ):

Description:

Callback when the client requests to log in to an account on the server.

You can perform management and control of user login here, for example:

You can use this interface to intercept user login, record the request for queuing, and return an error code to the client to indicate the queue status. The client can keep logging in to get the status from here.

Note

This callback must be implemented in the entry module (kbengine_defaults.xml->entryScriptFile).

Parameters:

loginName: string, the account name submitted at login.

password: string, MD5 password.

clientType: integer, client type, provided at login.

datas: bytes, data attached by the client request, can be forwarded to third-party platforms.

Returns:

Tuple, the return value is (error code, real account name, password, client type, client submitted data datas). If there is nothing to extend or modify, usually return the passed-in values (KBEngine.SERVER_SUCCESS, loginName, password, clientType, datas).


def onLoginCallbackFromDB( loginName, accountName, errorno, datas ):

Description:

Callback returned by dbmgr after the client requests to log in to an account on the server.

Note: This callback must be implemented in the entry module (kbengine_defaults.xml->entryScriptFile).

Parameters:

loginName: string, the account name submitted at login.

accountName: string, the real account name (queried by dbmgr).

errorno: integer, error code. If not KBEngine.SERVER_SUCCESS, it means login failed.

datas: bytes, can be any data, e.g., data returned by a third-party platform or data returned by dbmgr and interfaces during login processing.


def onRequestCreateAccount( accountName, password, datas ):

Description:

Callback when the client requests to create an account on the server.

Note: This callback must be implemented in the entry module (kbengine_defaults.xml->entryScriptFile).

Parameters:

accountName: string, the account name submitted by the client.

password: string, MD5 password.

datas: bytes, data attached by the client request, can be forwarded to third-party platforms.

Returns:

Tuple, the return value is (error code, real account name, password, client submitted data datas). If there is nothing to extend or modify, usually return the passed-in values (KBEngine.SERVER_SUCCESS, loginName, password, datas).


def onCreateAccountCallbackFromDB( accountName, errorno, datas ):

Description:

Callback returned by dbmgr after the client requests to create an account on the server.

Note: This callback must be implemented in the entry module (kbengine_defaults.xml->entryScriptFile).

Parameters:

accountName: string, the account name submitted by the client.

errorno: integer, error code. If not KBEngine.SERVER_SUCCESS, it means login failed.

datas: bytes, can be any data, e.g., data returned by a third-party platform or data returned by dbmgr and interfaces during account creation processing.

文档内容由 Comblock® 提供