Network (rnet)
Subscribing
React to packets as they pass through.
A handler receives a packet object. Each handler call runs in its own coroutine, so you can use task.wait inside it. Errors inside a handler are printed as rnet handler error: ....
Every subscribing function returns a connection table with Disconnect() (also disconnect() and Destroy()).
rnet.on(pattern, fn, dir)
Calls fn(packet) for every matching packet. Alias: rnet.hookpacket.
local conn = rnet.on("set_health", function(p)
print("health", p.health, "food", p.food)
end, "in")
-- later
conn.Disconnect()
rnet.onOut(pattern, fn)
Same as rnet.on(pattern, fn, "out").
rnet.onIn(pattern, fn)
Same as rnet.on(pattern, fn, "in").
rnet.once(pattern, fn, dir)
Like on, but disconnects after the first packet.
rnet.intercept(pattern, fn, dir)
Lets you change or drop packets before they go on.
fn(packet)can edit fields (p.x = 1).- Return
falseto drop the packet. - Return anything else to let it continue.
- The packet is held for at most 300 ms. If your function is slower, or errors, the packet goes on unchanged.
- If you call
p:send(),p:release()orp:drop()yourself, it is not released again afterwards.
Returns an object with Disconnect().
rnet.intercept("chat_command", function(p)
if p.command:find("^home") then
return false -- drop it
end
end, "out")
rnet.wait(pattern, timeout = 5, dir)
Pauses until a matching packet arrives and returns it, or returns nil after timeout seconds. The returned packet is kept alive, so call packet:free() when you are done with it.
local p = rnet.wait("set_health", 10, "in")
if p then
print(p.health)
p:free()
end
rnet.log(pattern = "*", dir)
Prints each matching packet to the console as [direction] name{field=value, ...}.
rnet.record(pattern = "*", max = 50, dir)
Keeps the last max matching packets. Returns a table:
| Member | Meaning |
|---|---|
list() | The array of recorded packets, oldest first |
stop() | Stops recording and frees the packets |
Signal-style access
Three lazily created signals are available on rnet. Each subscribes to everything when first used:
| Signal | Packets |
|---|---|
rnet.Packet | Both directions |
rnet.Outgoing | Sent to the server |
rnet.Incoming | Received from the server |
rnet.Outgoing:Connect(function(p)
print(p.name)
end)