Added chat
This commit is contained in:
@@ -1,8 +1,19 @@
|
|||||||
|
local modem = peripheral.find("modem")
|
||||||
|
local PORT = 1
|
||||||
|
|
||||||
local function start()
|
local function start()
|
||||||
|
if modem then
|
||||||
|
modem.open(PORT)
|
||||||
|
modem.transmit(PORT, PORT, "User connected")
|
||||||
|
end
|
||||||
print("Chat program started.")
|
print("Chat program started.")
|
||||||
end
|
end
|
||||||
|
|
||||||
local function stop()
|
local function stop()
|
||||||
|
if modem then
|
||||||
|
modem.transmit(PORT, PORT, "User disconnected")
|
||||||
|
modem.close(PORT)
|
||||||
|
end
|
||||||
print("Chat program stopped.")
|
print("Chat program stopped.")
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -11,15 +22,36 @@ local function restart()
|
|||||||
start()
|
start()
|
||||||
end
|
end
|
||||||
|
|
||||||
local function main()
|
local function receiveLoop()
|
||||||
print("Chat main loop running...")
|
|
||||||
while true do
|
while true do
|
||||||
local event = os.pullEvent()
|
local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
|
||||||
-- Chat logic here
|
if channel == PORT then
|
||||||
if event == "char" then
|
local x, y = term.getCursorPos()
|
||||||
print("Char typed")
|
term.setCursorPos(1, y)
|
||||||
|
term.clearLine()
|
||||||
|
print("Received: " .. tostring(message))
|
||||||
|
write("> ")
|
||||||
end
|
end
|
||||||
end
|
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 }
|
return { start = start, stop = stop, restart = restart, main = main }
|
||||||
|
|||||||
Reference in New Issue
Block a user