Luna Framework Documentation
Complete guide for using the Luna server library and Lunac client library for creating network applications in Lua (5.1/LuaJIT).
Luna - Server Side
Core methods for working with the Luna server.
Updates server state. Should be called in the main application loop.
function love.update(dt)
luna.update(dt)
end
Creates a new server application.
Configuration Parameters:
Parameter | Type | Required | Description |
---|---|---|---|
name | string | Yes | Application name (for debugging) |
max_ip_connected | number | No | Maximum connections from a single IP |
error_handler | function | No | Function for error handling |
no_errors | boolean | No | If false, errors will stop the server. If true, errors will be passed to error_handler |
debug | boolean | No | when debug = false is set - disables application debugging, default is true |
host | string | Yes | Host to run server on (e.g. "127.0.0.1") |
port | number | Yes | Port to run server on (e.g. 8080) |
new_client | function | No | Callback when new client connects: function(client) |
close_client | function | No | Callback when client disconnects: function(client) |
request_listener | function | No | Callback when new request |
Example:
local app = luna.new_app({
name = "My Server",
max_ip_connected = 20,
error_handler = function(err)
print("Server error:", err)
end,
no_errors = true,
host = "127.0.0.1",
port = 8080,
new_client = function(client)
print("New client connected")
end,
close_client = function(client)
print("Client disconnected")
end
})
Removes a server application. Accepts either the app_data object returned by new_app, or the application name.
luna.remove_app("My Server")
-- or
luna.remove_app(app)
Application (App)
Methods for working with server applications.
Returns a table of all connected clients.
local clients = app:get_clients()
Creates a new router for request handling.
Configuration Parameters:
prefix | string | No | Prefix for all routes in this router |
no_errors | boolean | No | Overrides global no_errors setting for this router |
error_handler | function | No | Error handling function for this router |
Example:
local api_router = app:new_router({
prefix = "api",
no_errors = true
})
Removes a router. Accepts either the router_data object or the router prefix.
app:remove_router("api")
-- or
app:remove_router(api_router)
Router
Methods for working with routers and request handling.
Creates a new request handler.
Configuration Parameters:
prefix | string | No | Request prefix (added to router prefix) |
fun | function | Yes | Handler function: fun(args, client) |
no_errors | boolean | No | Overrides no_errors settings for this request |
async | boolean | No | Creating an asynchronous function |
error_handler | function | No | Error handling function for this request |
validate | table | No | Parameter validation: {["param"] = {"type1", "type2"}} |
responce_validate | table | No | Response validation: {"type1", "type2"} |
max_message_size | number | No | Maximum size of the request message in bytes |
message_penalty | string | No | Penalty for exceeding max_message_size ("timeout" or "closed", default: "timeout") |
timeout_duration | number | No | Duration of timeout penalty in seconds (used when message_penalty is "timeout") |
middlewares | table | No | List of middleware functions to process requests and responses middleware(context, is_pre) |
Example:
api_router:new({
prefix = "echo",
validate = {text = {"string", "nil"}},
responce_validate = {"string"},
fun = function(args, client)
return args.text or "no text provided"
end
})
Removes a request handler. Accepts either the req_data object or the request prefix.
api_router:remove("echo")
-- or
api_router:remove(echo_handler)
Lunac - Client Side
Core methods for working with the Lunac client.
Updates client state. Should be called in the main application loop.
function love.update(dt)
lunac.update(dt)
end
Disconnects from a server application. Accepts either the app_data object or the application name.
lunac.disconnect_to_app("My Client")
-- or
lunac.disconnect_to_app(client)
Connects to a server application.
Configuration Parameters:
name | string | No | Application name (for debugging) |
host | string | Yes | Server host (e.g. "127.0.0.1") |
port | number | Yes | Server port (e.g. 8080) |
no_errors | boolean | No | If true, errors won't stop the client |
error_handler | function | No | Function for error handling |
listener | function | No | Function to handle messages from client:send |
server | table | No | Luna object (if client and server are in the same app) |
reconnect_time | number | No | Reconnection time in seconds (if connection is lost) |
connect_server | function | No | Callback when successfully connected to server |
disconnect_server | function | No | Callback when disconnected from server: function(error_message) |
Example:
local client = lunac.connect_to_app({
name = "My Client",
host = "127.0.0.1",
port = 8080,
no_errors = true,
reconnect_time = 5, -- will attempt to reconnect every 5 seconds if disconnected
listener = function(message)
print("Received message:", message)
end,
server = luna -- only if server is in the same application,
connect_server = function()
print("Successfully connected to server!")
end,
disconnect_server = function(err)
print("Disconnected from server:", err)
end
})
Request Methods
Methods for interacting with the server.
Sends a request to the server and waits for response.
Parameters:
path | string | Yes | Request path (router_prefix/request_prefix) |
args | table | No | Request parameters |
timeout | number | No | Response timeout (default 5 seconds) |
Example:
local response = client:fetch("api/echo", {text = "Hello"}, 3)
print(response) -- "Hello"
Asynchronously sends a request.
Parameters:
path | string | Yes | Request path (router_prefix/request_prefix) |
callback | function | Yes | Called when the request has returned a response. callback(data, err) |
args | table | No | Request parameters |
Example:
client:noawait_fetch("api/log", {message = "Debug info"})