http#
| Function | Parameters | Returns | Description |
|---|---|---|---|
http.get(url, fn, headers?) | fn(body, status) | none | body is nil on failure. |
http.post(url, body, fn, headers?) | body: string or table (sent as JSON) | none | |
http.request(method, url, opts, fn) | opts: {headers = {...}, body = "..."} | none | http.put, http.delete |
http.get("https://api.example.com/data", function(body, status)
if status == 200 and body then
local data = json.decode(body)
print("Got:", data and data.value)
end
end, {["User-Agent"] = "LunaScript"})
http.post("https://api.example.com/score", {kills = 3}, function(body, status)
print("posted", status)
end)websocket#
websocket.connect(url, headers?) (also ws.connect) opens a ws:// or wss:// connection on its own thread. Callbacks run on the render thread on the next frame. Up to 16 sockets per script, messages up to 16 MB; sockets close when the script unloads.
| Function | Parameters | Returns | Description |
|---|---|---|---|
socket:send(data) | string or table (JSON) | bool | send_binary(bytes), send_json(table) |
socket:close(code?, reason?) | none | ||
socket:state() | string | connecting, open or closed. socket:is_open() | |
socket:on_open(fn) | socket | on_message(fn(data, binary)), on_close(fn(code, reason)), on_error(fn(text)), on("json", fn(table)) |
local socket = websocket.connect("wss://echo.websocket.org", {["User-Agent"] = "luna"})
socket:on_open(function()
socket:send({type = "hello", map = globals.mapname()})
end)
socket:on_message(function(data, binary)
print("received", data)
end)
socket:on_close(function(code, reason)
print("closed", code, reason)
end)
socket:on_error(function(err)
print("websocket error", err)
end)relay (Luna server)#
relay connects every Luna user to the same rooms and shared storage on lunacheat.codes, so scripts can talk to each other: shared markers, chats, leaderboards, synced settings. The connection opens on the first call, queues what you send until it is ready and reconnects by itself, rejoining your rooms.
| Function | Parameters | Returns | Description |
|---|---|---|---|
relay.join(room) | room: letters, digits, _ . : -, up to 64 | none | relay.leave(room) |
relay.send(room, data, echo?) | data: any JSON value | none | Delivered to everyone else in the room (and to you with echo). |
relay.on_message(fn) | fn(room, data, user, from) | none | on_join(fn(room, user, from)), on_leave, relay.on("open" | "close" | "welcome" | "error", fn) |
relay.get(key, fn) | fn(value, error) | none | Shared storage, one namespace per script name. |
relay.set(key, value, fn?) | value up to 16 KB, nil deletes | none | fn(ok, error) |
relay.members(room, fn) | fn(list) | none | List of {id, user}. |
relay.ping(fn) | fn(ms) | none | relay.is_connected(), relay.close(), relay.user = "name" |
Limits: 16 rooms per script, about 20 messages a second, 64 KB per message, 8 connections per IP.
relay.user = "luna user"
relay.join("global")
relay.on_message(function(room, data, user)
print(user .. " in " .. room .. ": " .. tostring(data.text))
end)
relay.send("global", {text = "hello"})
relay.get("visits", function(value)
relay.set("visits", (tonumber(value) or 0) + 1)
end)