Files
nova-corp/programs/chat.lua
2025-12-12 15:52:18 -05:00

60 lines
1.3 KiB
Lua

local modem = peripheral.find("modem")
local PORT = 1
local speaker = require("speaker")
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
local function restart()
stop()
start()
end
local function receiveLoop()
while true do
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))
speaker.play("notification")
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 }