From 4e6b83a45609eb7a40f1593a3aac9c71583130a2 Mon Sep 17 00:00:00 2001 From: itzmarkoni Date: Fri, 12 Dec 2025 14:36:18 -0500 Subject: [PATCH] Added chat --- programs/chat.lua | 44 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/programs/chat.lua b/programs/chat.lua index 4f38169..24f7b62 100644 --- a/programs/chat.lua +++ b/programs/chat.lua @@ -1,8 +1,19 @@ +local modem = peripheral.find("modem") +local PORT = 1 + local function start() + if modem then + modem.open(PORT) + modem.transmit(PORT, PORT, "User connected") + end print("Chat program started.") end local function stop() + if modem then + modem.transmit(PORT, PORT, "User disconnected") + modem.close(PORT) + end print("Chat program stopped.") end @@ -11,15 +22,36 @@ local function restart() start() end -local function main() - print("Chat main loop running...") +local function receiveLoop() while true do - local event = os.pullEvent() - -- Chat logic here - if event == "char" then - print("Char typed") + local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message") + if channel == PORT then + local x, y = term.getCursorPos() + term.setCursorPos(1, y) + term.clearLine() + print("Received: " .. tostring(message)) + write("> ") end end end +local function sendLoop() + while true do + write("> ") + local input = read() + if modem then + modem.transmit(PORT, PORT, input) + end + end +end + +local function main() + if not modem then + print("No modem attached") + while true do os.pullEvent() end + end + + parallel.waitForAny(receiveLoop, sendLoop) +end + return { start = start, stop = stop, restart = restart, main = main }