updated
This commit is contained in:
@@ -1,19 +1,22 @@
|
||||
local modem = peripheral.find("modem")
|
||||
local PORT = 1
|
||||
local speaker = require("speaker-driver")
|
||||
local SERVER_PORT = 100
|
||||
local CLIENT_PORT = 101
|
||||
|
||||
local username = os.getComputerLabel() or ("User" .. os.getComputerID())
|
||||
|
||||
local function start()
|
||||
if modem then
|
||||
modem.open(PORT)
|
||||
modem.transmit(PORT, PORT, "User connected")
|
||||
modem.open(CLIENT_PORT)
|
||||
modem.transmit(SERVER_PORT, CLIENT_PORT, { type = "join", username = username })
|
||||
end
|
||||
print("Chat program started.")
|
||||
print("Chat program started as " .. username)
|
||||
end
|
||||
|
||||
local function stop()
|
||||
if modem then
|
||||
modem.transmit(PORT, PORT, "User disconnected")
|
||||
modem.close(PORT)
|
||||
modem.transmit(SERVER_PORT, CLIENT_PORT, { type = "leave", username = username })
|
||||
modem.close(CLIENT_PORT)
|
||||
end
|
||||
print("Chat program stopped.")
|
||||
end
|
||||
@@ -26,12 +29,19 @@ end
|
||||
local function receiveLoop()
|
||||
while true do
|
||||
local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
|
||||
if channel == PORT then
|
||||
if channel == CLIENT_PORT and type(message) == "table" then
|
||||
local x, y = term.getCursorPos()
|
||||
term.setCursorPos(1, y)
|
||||
term.clearLine()
|
||||
print("Received: " .. tostring(message))
|
||||
speaker.play("notification")
|
||||
|
||||
if message.type == "system" then
|
||||
print("[System] " .. message.content)
|
||||
speaker.play("notification")
|
||||
elseif message.type == "chat" then
|
||||
print(message.username .. ": " .. message.content)
|
||||
speaker.play("notification")
|
||||
end
|
||||
|
||||
write("> ")
|
||||
end
|
||||
end
|
||||
@@ -42,7 +52,7 @@ local function sendLoop()
|
||||
write("> ")
|
||||
local input = read()
|
||||
if modem then
|
||||
modem.transmit(PORT, PORT, input)
|
||||
modem.transmit(SERVER_PORT, CLIENT_PORT, { type = "chat", username = username, content = input })
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user