Utilities

HTTP and websockets

Make HTTP requests and talk to other players through the Luna relay.

http#

FunctionParametersReturnsDescription
http.get(url, fn, headers?)fn(body, status)nonebody 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 = "..."}nonehttp.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.

FunctionParametersReturnsDescription
socket:send(data)string or table (JSON)boolsend_binary(bytes), send_json(table)
socket:close(code?, reason?)none
socket:state()stringconnecting, open or closed. socket:is_open()
socket:on_open(fn)socketon_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.

FunctionParametersReturnsDescription
relay.join(room)room: letters, digits, _ . : -, up to 64nonerelay.leave(room)
relay.send(room, data, echo?)data: any JSON valuenoneDelivered to everyone else in the room (and to you with echo).
relay.on_message(fn)fn(room, data, user, from)noneon_join(fn(room, user, from)), on_leave, relay.on("open" | "close" | "welcome" | "error", fn)
relay.get(key, fn)fn(value, error)noneShared storage, one namespace per script name.
relay.set(key, value, fn?)value up to 16 KB, nil deletesnonefn(ok, error)
relay.members(room, fn)fn(list)noneList of {id, user}.
relay.ping(fn)fn(ms)nonerelay.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)