Events and networking
Local events, client to server and server to client, and why every network event is declared with sixm.red.
Everything in SixM works through events: a name ("tienda:comprar") and some arguments. There are three directions.
| From → to | Send | Receive |
|---|---|---|
| same side (server → server, client → client) | sixm.emitir(evento, ...) | sixm.on(evento, fn(...)) |
| client → server | sixm.al_servidor(evento, ...) | sixm.on(evento, fn(id, ...)), after sixm.red(evento) |
| server → client | sixm.al_cliente(id, evento, ...) (-1 = everyone) | sixm.on(evento, fn(...)) |
Prefix your events with the resource's name (tiendas:comprar, tel:mensaje) so they don't clash with others.
Client → server
The server has to declare every event it accepts from clients. Without sixm.red, the server drops it: this way a player can't fire other resources' internal events.
-- server
sixm.red("taller:reparar")
sixm.on("taller:reparar", function(id, precio)
-- id: who sent it. The server sets it; the client can't fake it.
end)
-- client
sixm.al_servidor("taller:reparar", 250)
Always validate what arrives from the client: types, ranges and whether that player can do it. The price, the distance or the permission are decided by the server, not the client.
sixm.on("taller:reparar", function(id, precio)
if type(precio) ~= "number" or precio < 0 then return end
local j = sixm.jugador(id)
if not j or not cerca_del_taller(j) then return end
-- ...
end)
The server also limits how many events per second it accepts from each player.
Server → client
sixm.al_cliente(id, "taller:listo", { coste = 250 }) -- to one
sixm.al_cliente(-1, "anuncio", "Reinicio en 5 minutos") -- to everyone
-- client
sixm.on("taller:listo", function(d) print("pagado: " .. d.coste) end)
Network events arrive in order and without loss (they go through SixM's reliable channel), even though they travel over UDP.
Same side
sixm.emitir reaches all resources on that side, including the one sending it. It is useful for notifying other resources without coupling them through exports:
-- sixm-rp does this when a character is chosen:
sixm.emitir("rp:personaje_listo", id, n)
-- any other server resource:
sixm.on("rp:personaje_listo", function(id, n) print(id .. " juega con el personaje " .. n) end)
Waiting inside a handler
A handler can use sixm.esperar: it runs in its own thread.
sixm.on("taller:reparar", function(id)
sixm.al_cliente(id, "taller:empieza")
sixm.esperar(3000)
sixm.al_cliente(id, "taller:listo")
end)
Events of the bundled resources
| Event | Side | Emitted by | Arguments |
|---|---|---|---|
rp:personaje_listo | server | sixm-rp | id, n (the character's number) |
rp:uso | server | sixm-rp | id, objeto (used an inventory item) |
sixm:idioma_listo | server | sixm-base | id, idioma ("en" or "es"): that player's language has just arrived or changed |
sixm:aviso | client | anyone (drawn by sixm-base) | texto, tipo |
gestos:hacer | client | anyone (listened by gestos) | gesto (name of a gesture, or nil to stop it): performs it on your own ped |
System events (jugador:entra, jugador:sale, recurso:arranca, recurso:para) are in the common API.