Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local socket = require "socket"
- function generateCode()
- local request = http.get(
- "https://www.random.org/integers/?num=4&min=0&max=9&col=1&base=10&format=plain&rnd=new"
- )
- local text = request.readAll()
- return string.gsub(text, "\n", "")
- end
- local codes = {
- pool_door = generateCode()
- }
- print(textutils.serializeJSON(codes))
- function response(id, protocol, table)
- local location = table["location"]
- return codes[location]
- end
- while true do
- print("Listening on port " .. os.computerID())
- local id, protocol, table = socket.receive()
- print(string.format("id=%s protocol=%s table=%s", id, protocol, table))
- local response = response(id, protocol, table)
- local data = {success = false, error = "bad request"}
- if response ~= nil then
- data = {success = true, code = response}
- end
- print(data)
- socket.send(id, "code", data)
- end
- print("Server stopped")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement