{
"id": "271",
"prompt": "A heartbeat keep-alive: every 120 frames try host::net::send (it returns 1 if queued, 0 if the socket is closed or bad - only attempt it when status() says OPEN), tally SENT vs FAILED with a brief green/red flash strip under each counter, and draw a shrinking bar of frames until the next beat. Open the socket once behind an init latch; send's message argument is an integer pointer to a length-prefixed string in cartridge memory, same ABI as open.",
"solution_rl": "fn frame(t: i32) {\n host::display::clear(0x101014);\n if host::display::state_get(0) == 0 {\n host::display::state_set(1, host::net::open(\"wss://relay.example.net/heartbeat\"));\n host::display::state_set(0, 1);\n }\n let h: i32 = host::display::state_get(1);\n if t % 120 == 0 {\n let mut ok: i32 = 0;\n if h >= 0 && host::net::status(h) == 1 {\n ok = host::net::send(h, \"beat\");\n }\n if ok == 1 {\n host::display::state_set(2, host::display::state_get(2) + 1);\n host::display::state_set(4, 10);\n } else {\n host::display::state_set(3, host::display::state_get(3) + 1);\n host::display::state_set(5, 10);\n }\n }\n host::display::draw_string(100, 50, \"HEARTBEAT SENDER\", 0xffffff, 2);\n host::display::draw_string(80, 140, \"SENT\", 0x22bb44, 2);\n host::display::draw_number(80, 180, host::display::state_get(2), 0xffffff, 4);\n host::display::draw_string(300, 140, \"FAILED\", 0xbb3333, 2);\n host::display::draw_number(300, 180, host::display::state_get(3), 0xffffff, 4);\n let gflash: i32 = host::display::state_get(4);\n if gflash > 0 {\n host::display::fill_rect(80, 260, 120, 16, 0x22bb44);\n host::display::state_set(4, gflash - 1);\n }\n let rflash: i32 = host::display::state_get(5);\n if rflash > 0 {\n host::display::fill_rect(300, 260, 120, 16, 0xbb3333);\n host::display::state_set(5, rflash - 1);\n }\n let next: i32 = 120 - t % 120;\n host::display::draw_string(80, 330, \"NEXT BEAT\", 0x888888, 1);\n host::display::fill_rect(80, 350, next * 3, 8, 0x66ddff);\n host::display::present();\n}\n",
"tags": [
"net",
"heartbeat",
"ui"
]
}