Advertisement
Guest User

httppull

a guest
Aug 28th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.95 KB | None | 0 0
  1. endpoint = "http://[::1]:8092/".. os.getComputerID()
  2. http.request(endpoint)
  3. modem = peripheral.wrap("top")
  4. if modem ~= nil then
  5.   modem.open(rednet.CHANNEL_REPEAT)
  6. end
  7. local running = true
  8.  
  9. while running do
  10.   local event, url, sourceText, rc, msg = os.pullEvent()
  11.   if event == "http_success" then
  12.     local response = sourceText.readAll()
  13.     sourceText.close()
  14.     if response ~= "" then
  15.       print(response)
  16.       local t = textutils.unserialize(response)
  17.       if t ~= nil then
  18.         print("Sending to "..t.msg.nRecipient)
  19.         modem.transmit(t.msg.nRecipient,t.rc,t.msg)
  20.       end    
  21.     end
  22.     http.request(endpoint)
  23.   elseif event == "http_failure" then
  24.     print("Lost communication with endpoint")
  25.     modem.close(rednet.CHANNEL_REPEAT)
  26.     running = false
  27.   elseif event == "modem_message" then
  28.     print("Relaying from "..rc)
  29.     t = {}
  30.     t.rc = rc
  31.     t.msg = msg
  32.     http.post(endpoint,textutils.serialize(t))
  33.   end
  34. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement