Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local computerId = os.getComputerID()
- local monitor = peripheral.find("monitor")
- local modem = peripheral.find("modem")
- local function generateRandomHostname()
- local randomSuffix = ""
- local randomLength = 5
- local characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
- for i = 1, randomLength do
- local randomIndex = math.random(1, #characters)
- randomSuffix = randomSuffix .. string.sub(characters, randomIndex, randomIndex)
- end
- local hostname = "NetTchat-" .. randomSuffix
- return hostname
- end
- local function findEnderModem()
- for _, side in ipairs({"left", "right", "top", "bottom", "front", "back"}) do
- if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
- return side
- end
- end
- error("No modem found")
- end
- local function displayServerInfo(monitor, computerId, hostname, protocol)
- monitor.clear()
- monitor.setCursorPos(1, 1)
- monitor.setTextScale(1)
- local title = string.format("Serveur NetTchat démarré sur le Computer #%s :", computerId)
- local underline = string.rep("-", #title)
- monitor.write(title)
- monitor.setCursorPos(1, 2)
- monitor.write(underline)
- monitor.setCursorPos(1, 4)
- monitor.write(string.format("Hostname : %s", hostname))
- monitor.setCursorPos(1, 5)
- monitor.write(string.format("Protocol : %s", protocol))
- end
- local function updateClientList(monitor, clients)
- monitor.clear()
- monitor.setCursorPos(1, 1)
- monitor.setTextScale(1)
- local title = string.format("Serveur NetTchat démarré sur le Computer #%s :", computerId)
- local underline = string.rep("-", #title)
- monitor.write(title)
- monitor.setCursorPos(1, 2)
- monitor.write(underline)
- monitor.setCursorPos(1, 4)
- monitor.write(string.format("Hostname : %s", hostname))
- monitor.setCursorPos(1, 5)
- monitor.write(string.format("Protocol : %s", protocol))
- monitor.setCursorPos(1, 7)
- monitor.write("Clients connectés:")
- monitor.setCursorPos(1, 8)
- monitor.write(string.rep("-", 20))
- local y = 9
- for clientId, client in pairs(clients) do
- monitor.setCursorPos(1, y)
- if client.connected then
- monitor.write(client.name .. " (Online)")
- else
- monitor.write(client.name .. " (Disconnected)")
- end
- y = y + 1
- end
- end
- local function startOnServer()
- term.clear()
- local protocol = "chat"
- local hostname = generateRandomHostname()
- local credits = "NetTchat Server Edition, created and designed by _Tsyke_ | Ver: 0.3"
- local creditsScale = 0.8
- monitor.setTextScale(creditsScale)
- local width, height = monitor.getSize()
- local creditsX = math.floor((width - #credits) / 2)
- local creditsY = math.floor(height / 2)
- monitor.clear()
- monitor.setCursorPos(creditsX, creditsY)
- monitor.write(credits)
- os.sleep(5)
- displayServerInfo(monitor, computerId, hostname, protocol)
- local modemSide = findEnderModem()
- rednet.open(modemSide)
- rednet.host(protocol, hostname)
- print("Serveur démarré. Attente des connexions...")
- print("Protocole: " .. protocol)
- print("Hostname: " .. hostname)
- print("Modem sur: " .. modemSide)
- local clients = {}
- local clientLastPing = {}
- local function checkClientPing()
- while true do
- local currentTime = os.time()
- for clientId, client in pairs(clients) do
- if client.connected then
- rednet.send(clientId, "Ping", protocol)
- local _, message = rednet.receive(protocol, 2) -- Attend une réponse pendant 2 secondes
- if not message or message ~= "Pong" then
- print("Client déconnecté: #" .. clientId)
- clients[clientId] = nil
- updateClientList(monitor, clients) -- Mettre à jour la liste des clients sur le moniteur
- end
- end
- end
- os.sleep(5)
- end
- end
- parallel.waitForAll(
- function()
- while true do
- local senderId, message, receivedProtocol = rednet.receive(protocol)
- if receivedProtocol == protocol then
- if message == "Demande de connexion" then
- clients[senderId] = { name = "Client #" .. senderId, connected = true }
- updateClientList(monitor, clients)
- print("Client connecté: #" .. senderId)
- rednet.send(senderId, "Connexion acceptée", protocol)
- elseif message == "Ping" then
- -- Le serveur ne devrait pas recevoir "Ping" directement ici, donc rien n'est fait ici.
- else
- print("Message reçu de #" .. senderId .. ": " .. message)
- end
- end
- end
- end,
- checkClientPing
- )
- end
- startOnServer()
Advertisement
Add Comment
Please, Sign In to add comment