aboutsummaryrefslogtreecommitdiff
path: root/example
diff options
context:
space:
mode:
Diffstat (limited to 'example')
-rwxr-xr-xexample/chat-client.lua23
-rwxr-xr-xexample/dump-traffic.lua48
-rwxr-xr-xexample/print-node.lua17
3 files changed, 49 insertions, 39 deletions
diff --git a/example/chat-client.lua b/example/chat-client.lua
index c1828ec..889369f 100755
--- a/example/chat-client.lua
+++ b/example/chat-client.lua
@@ -2,19 +2,24 @@
local escapes = require("escapes")
local client = require("client")()
-client:subscribe("chat_msg")
+client:enable("pkts")
+client.pkts:subscribe("chat_msg")
+
client:connect()
-while not hydra.canceled() do
- local pkt, interrupt = client:poll(1)
+while true do
+ local evt = client:poll(1)
- if pkt then
- print(escapes.strip_all(pkt.text))
- elseif interrupt then
- client:send("chat_msg", {msg = "test"})
- else
- print("disconnected")
+ if not evt then
+ break
+ end
+
+ if not evt or evt.type == "interrupt" or evt.type == "disconnect" then
break
+ elseif evt.type == "pkt" then
+ print(escapes.strip_all(evt.pkt_data.text))
+ elseif evt.type == "timeout" then
+ client:send("chat_msg", {msg = "test"})
end
end
diff --git a/example/dump-traffic.lua b/example/dump-traffic.lua
index 5dc83b6..f003c74 100755
--- a/example/dump-traffic.lua
+++ b/example/dump-traffic.lua
@@ -3,7 +3,9 @@ local escapes = require("escapes")
local base64 = require("base64")
local client = require("client")()
-client:wildcard(true)
+client:enable("pkts")
+client.pkts:wildcard(true)
+
client:connect()
local function dump(val, indent)
@@ -16,41 +18,43 @@ local function dump(val, indent)
end
print(val)
else
- print(val._type or "")
+ print()
local idt = (indent or "") .. " "
for k, v in pairs(val) do
- if k ~= "_type" then
- io.write(idt .. k .. " ")
- dump(v, idt)
- end
+ io.write(idt .. k .. " ")
+ dump(v, idt)
end
end
end
-while not hydra.canceled() do
- local pkt, interrupt = client:poll()
+while true do
+ local evt = client:poll()
- if pkt then
- if pkt._type == "srp_bytes_salt_b" then
- pkt.b = base64.encode(pkt.b)
- pkt.salt = base64.encode(pkt.salt)
+ if not evt or evt.type == "disconnect" or evt.type == "interrupt" then
+ break
+ elseif evt.type == "error" then
+ print(evt.error)
+ elseif evt.type == "pkt" then
+ local type, data = evt.pkt_type, evt.pkt_data
+
+ if type == "srp_bytes_salt_b" then
+ data.b = base64.encode(data.b)
+ data.salt = base64.encode(data.salt)
end
- if pkt._type == "chat_msg" then
- pkt.text = escapes.strip_all(pkt.text)
+ if type == "chat_msg" then
+ data.text = escapes.strip_all(data.text)
end
- if pkt._type == "blk_data" then
- pkt.blk.param0 = {}
- pkt.blk.param1 = {}
- pkt.blk.param2 = {}
+ if type == "blk_data" then
+ data.blk.param0 = {}
+ data.blk.param1 = {}
+ data.blk.param2 = {}
end
- dump(pkt)
- elseif not interrupt then
- print("disconnected")
- break
+ io.write(type)
+ dump(data)
end
end
diff --git a/example/print-node.lua b/example/print-node.lua
index 3cf514e..d72f474 100755
--- a/example/print-node.lua
+++ b/example/print-node.lua
@@ -1,20 +1,21 @@
#!/usr/bin/env hydra-dragonfire
local client = require("client")()
-client:enable("map")
-client:subscribe("move_player")
+client:enable("pkts", "map")
+client.pkts:subscribe("move_player")
+
client:connect()
local pos
-while not hydra.canceled() do
- local pkt, interrupted = client:poll(1)
+while true do
+ local evt = client:poll(1)
- if pkt then
- pos = (pkt.pos / hydra.BS + vec3(0, -1, 0)):round()
- elseif not interrupted then
+ if not evt or evt.type == "disconnect" or evt.type == "interrupt" then
break
- elseif pos then
+ elseif evt.type == "pkt" then
+ pos = (evt.pkt_data.pos / hydra.BS + vec3(0, -1, 0)):round()
+ elseif evt.type == "timeout" and pos then
local node = client.map:node(pos)
print(pos, node and node.param0)
end