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")
- if modem == nil then
- error("Aucun modem disponible.")
- return
- end
- local function findModem()
- local sides = { "top", "bottom", "left", "right", "back", "front" }
- for _, side in ipairs(sides) do
- if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
- return side
- end
- end
- return nil
- end
- local function connectToServer(protocol, hostname)
- local modemSide = findModem()
- rednet.open(modemSide)
- term.clear()
- print(string.format("Tentative de connexion à %s sur le protocol : %s...", hostname, protocol))
- local serverId = rednet.lookup(protocol)
- if serverId then
- rednet.send(serverId, "Demande de connexion", protocol) -- Envoyer une demande de connexion au serveur
- return serverId
- else
- monitor.clear()
- monitor.setCursorPos(1, 1)
- monitor.setTextScale(1)
- local title = string.format("[ECHEC] Client NetTchat Computer #%s :", os.getComputerID())
- local underline = string.rep("-", #title)
- monitor.write(title)
- monitor.setCursorPos(1, 2)
- monitor.write(underline)
- monitor.setCursorPos(1, 4)
- monitor.write(string.format("Connexion : Aucune"))
- monitor.setCursorPos(1, 5)
- monitor.write(string.format("Erreur : Aucun serveur trouvé pour le protocole : " .. protocol))
- print("Aucun serveur trouvé pour le protocole : " .. protocol)
- return false
- end
- end
- local function displayStartupMessage()
- local message = "NetTchat Client Edition, created and designed by _Tsyke_ | Ver: 0.1"
- local scale = .8
- monitor.setTextScale(scale)
- monitor.clear()
- local width, height = monitor.getSize()
- local x = math.floor((width - #message) / 2)
- local y = math.floor(height / 2)
- monitor.setCursorPos(x, y)
- monitor.write(message)
- sleep(5)
- monitor.clear()
- monitor.setTextScale(1)
- end
- local function updateMonitor(hostname, protocol)
- monitor.clear()
- monitor.setCursorPos(1, 1)
- monitor.setTextScale(1)
- local title = string.format("Client NetTchat Computer #%s :", os.getComputerID())
- 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 startOnClient()
- displayStartupMessage()
- term.clear()
- term.setCursorPos(1, 1)
- print("=== Configuration ===")
- print("Entrez le nom du protocole :")
- term.setCursorPos(1, 4)
- write("> ")
- local selectedProtocol = read()
- print("Entrez le nom d'hôte du serveur:")
- term.setCursorPos(1, 6)
- write("> ")
- local selectedHostname = read()
- local serverId = connectToServer(selectedProtocol, selectedHostname)
- if serverId then
- print("Connexion établie avec le serveur #" .. serverId)
- updateMonitor(selectedHostname, selectedProtocol)
- while true do
- rednet.send(serverId, "Ping", selectedProtocol)
- local senderId, message, protocol = rednet.receive(selectedProtocol, 5)
- if senderId and message == "Pong" then
- print(string.format("Réponse reçue de %d : %s", senderId, message))
- else
- print("Aucune réponse reçue dans les 5 secondes. Déconnexion...")
- return
- end
- sleep(5)
- end
- else
- print("Impossible de se connecter au serveur.")
- end
- end
- startOnClient()
Advertisement
Add Comment
Please, Sign In to add comment