diff --git a/scripts/http/webcommands/minetest_webcommands.js b/scripts/http/webcommands/minetest_webcommands.js new file mode 100644 index 0000000..a574490 --- /dev/null +++ b/scripts/http/webcommands/minetest_webcommands.js @@ -0,0 +1,31 @@ +// listen to web request and pass it to minetest, rnd 2018 + +const http = require('http'); + +const hostname = '192.168.0.10' //write address of your router (it will be accessible from internet then if you open firewall for nodejs process) +const port = 80; + +var webreq = "" + +// take request from web and pass it to minetest +const server = http.createServer((req, res) => { + res.statusCode = 200; + res.setHeader('Content-Type', 'text/plain'); + + if (req.url == '/favicon.ico') return // prevent passing this as request + + var pos = (req.url).indexOf("/MT"); + if (pos >=0) { // did request come from minetest? then answer with latest request + res.write(webreq);webreq = "";res.end();return + } + + //process web request and store it + var ip = req.connection.remoteAddress; + webreq = ip + ' ' + req.url + res.write('request received: ' + webreq);res.end(); // acknowledge request + return +}); + +server.listen(port, hostname, () => { + console.log(`Server running at http://${hostname}:${port}/`); +}); \ No newline at end of file diff --git a/scripts/http/webcommands/webcommands.lua b/scripts/http/webcommands/webcommands.lua new file mode 100644 index 0000000..c068bb8 --- /dev/null +++ b/scripts/http/webcommands/webcommands.lua @@ -0,0 +1,31 @@ +-- webcommands : access url like: 192.168.0.10/hello_world + +--[[ instructions: + 1.download nodejs server from + https://nodejs.org/dist/v10.14.2/node-v10.14.2-win-x86.zip + 2. run nodejs server using run.bat + :loop + node --inspect myprogs/minetest_webcommands.js + goto loop + 3. run robot and type 'http://192.168.0.10:80/hello this is a test' into browser + (you need to write your router address here, i.e. ip accessible from internet OR lan address) +--]] + +if not fetch then + fetch = _G.basic_robot.http_api.fetch; + state = 0 -- ready to fetch new command +-- WARNING: this is run outside pcall and can crash server if errors! + result = function(res) -- res.data is string containing result + state = 0 + if not res.succeeded then self.label("#ERROR: data couldn't be downloaded :\n" .. minetest.serialize(res) ) return end + if res.data == "" then return end + local req = res.data; req = string.gsub(req,"%%20","S") + if res.data then self.label(os.clock() .. ' received cmd : ' .. res.data) end + end + +end + +if state == 0 then + fetch({url = "http://192.168.0.10/MT", timeout = 30}, result) + state = 1 +end \ No newline at end of file