OpenComputers Von Neumann Machine Programs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
963 B

1 year ago
  1. local computer = computer or require("computer")
  2. local event = event or require("event")
  3. local modem = component.proxy(component.list("modem")())
  4. local function unknown()
  5. -- do nothing
  6. end
  7. local http = {}
  8. local http.handlers = setmetatable({}, { __index = function() return unknown end })
  9. local function http.example()
  10. http.handlers["/example"] = function (...)
  11. -- "modem_message", local addr, remote addr, port, distance, "/example"
  12. tmp = {...}
  13. modem.send(tmp[3], tmp[4], "hello world")
  14. end
  15. end
  16. local function http.listen(port)
  17. modem.open(port)
  18. eventID = event.listen("modem_message", function (_, ...)
  19. -- event type, local addr, remote addr, port, distance, msg
  20. tmp = {...}
  21. -- Filter out messages coming from the wrong port
  22. if tmp[3] ~= port then return end
  23. -- Forward message to the correct handler
  24. http.handlers[tmp[5]](tmp)
  25. end)
  26. return eventID
  27. end