Added chat

This commit is contained in:
2025-12-12 14:36:18 -05:00
parent f6dd0bd669
commit 4e6b83a456

View File

@@ -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 }