diff --git a/xwords4/wasm/Makefile b/xwords4/wasm/Makefile index 1b410b991..a6d3fefd1 100644 --- a/xwords4/wasm/Makefile +++ b/xwords4/wasm/Makefile @@ -46,6 +46,7 @@ DEFINES += -DXWFEATURE_STREAMREF DEFINES += -DNATIVE_NLI DEFINES += -DDEBUG_REF DEFINES += -Wno-switch +DEFINES += -DGITREV=\"$(shell git describe --tags --dirty)\" ifeq ($(MEMDEBUG),TRUE) DEFINES += -DMEM_DEBUG -DDEBUG -O0 diff --git a/xwords4/wasm/main.c b/xwords4/wasm/main.c index 10419cd6e..904c45bde 100644 --- a/xwords4/wasm/main.c +++ b/xwords4/wasm/main.c @@ -156,9 +156,11 @@ EM_JS(void, call_get_string, (const char* msg, const char* dflt, nbGetString( jsMgs, jsDflt, proc, closure ); } ); -EM_JS(void, call_haveDevID, (void* closure, const char* devid, StringProc proc), { - onHaveDevID(closure, UTF8ToString(devid), proc); -}); +EM_JS(void, call_haveDevID, (void* closure, const char* devid, + const char* gitrev, int now, StringProc proc), { + let jsgr = UTF8ToString(gitrev); + onHaveDevID(closure, UTF8ToString(devid), jsgr, now, proc); + }); EM_JS(bool, call_mqttSend, (const char* topic, const uint8_t* ptr, int len), { let topStr = UTF8ToString(topic); @@ -550,7 +552,8 @@ initDeviceGlobals( Globals* globals ) XP_UCHAR buf[32]; XP_SNPRINTF( buf, VSIZE(buf), MQTTDevID_FMT, devID ); XP_LOGFF( "got mqtt devID: %s", buf ); - call_haveDevID( globals, buf, onConflict ); + int now = dutil_getCurSeconds( globals->dutil, NULL ); + call_haveDevID( globals, buf, GITREV, now, onConflict ); } static void diff --git a/xwords4/wasm/xwutils.js b/xwords4/wasm/xwutils.js index b13336ac9..b89dfbb93 100644 --- a/xwords4/wasm/xwutils.js +++ b/xwords4/wasm/xwutils.js @@ -8,7 +8,46 @@ function ccallString(proc, closure, str) { [proc, closure, str]); } -function onHaveDevID(closure, devid, proc) { +function registerOnce(devid, gitrev, now) { + let nextTimeKey = 'next_reg'; + let gitKey = 'last_write'; + let nextTime = localStorage.getItem(nextTimeKey); + let prevGit = localStorage.getItem(gitKey); + // false: for now, always register. PENDING + if ( false && prevGit == gitrev && now < nextTime ) { + // console.log("registerOnce: skipping"); + } else { + let args = { devid: devid, + gitrev: gitrev, + loc: navigator.language, + os: navigator.appName, + vers: '0.0', + dbg: true, + myNow: now, + vrntName: 'wasm', + }; + let body = JSON.stringify(args); + // console.log('registerOnce(): args: ' + body); + + fetch('/xw4/api/v1/register', { + method: 'post', + body: body, + headers: { + 'Content-Type': 'application/json' + }, + }).then(res => { + return res.json(); + }).then(data => { + console.log('data: ' + JSON.stringify(data)); + if ( data.success ) { + localStorage.setItem(nextTimeKey, data.atNext); + localStorage.setItem(gitKey, gitrev); + } + }); + } +} + +function onHaveDevID(closure, devid, gitrev, now, proc) { // Set a unique tag so we know if somebody comes along later let tabID = Math.random(); localStorage.setItem('tabID', tabID); @@ -22,6 +61,8 @@ function onHaveDevID(closure, devid, proc) { }; window.addEventListener('storage', listener); + registerOnce(devid, gitrev, now); + state.closure = closure; document.getElementById("mqtt_span").textContent=devid;