Advertisement
Feddis08

Client2

Sep 26th, 2022 (edited)
815
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.12 KB | Gaming | 0 0
  1. print("client")
  2. local component = require("component")
  3. local computer = require("computer")
  4. local event = require("event")
  5. local m = component.modem
  6. local spaceing = "}|}!"
  7. local self_address
  8. local connectedToRouter = false
  9. local router_address = ""
  10. m.open(5)
  11. computer.beep()
  12.  
  13. function split(s, delimiter)
  14.     result = {};
  15.     for match in (s..delimiter):gmatch("(.-)"..delimiter) do
  16.         table.insert(result, match);
  17.     end
  18.     return result;
  19. end
  20. function sendMessage(to, message)
  21.     m.send(router_address, 5, "send" .. spaceing .. to .. spaceing .. message)
  22.     end
  23. function sendBroadcast(message)
  24.     m.send(router_address, 5, "broadcast" .. spaceing .. message)
  25.     end
  26. function listenToRouter()
  27.     local _, _, from, port, _, message = event.pull("modem_message")
  28.     if port == 5 then
  29.         command = split(message, spaceing)
  30.         if command[1] == "transmit" then
  31.             if command[2] == "send" then
  32.                 print("got message " .. command[4] .. " from " .. command[3])
  33.                 end
  34.             if command[2] == "broadcast" then
  35.                 print("got broadcast_message " .. command[4] .. " from " .. command[3])
  36.                 end
  37.             end
  38.         if command[1] == "connect" then
  39.             if command[2] == "2" then
  40.                 connectToRouter()
  41.                 end
  42.             end
  43.         end
  44.     end
  45.  
  46. function connectToRouter()
  47.     print("searching for router...")
  48.     m.broadcast(5, "connect" .. spaceing)
  49.     local _, _, from, port, _, message = event.pull("modem_message")
  50.     print(port .. message)
  51.     if port == 5 then
  52.         print(from .. " " .. message)
  53.         command = split(message, spaceing)
  54.         if command[1] == "connect" then
  55.             if command[2] == "1" then
  56.                 router_address = from
  57.                 self_address = command[3]
  58.                 connectedToRouter = true
  59.                 print("connected with " .. from .. " !")
  60.                 end
  61.             end
  62.         end
  63.     end
  64. connectToRouter()
  65. connectToRouter()
  66. sendMessage(self_address, "Hallo")
  67. listenToRouter()
  68. sendBroadcast("Hallo2")
  69. listenToRouter()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement