Thanks Stefan. Have you got that working with an AJAX call? I am trying to detect mouse events and fire them back to the app quite rapidly. All the pieces seem to work but the whole is less than the sum of the parts, for some reason.
I have found this post: http://forums.coronalabs.com/topic/20108-web-popups/ which includes a link to this blog page: http://www.think-techie.com/2012/05/corona-sdk-how-to-communicate-between.html
There, the code is explaining how to create a small web server in the Corona app with a web page which fires data to the server. I found another post (w3schools I think) which could detect mouse events and update the page with the mouse location.
I merged these examples together to try to send the mouse location to the Lua app but that one join seems to break. Just nothing happens. Here’s my code:
main.lua:
-- Corona SDK: Example how to interact between JS and Lua -- Possible solution for the URL bug in Android honeycomb and ICS -- https://code.google.com/p/xui-sdk/source/browse/#svn%2Fsrc%2FLuaToHTML%253Fstate%253Dclosed local isSimulator = "simulator" == system.getInfo("environment") if not isSimulator then display.setStatusBar( display.HiddenStatusBar ) end if system.getInfo( "platformName" ) == "Mac OS X" then isSimulator = false; end -- Parse values from webpopup function getArgs( query ) local parsed = {} local pos = 0 query = string.gsub(query, "&", "&") query = string.gsub(query, "<", "\<") query = string.gsub(query, ">", "\>") local function ginsert(qstr) local first, last = string.find(qstr, "=") if first then parsed[string.sub(qstr, 0, first-1)] = string.sub(qstr, first+1) end end while true do local first, last = string.find(query, "&", pos) if first then ginsert(string.sub(query, pos, first-1)); pos = last+1 else ginsert(string.sub(query, pos)); break; end end return parsed end -- Write values to be used by webpopup function setArgs( args ) local path = system.pathForFile( "args.json", system.DocumentsDirectory ) local file, errStr = io.open( path, "w+b" ) if file then local newstr = "" for k,v in pairs(args) do if k ~= nil and v ~= nil then if newstr ~= "" then newstr = newstr .. ", " end local val = "" if type(v) == "boolean" or tonumber(v) ~= nil then val = tostring(v) else val = "\"" .. v .. "\"" end newstr = newstr .. "\"" .. k .. "\": " .. val end end local content = "{ " .. newstr .. " }" file:write( content ) file:close() return true end end -- Thank you Matthew Pringle for this piece of code -- http://developer.anscamobile.com/reference/index/networkrequest local function createTCPServer( port ) local socket = require("socket") -- Create Socket local tcpServerSocket , err = socket.tcp() local backlog = 5 -- Check Socket if tcpServerSocket == nil then return nil , err end -- Allow Address Reuse tcpServerSocket:setoption( "reuseaddr" , true ) -- Bind Socket local res, err = tcpServerSocket:bind( "\*" , port ) if res == nil then return nil , err end -- Check Connection res , err = tcpServerSocket:listen( backlog ) if res == nil then return nil , err end -- Return Server return tcpServerSocket end -- code from http://developer.anscamobile.com/node/2937 function copyFile( srcName, srcPath, dstName, dstPath, overwrite ) -- assume no errors local results = true -- Copy the source file to the destination file local rfilePath = system.pathForFile( srcName, srcPath ) local wfilePath = system.pathForFile( dstName, dstPath ) local rfh = io.open( rfilePath, "rb" ) local wfh = io.open( wfilePath, "wb" ) if not wfh then print( "writeFileName open error!") results = false else -- Read the file from the Resource directory and write it to the destination directory local data = rfh:read( "\*a" ) if not data then print( "read error!" ) results = false -- error else if not wfh:write( data ) then print( "write error!" ) results = false -- error end end end -- Clean up our file handles rfh:close() wfh:close() return results end local function doStuff( args ) setArgs( args ) if runTCPServer ~= nil then Runtime:removeEventListener( "enterFrame" , runTCPServer ) end runTCPServer = function() tcpServer:settimeout( 0 ) local tcpClient , \_ = tcpServer:accept() if tcpClient ~= nil then local tcpClientMessage , \_ = tcpClient:receive('\*l') if tcpClient ~= nil then tcpClient:close() end if ( tcpClientMessage ~= nil ) then local myMessage = tcpClientMessage local event = {} local xArgPos = string.find( myMessage, "?" ) if xArgPos then local newargs = getArgs(string.sub( myMessage, xArgPos+1 )) if newargs.shouldLoad == nil or newargs.shouldLoad == "false" then native.cancelWebPopup() else -- do some stuff ... local url = require("socket.url") local str = url.unescape(newargs.arg) print("Value from HTML:" .. (str)) -- send some dumb stuff newargs.arg = tostring(os.date( "\*t" )) setArgs(newargs) -- or you can use send but then you have to re-implement a -- new parser. Note: dont close client before this line -- tcpClient:send( "ssssss" .. "\n") end end end end end if tcpServer == nil then tcpServer, \_ = createTCPServer( "8087" ) end Runtime:addEventListener( "enterFrame" , runTCPServer ) local options = { --hasBackground = false, baseUrl = system.DocumentsDirectory, --urlRequest = function( event ) return true end } -- native.showWebPopup("index.html", options ) webView = native.newWebView( display.actualContentWidth/2, display.actualContentHeight/2, display.actualContentWidth, display.actualContentHeight ) webView:request( "index.html", system.ResourceDirectory ) end -- On my case I use uncompress a tar file containing all the page assets -- This illustrates a simple scenario copyFile( "index.html", system.ResourcesDirectory, "index.html", system.DocumentsDirectory ) local args = {} args.arg = "Hello" doStuff( args )
index.html:
\<!doctype html\> \<html lang="en"\> \<head\> \<meta charset="utf-8"\> \<title\>mousemove demo\</title\> \<style\> div { width: 220px; height: 170px; margin: 10px 50px 10px 10px; background: yellow; border: 2px groove; float: right; } p { margin: 0; margin-left: 10px; color: red; width: 220px; height: 120px; padding-top: 70px; float: left; font-size: 14px; } span { display: block; } \</style\> \<script type="text/javascript"\> var urlVars = {} function getArgs(){ try { var xobj = new XMLHttpRequest(); xobj.overrideMimeType("application/json"); xobj.open('GET', "args.json", false); xobj.send(null); return eval("(" + xobj.responseText + ")"); } catch(err){} return null; } function getVariable( name, defaultValue ){ try { if (urlVars[name] == null) return defaultValue; return urlVars[name]; } catch(err){} return defaultValue; } function update( ){ urlVars = getArgs(); var arg = getVariable( 'arg', 1 ); document.getElementById("method").style.display = 'block'; document.getElementById('method').innerHTML = "Corona p-value: " + arg; return true } function send( params ){ var xobj = new XMLHttpRequest(); xobj.open('GET', "http://127.0.0.1:8087?" + params, true); xobj.onreadystatechange = update( ); xobj.send(null); return true } try{ window.onload = function () { update(); }; } catch(err){ } \</script\> \<script src="jquery-2.1.3.min.js"\>\</script\> \</head\> \<body\> \<p\> \<span\>Move the mouse over the div.\</span\> \<span\> \</span\> \</p\> \<div\>\</div\> \<a href="test"\>LINK\</a\> \<script\> $( "div" ).mousemove(function( event ) { var pageCoords = "( " + event.pageX + ", " + event.pageY + " )"; var clientCoords = "( " + event.clientX + ", " + event.clientY + " )"; $( "span:first" ).text( "page: " + pageCoords ); send('arg=1&shouldLoad=false&'); // DOES NOT SEEM TO DO ANYTHING AT ALL!!! $( "span:last" ).text( "Xclient: " + clientCoords ); }); \</script\> \<h1\>Hello World\</h1\> \<h2 id="method" style="display:none;"\>\</h2\> \<button onclick="send('arg=1&shouldLoad=true&')"\>send 1 to lua\</button\> \<button onclick="send('arg=one&shouldLoad=true&')"\>send one to lua\</button\> \<button onclick="send('arg=1&shouldLoad=false&')"\>close\</button\> \</body\> \</html\>
I also have jquery-2.1.3.min.js in the same app folder for good measure.