Skip to content

KcdMp.post

Asks a client script something and waits for its answer.

A client script of the server answers in its OnWebEvent(frame, name, data) callback; the value the callback returns is what the promise resolves to, read from JSON. data is any JSON value (or nothing) of at most 16 KB.

The promise rejects with an Error whose message says why: "no handler" when no client script answers, "handler failed" when the script raised an error, "timeout" when no answer came within about ten seconds, "too many calls" over the budget of about a hundred calls a second, and a text of the game’s when the interface reloaded or the game left the server while the call waited. A call never hangs.

Use KcdMp.emitServer for the game mode: the mode’s own answers come back as events or messages.

await KcdMp.post(name, data?)
Parameter Type
name string what the call is, 1 to 64 characters of a-z 0-9 _ . : - - the script’s OnWebEvent gets it as name
data unknown any JSON value (optional)

Promise<unknown> - the script’s answer, read from JSON

try {
const reply = await KcdMp.post("buy", { item: "bread" });
showResult(reply.ok);
} catch (e) {
showError(e.message); // "no handler" when the server has no client script for it
}

KcdMp.onMessage · KcdMp.emitServer · the Messages and events group of the index